Author |
Topic |
legacyvbc
Starting Member
37 Posts |
Posted - 2007-10-09 : 13:12:22
|
I'm trying to avoid using functions since they take so long but I have table of orders with 3 different transaction types and am trying to select a sum of each for each month over the past several yearsTable tOrder looks like this:Date,TransId,AmountI would like to select it to something like:Date,TranId1,TranId2,TranId3Where Dates are aggregated into months and TranId1 = Sum(TranId1) during the month and TranId2 = Sum(TranId2) etc....Sample Data and Result I am trying to achieve are below.ThanksDECLARE @tbl TABLE(Date smalldatetime,TranId tinyint,Amt money)INSERT @tblSELECT '20041209', 1, 83147 UNION ALLSELECT '20041210', 1, 64771 UNION ALLSELECT '20050427', 0, 21232 UNION ALLSELECT '20050428', 0, 29270 UNION ALLSELECT '20050502', 1, 91852 UNION ALLSELECT '20050502', 1, 81108 UNION ALLSELECT '20050708', 2, 108502 UNION ALLSELECT '20050817', 2, 66506 UNION ALLSELECT '20050818', 2, 32160 UNION ALLSELECT '20050818', 0, 70197 UNION ALLSELECT '20050819', 2, 29357I Am Trying to Get the Query to Return:DATE TranId0 TranId1 TranId212/31/2004 -- 147,918 -- 4/30/2005 50,502 -- -- 5/31/2005 -- 172,960 -- 7/31/2005 -- -- 108,502 8/31/2005 70,197 -- 128,023 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-09 : 13:30:33
|
DECLARE @tbl TABLE(Date smalldatetime,TranId tinyint,Amt money)INSERT @tblSELECT '20041209', 1, 83147 UNION ALLSELECT '20041210', 1, 64771 UNION ALLSELECT '20050427', 0, 21232 UNION ALLSELECT '20050428', 0, 29270 UNION ALLSELECT '20050502', 1, 91852 UNION ALLSELECT '20050502', 1, 81108 UNION ALLSELECT '20050708', 2, 108502 UNION ALLSELECT '20050817', 2, 66506 UNION ALLSELECT '20050818', 2, 32160 UNION ALLSELECT '20050818', 0, 70197 UNION ALLSELECT '20050819', 2, 29357SELECT [Date] = DATEADD(month, DATEDIFF(month, -1, date), -1) /* I stole the above line from somebody else on this site, he had a whole list of them*/ ,TranId0 = SUM(CASE WHEN TranId = 0 THEN amt ELSE 0 END) ,TranId1 = SUM(CASE WHEN TranId = 1 THEN amt ELSE 0 END) ,TranId2 = SUM(CASE WHEN TranId = 2 THEN amt ELSE 0 END)FROM @tblGROUP BY DATEADD(month, DATEDIFF(month, -1, date), -1) Jim |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-09 : 13:32:29
|
Oops. This will format the date for you[Date] = CONVERT(varchar(10), DATEADD(month, DATEDIFF(month, -1, date), -1) ,101)Jim |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2007-10-09 : 16:44:57
|
Thank you so much - works great! |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2007-10-09 : 23:22:44
|
I thought this solution was going to work, however it is more complicated than I thought because I need to join some tables and DATE=DATEADD(month,DATEDIFF(month,-1,b.DATE),-1)returns the last day of the month but I actually need the last business day of the month since the info I am joining and even the orders use the last business day not the last day of the month.As an example, 6/28/1996 is the last business day of the month yet the table below actually returns 6/30/1996 which I cannot use when joining other tables.Any ideas? Thanks |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-10-10 : 10:21:19
|
This actually makes the problem a lot harder because last business day of month isn't well defined. For example, if Dec 30th is a Thursday, Dec 31st might not be a businessday. Your best bet is to make a calendar table with a column that determines whether a given day is business day or not. If you search this site for "business days" or "calendar"you might come up with something useful. There's some pretty smart people on this site that have addressed your problem.Good LuckJim |
 |
|
legacyvbc
Starting Member
37 Posts |
Posted - 2007-10-10 : 12:16:59
|
Ok Thanks |
 |
|
|
|
|