Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get total of months, like Year to Day in cross tab query?


How to get total of months, like Year to Day in cross tab query?

Author
Message
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
I used code below to create cross tab query. It works fine.
How to add code to get total of months, like Year to Day?

SELECT *
FROM (
SELECT
year(invoiceDate) as [year],left(datename(month,invoicedate),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pivot
Jim Mackenzie
Jim Mackenzie
Old Hand
Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)

Group: General Forum Members
Points: 350 Visits: 715
Any chance you could provide us with some sample data so we can test the query that you've written?

SQL SERVER Central Forum Etiquette
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
I found the code here but no test data only screen shot.

http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query
Jim Mackenzie
Jim Mackenzie
Old Hand
Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)

Group: General Forum Members
Points: 350 Visits: 715
This is quick and dirty, but since the number of months in a year isn't going to change any time soon you can do this:


;With Invoice AS
(
SELECT '20130101' [InvoiceDate], 100 [InvoiceAmount] UNION ALL
SELECT '20120102', 101 UNION ALL
SELECT '20120202', 140 UNION ALL
SELECT '20120302', 130 UNION ALL
SELECT '20120402', 120 UNION ALL
SELECT '20120502', 110 UNION ALL
SELECT '20130102', 101 UNION ALL
SELECT '20130202', 140 UNION ALL
SELECT '20130302', 130 UNION ALL
SELECT '20130402', 120 UNION ALL
SELECT '20130502', 110 UNION ALL
SELECT '20130502', 111
)


SELECT *,   ISNULL(jan,0) +
         ISNULL(feb,0) +
         ISNULL(mar,0) +
         ISNULL(apr,0) +
         ISNULL(may,0) +
         ISNULL(jun,0) +
         ISNULL(jul,0) +
         ISNULL(aug,0) +
         ISNULL(sep,0) +
         ISNULL(oct,0) +
         ISNULL(nov,0) +
         ISNULL(dec,0) [YearToDate]
FROM (
SELECT
year(CONVERT(DATE,InvoiceDate)) as [year],left(datename(month,CONVERT(DATE,InvoiceDate)),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
)AS [pivot]



SQL SERVER Central Forum Etiquette
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
It is working great.
One more question, I want to insert the result into a temp table but code below did not work.
Something wrong with my code?

select * into #myinvoice from ( //your code below

SELECT *,   ISNULL(jan,0) +
         ISNULL(feb,0) +
         ISNULL(mar,0) +
         ISNULL(apr,0) +
         ISNULL(may,0) +
         ISNULL(jun,0) +
         ISNULL(jul,0) +
         ISNULL(aug,0) +
         ISNULL(sep,0) +
         ISNULL(oct,0) +
         ISNULL(nov,0) +
         ISNULL(dec,0) [YearToDate]
FROM (
SELECT
year(CONVERT(DATE,InvoiceDate)) as [year],left(datename(month,CONVERT(DATE,InvoiceDate)),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
)AS [pivot]

)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
adonetok (7/2/2013)
It is working great.
One more question, I want to insert the result into a temp table but code below did not work.
Something wrong with my code?

select * into #myinvoice from ( //your code below

SELECT *,   ISNULL(jan,0) +
         ISNULL(feb,0) +
         ISNULL(mar,0) +
         ISNULL(apr,0) +
         ISNULL(may,0) +
         ISNULL(jun,0) +
         ISNULL(jul,0) +
         ISNULL(aug,0) +
         ISNULL(sep,0) +
         ISNULL(oct,0) +
         ISNULL(nov,0) +
         ISNULL(dec,0) [YearToDate]
FROM (
SELECT
year(CONVERT(DATE,InvoiceDate)) as [year],left(datename(month,CONVERT(DATE,InvoiceDate)),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
)AS [pivot]

)


You need to give you table an alias OR just do this as an insert.


select * into #myinvoice from ( //your code below

SELECT *,   ISNULL(jan,0) +
         ISNULL(feb,0) +
         ISNULL(mar,0) +
         ISNULL(apr,0) +
         ISNULL(may,0) +
         ISNULL(jun,0) +
         ISNULL(jul,0) +
         ISNULL(aug,0) +
         ISNULL(sep,0) +
         ISNULL(oct,0) +
         ISNULL(nov,0) +
         ISNULL(dec,0) [YearToDate]
FROM (
SELECT
year(CONVERT(DATE,InvoiceDate)) as [year],left(datename(month,CONVERT(DATE,InvoiceDate)),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
)AS [pivot]

) as MYALIAS



Or the simpler version....


INSERT into #myinvoice

SELECT *,   ISNULL(jan,0) +
         ISNULL(feb,0) +
         ISNULL(mar,0) +
         ISNULL(apr,0) +
         ISNULL(may,0) +
         ISNULL(jun,0) +
         ISNULL(jul,0) +
         ISNULL(aug,0) +
         ISNULL(sep,0) +
         ISNULL(oct,0) +
         ISNULL(nov,0) +
         ISNULL(dec,0) [YearToDate]
FROM (
SELECT
year(CONVERT(DATE,InvoiceDate)) as [year],left(datename(month,CONVERT(DATE,InvoiceDate)),3)as [month],
InvoiceAmount as Amount
FROM Invoice
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
)AS [pivot]



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
Thank you very much. Now all is working.
The key is "You need to give you table an alias"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search