I have 3 tables - Production, History & Lookup. Production & History are identical and the History table contains any data over a month old from the Production table. The lookup table contains description information pertaining to data in the other table.
Tables -
HISTORY -
CREATE TABLE [dbo].[History](
[DTStamp] [datetime] NOT NULL,
[ProductionOrder] varchar NOT NULL,
[ItemNo] varchar NOT NULL,
[Quantity] varchar NOT NULL,
[LastModified] [datetime] NOT NULL
) ON [PRIMARY]
DATA -
CREATE TABLE [dbo].[Production](
[DTStamp] [datetime] NOT NULL,
[ProductionOrder] varchar NOT NULL,
[ItemNo] varchar NOT NULL,
[Quantity] varchar NOT NULL,
[LastModified] [datetime] NOT NULL
) ON [PRIMARY]
LOOKUP -
CREATE TABLE [dbo].[Lookup](
[ItemNo] varchar NULL,
[Description] varchar NULL,
[Description2] varchar NULL
) ON [PRIMARY]
I want to create a sql to output data from the either the Production or History table depending upon the user dates. The History table contains all data from more than 30 days from todays date. EG - Anything earlier than 09\10\2016 is stored in History, the newer data is logged into Production. If the user wants a range of dates, then the SQL must fetch the data from both the tables and get the corresponding description from the lookup table.
DATA in the tables -
HISTORY -
DTStamp PO ItemNo QTY LastModified
2016-06-22 17:45:03.517 P025439 8731-T499-XLXL 1 2016-06-22 13:46:00.327
2016-06-22 17:45:05.720 P025303 8730-T499-M 1 2016-06-22 13:46:00.327
2016-06-22 17:45:06.987 P025439 8731-T499-XLXL 1 2016-06-22 13:46:00.327
2016-06-27 17:45:08.347 P025303 8730-T499-M 1 2016-06-27 13:46:00.327
2016-06-27 17:45:11.547 P025439 8731-T499-XLXL 1 2016-06-27 13:46:00.327
PRODUCTION -
DTStamp PO ItemNo QTY LastModified
2016-10-02 21:31:10.837 P025564 8730-T499-S 1 2016-10-02 17:32:02.190
2016-10-02 21:31:13.557 P025564 8730-T499-S 1 2016-10-02 17:32:02.190
2016-10-03 21:31:16.213 P025564 8730-T499-S 1 2016-10-03 17:32:02.190
2016-10-04 21:31:18.417 P025564 8730-T499-S 1 2016-10-04 17:32:02.190
LOOKUP -
ItemNo Description Description2
8730-T499-S TEST DESC 1 8415016411719
8730-T499-M TEST DESC 2 8415016411717
8730-T499-XL TEST DESC 3 8415016411751
8731-T499-XLXL TEST DESC 4 8415016410916
8735-T499-S TEST DESC 5 8415016411719
8734-T499-L TEST DESC 6 8415015801355
8735-T499-SS TEST DESC 7 8415016411717
I need an output for a date range of 2016-06-27 to 2016-10-02 [The dates can vary] like -
DTStamp PO ItemNo QTY LastModified Description
2016-06-27 17:45:08.347 P025303 8730-T499-M 1 2016-06-27 13:46:00.327 TEST DESC 2
2016-06-27 17:45:11.547 P025439 8731-T499-XLXL 1 2016-06-27 13:46:00.327 TEST DESC 4
2016-10-02 21:31:10.837 P025564 8730-T499-S 1 2016-10-02 17:32:02.190 TEST DESC 1
2016-10-02 21:31:13.557 P025564 8730-T499-S 1 2016-10-02 17:32:02.190 TEST DESC 1
I am a newbie at this. Any help will be welcome. If there is a better way to do the history between 2 tables and the description, I will be happy to learn. The front end selects 2 dates - From and TO for the SQL.
Thanks,
MM.