
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
howto: pivot table using sql
Hi Community,
may you help to get started with PIVOT queries. Most business applications will need some sort of PIVOT queries and I am sure many of you must have come across pivoting requirements several times.
Using the Code
Let us have a table name Invoice
which has three properties, InvoiceNumber
, InvoiceDate
, InvoiceAmount
. Suppose we have several rows input in the table. Our goal is to display the sum of InvoiceAmount
each month.
Hide Copy Code
SELECT * FROM (SELECT year(invoiceDate) as [year], left(datename(month,invoicedate),3)as [month], _ InvoiceAmount as Amount FROM Invoice) as InvoiceResult
Hide Copy Code
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 pvt

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can do this in the script using The Generic Load, but why not load the data normally and create a pivot table in the front end of the application?


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you can achieve it simple using the below script:
SELECT
year(invoiceDate) as year,
month(invoiceDate) as month
InvoiceAmount as Amount
FROM your_invoices_table;
in the front end create a pivot table with year and month as dimensions and Sum(Amount) as measure.
