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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Loop thru all month

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 time

here is my sql:
SELECT SUM(totals) AS totals
FROM (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 DERIVEDTBL

thanks 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 time

here is my sql:
SELECT SUM(totals) AS totals
FROM (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 DERIVEDTBL

thanks a lot,

mike




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

mike13
Posting Yak Master

219 Posts

Posted - 2012-01-09 : 10:54:03
yes i did.
please note this
SELECT SUM(totals) AS totals
FROM (...) AS DERIVEDTBL

that only give me one value
Go to Top of Page

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 this

SELECT
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 DERIVEDTBL
GROUP 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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 = 8
should be

om.Orderstatus = 8

to 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
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -