Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-06-15 : 18:33:24
|
| HiI am completely stuck trying to make a queryHeres is a shorten downed example of what I am trying to achieve. I have four tables. The first being OrderHeader, the second OrderLines, the third StockCategory. The OrderHeader table contains basic order details, which in turn is linked to the OrderLines table which show if the order in the OrderHeader has a single or multiple order lines. The StockCategory table shows what stock group the item in each order line is associated to. Here is an example of the tables in a shorten downed version (in both data and fields):OrderHeader:Sales Order Ref, Order Date, 1, 01/05/20082, 01/05/20083, 02/05/20084, 02/05/2008OrderLines:Sales Order Ref, Part Number1, 2221, 2341, 3332, 2222, 5552, 4443, 3333, 1114, 222StockCategoryStock Category, Part Number, A, 222B, 234C, 333D, 444E, 111Stock Group:Stock Group, Stock Category, PriorityBerr, A, 1Berr, B, 1OGD, C, 2OGD, D, 2DFID , E, 3The thing I am trying to do is assign each of the orders to a Stock Group which I can do. The thing that has baffled me is if an two of the order lines in one order are assigned to different stock groups. If this occurs I want to assign the order to the order to the stock group with the highest priority (1 being highest, 3 being lowest) for example if one order line in the order was assigned to Berr (priority 1) and the other to DFID (priority 3) the order would be assigned to Berr.I am using the following query:DECLARE @Period DATETIMEDECLARE @BeginDate DATETIMEDECLARE @EndDate DATETIMESET @Period =’2008-05-01’SELECT @BeginDate = [BeginDate] FROM DatePeriods AS dp WHERE dp.ID = @PeriodSELECT @EndDate = [EndDate] FROM DatePeriods AS dp WHERE dp.ID = @PeriodSELECTCOUNT(oh.[Sales Order Reference])FROM dbo.OrderHeaders AS ohLEFT JOIN dbo.OrderLines AS ol ON oh.[Sales Order Reference] = ol.[Sales Order Reference]LEFT JOIN dbo.StockCategories AS sc ON ol.[Part Number] = sc.[Part Number]WHERE oh.[Order Date] BETWEEN @BeginDate AND @EndDateAND sc.[Stock Group] IN (SELECT sg.[Stock Group] FROM dbo.StockGroup AS sg WHERE sg.[ID] = 'Berr')AND ol.[Sales Order Reference] NOT IN (SELECT col.[Sales Order Reference]FROM dbo.CancelledOrderLines AS colWHERE col.[Part Number] = ol.[Part Number])GROUP BY oh.[Sales Order Reference]Is this possible to do?Thanking you in advance!!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-16 : 00:36:17
|
Is this what you're looking for?DECLARE @OrderHeader table(Sales_Order_Ref int, Order_Date datetime)INSERT INTO @OrderHeaderSELECT 1, '01/05/2008'UNION ALLSELECT 2, '01/05/2008'UNION ALLSELECT 3, '02/05/2008'UNION ALLSELECT 4, '02/05/2008'DECLARE @OrderLines table(Sales_Order_Ref int, Part_Number int)INSERT INTO @OrderLinesSELECT 1, 222UNION ALLSELECT 1, 234UNION ALLSELECT 1, 333UNION ALLSELECT 2, 222UNION ALLSELECT 2, 555UNION ALLSELECT 2, 444UNION ALLSELECT 3, 333UNION ALLSELECT 3, 111UNION ALLSELECT 4, 222DECLARE @StockCategory table(Stock_Category char(1), Part_Number int)INSERT INTO @StockCategorySELECT 'A', 222UNION ALLSELECT 'B', 234UNION ALLSELECT 'C', 333UNION ALLSELECT 'D', 444UNION ALLSELECT 'E', 111DECLARE @StockGroup table(Stock_Group varchar(5), Stock_Category char(1), Priority int)INSERT INTO @StockGroupSELECT 'Berr', 'A', 1UNION ALLSELECT 'Berr', 'B', 1UNION ALLSELECT 'OGD', 'C', 2UNION ALLSELECT 'OGD', 'D', 2UNION ALLSELECT 'DFID' , 'E', 3SELECT MAX(Stock_Group) AS StockGroup,COUNT(DISTINCT Sales_Order_Ref) AS OrderLineCountFROM @StockGroup sg1LEFT JOIN(SELECT ol.Sales_Order_Ref,MIN(Priority) AS HighestPriorityFROM @OrderHeader ohINNER JOIN @OrderLines olON ol.Sales_Order_Ref=oh.Sales_Order_RefINNER JOIN @StockCategory scON sc.Part_Number=ol.Part_NumberINNER JOIN @StockGroup sgON sg.Stock_Category=sc.Stock_CategoryGROUP BY ol.Sales_Order_Ref) tON t.HighestPriority=sg1.PriorityGROUP BY HighestPriorityStockGroup OrderLineCount---------- --------------DFID 0Berr 3OGD 1 |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-06-16 : 04:46:30
|
| HiThanks for that, is it possible to put in a similar syntax that I have used above for consistency. Sorry to be a pain.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-16 : 05:02:38
|
quote: Originally posted by rcr69er HiThanks for that, is it possible to put in a similar syntax that I have used above for consistency. Sorry to be a pain.Thanks
Similar syntax? didnt understand that. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-16 : 05:20:00
|
Did you mean this?SELECT MAX(Stock_Group) AS StockGroup,COUNT(DISTINCT Sales_Order_Ref) AS OrderLineCountFROM @StockGroup sg1LEFT JOIN(SELECT ol.Sales_Order_Ref,MIN(Priority) AS HighestPriorityFROM @OrderHeader ohLEFT JOIN @OrderLines olON ol.Sales_Order_Ref=oh.Sales_Order_RefLEFT JOIN @StockCategory scON sc.Part_Number=ol.Part_NumberLEFT JOIN @StockGroup sgON sg.Stock_Category=sc.Stock_CategoryWHERE oh.[Order Date] BETWEEN @BeginDate AND @EndDateGROUP BY ol.Sales_Order_Ref) tON t.HighestPriority=sg1.PriorityGROUP BY HighestPriority |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-06-16 : 06:01:06
|
| Sorry I meant apply it to the query that I had already created.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-16 : 06:09:33
|
quote: Originally posted by rcr69er Sorry I meant apply it to the query that I had already created.Thanks
But that wont give you desired result. that was the reason why i cahnged it. ANy reason why you cant use mine? |
 |
|
|
|
|
|
|
|