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 |
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-04-29 : 05:59:09
|
I have a query that returns data with a total amountdeclare @Value intset @Value = 10000SELECT cust_name, costcent, SUM(Invoices.amount) AS amount, SUM( CASE WHEN amount < totamnt AND totamnt = promamt THEN amount ELSE PromAmt END )AS AmountPromisedFROM Invoices WHERE Invoices.PaidFlag = 0GROUP BY cust_name, costcentHAVING SUM(Invoices.amount) >= @ValueORDER BY cust_name, costcent Retunrs data as3 HARDMAN SQUARE RETAIL LIMITED ZMCR 11045.0000 11045.0000A-ONE+ ZCIN 899553.9000 NULLA1 MOBIL KONSORTIUM ZDTI 23269.0300 NULLACCIONA ZDTI 11896.8800 NULL Now, the amount is a total promised amount for a load of invoices.I need to add the actual month the amount was promised as a separate column:select datename(month, promamtdate), datename(year, promamtdate) from invoices where promamtdate is not null So, I would get:cust_name, costcent, amount, AmountPromised, Nov 2007, Dec 2007, Jan 2008, Feb 2008, March 2008, April 2008, June 2008, etc. I guess the columns need to be dynamic.Any ideas on how to do this?Could I do it with a case statement or am I going to have to create a temporary table? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-29 : 06:21:24
|
How many columns will you need to show? six months forward always? |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-04-29 : 06:24:25
|
Well, it has to be somewhat dynamic. It could be whatever is in the tables, usually 6 months back, 3 months forward.However, I would be able to get hold of a distinct list using the SQL:select datename(month, promamtdate), datename(year, promamtdate) from invoices where promamtdate is not nullgroup by datename(month, promamtdate), datename(year, promamtdate) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-29 : 08:14:59
|
I think this should give you a start:http://www.mssqltips.com/tip.asp?tip=937 |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-04-29 : 11:38:53
|
I have tried one of those crosstab queries but the part giving me problems is when I run into data like this:LINPAVE BUILDING LIMITED ZBIA 10575.0000 10575.0000 April 2008LINPAVE BUILDING LIMITED ZBIA 35250.0000 35250.0000 May 2008 The sp_Crosstab sproc generates the column names properly but it's then supposed to aggregate the 2 values into 1 line.However, it still presents them as 2 lines as that is how the data is presented:declare @Value intset @Value = 10000SELECT cust_name, costcent, SUM(Invoices.amount) AS amount, SUM( CASE WHEN amount < totamnt AND totamnt = promamt THEN amount ELSE ISNULL(PromAmt,0) END )AS AmountPromised, datename(month, promamtdate) AS MonthName, datename(year, promamtdate) AS YearNameFROM Invoices WHERE Invoices.PaidFlag = 0 and datename(month, promamtdate) is not nullGROUP BY cust_name, costcent, datename(month, promamtdate), datename(year, promamtdate)HAVING SUM(Invoices.amount) >= @ValueORDER BY cust_name, costcent |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-04-30 : 05:59:09
|
any ideas? |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2008-04-30 : 06:09:43
|
So, I have come up with thisdeclare @Value intset @value = 10000--select datename(month, dateadd(month,-1, getdate()))SELECT cust_name, costcent, SUM(Invoices.amount) AS amount, SUM( CASE WHEN amount < totamnt AND totamnt = promamt THEN amount ELSE ISNULL(PromAmt,0) END )AS AmountPromised, SUM(CASE datename(month, promamtdate) WHEN datename(month, dateadd(month,-2, getdate())) THEN promamt ELSE 0 END) AS [-2], SUM(CASE datename(month, promamtdate) WHEN datename(month, dateadd(month,-1, getdate())) THEN promamt ELSE 0 END) AS [-1], SUM(CASE datename(month, promamtdate) WHEN datename(month, dateadd(month,0, getdate())) THEN promamt ELSE 0 END) AS [0], SUM(CASE datename(month, promamtdate) WHEN datename(month, dateadd(month,+1, getdate())) THEN promamt ELSE 0 END) AS [1], SUM(CASE datename(month, promamtdate) WHEN datename(month, dateadd(month,+2, getdate())) THEN promamt ELSE 0 END) AS [2]FROM Invoices WHERE Invoices.PaidFlag = 0GROUP BY cust_name, costcentHAVING SUM(Invoices.amount) >= @ValueORDER BY cust_name, costcent But I need to make the columns dynamic somehow.Any thoughts? Can I use a varibale in the AS [2] section ?Or should I do this in the calling application? |
 |
|
|
|
|
|
|