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 |
mike13
Posting Yak Master
219 Posts |
Posted - 2012-01-09 : 10:22:18
|
Hi all,I want to loop thru the 12 month, so i do not need to change the value of (MONTH(dbo.T_Order_Main.Shipdate) = 12) each timehere is my sql:SELECT SUM(totals) AS totalsFROM (SELECT SUM(dbo.T_Order_Detail.Quantity * dbo.T_Order_Detail.Cost) AS totals, dbo.T_Order_Ship.payment FROM dbo.T_Order_Main INNER JOIN dbo.T_Order_Ship ON dbo.T_Order_Main.ORDERID = dbo.T_Order_Ship.OrderID RIGHT OUTER JOIN dbo.T_Order_Detail ON dbo.T_Order_Main.ORDERID = dbo.T_Order_Detail.OrderID WHERE (dbo.T_Order_Main.Orderstatus = 8) AND (YEAR(dbo.T_Order_Main.Shipdate) = 2011) AND (MONTH(dbo.T_Order_Main.Shipdate) = 12) AND (dbo.T_Order_Ship.payment = 'btransfer') GROUP BY dbo.T_Order_Detail.OrderID, dbo.T_Order_Ship.payment) AS DERIVEDTBLthanks a lot,mike |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-01-09 : 10:33:56
|
Try this:quote: Originally posted by mike13 Hi all,I want to loop thru the 12 month, so i do not need to change the value of (MONTH(dbo.T_Order_Main.Shipdate) = 12) each timehere is my sql:SELECT SUM(totals) AS totalsFROM (SELECT SUM(dbo.T_Order_Detail.Quantity * dbo.T_Order_Detail.Cost) AS totals, dbo.T_Order_Ship.payment FROM dbo.T_Order_Main INNER JOIN dbo.T_Order_Ship ON dbo.T_Order_Main.ORDERID = dbo.T_Order_Ship.OrderID RIGHT OUTER JOIN dbo.T_Order_Detail ON dbo.T_Order_Main.ORDERID = dbo.T_Order_Detail.OrderID WHERE (dbo.T_Order_Main.Orderstatus = 8) AND (YEAR(dbo.T_Order_Main.Shipdate) = 2011) AND (MONTH(dbo.T_Order_Main.Shipdate) = 12) AND (dbo.T_Order_Ship.payment = 'btransfer') GROUP BY MONTH(dbo.T_Order_Main.Shipdate),dbo.T_Order_Detail.OrderID, dbo.T_Order_Ship.payment) AS DERIVEDTBLthanks a lot,mike
No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
mike13
Posting Yak Master
219 Posts |
Posted - 2012-01-09 : 10:39:56
|
thank, but that didn't work, just give me the value of december |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-01-09 : 10:42:15
|
Have you seen the changes in the WHERE clause? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
mike13
Posting Yak Master
219 Posts |
Posted - 2012-01-09 : 10:54:03
|
yes i did.please note thisSELECT SUM(totals) AS totalsFROM (...) AS DERIVEDTBLthat only give me one value |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-09 : 11:52:02
|
I suspect that you don't really know what you want.The reason you only get one value is that you are wrapping your query and selecting the MAX without any grouping.Also -- I'm pretty suspicious of the RIGHT OUTER JOIN.I've cleaned up the code and used aliases -- they are much easier to read than repeating the table names so many times.Try thisSELECT SUM([totals]) AS [totalMonthPayment] , [ShipDateMonth] AS [ShipDateMonth]FROM ( SELECT SUM(od.[Quantity] * od.[Cost]) AS [totals] , os.[payment] AS [payment] , MONTH(om.[ShipDate]) AS [ShipDateMonth] FROM dbo.T_Order_Main AS om INNER JOIN dbo.T_Order_Ship AS os ON om.ORDERID = os.OrderID RIGHT OUTER JOIN dbo.T_Order_Detail AS od ON om.ORDERID = od.OrderID WHERE om = 8 --AND YEAR(om.Shipdate) = 2011 (THIS IS BAD IT CAN'T USE AN INDEX) AND om.[ShipDate] >= '20110101' AND om.[ShipDate] < '20120101' AND os.[payment] = 'btransfer' GROUP BY od.[OrderID] , os.[payment] , MONTH(om.[ShipDate]) ) AS DERIVEDTBLGROUP BY [ShipDateMonth] I've also changed the WHERE clause to allow you to use an index over the ShipDate column.The query now groups by the month and is for the whole of 2011.You are throwing away the order_ship.[payment] column but grouping by that inside the derived table. Why would you want to do that?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
mike13
Posting Yak Master
219 Posts |
Posted - 2012-01-09 : 15:53:02
|
Hi Charlie,Thanks a lot, this speed thing up.One small typo WHERE om = 8should beom.Orderstatus = 8to answer you questions, i do not know, i did most in Visual studio or sql editor, do not know to much T-SQL more a programmer.Thanks a lot for the solution |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-01-09 : 19:30:59
|
glad to help.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|