Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
fmazzarelli
Partner - Creator III
Partner - Creator III

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.

SELECT * FROM (SELECT year(invoiceDate) as [year], left(datename(month,invoicedate),3)as [month], _ InvoiceAmount as Amount FROM Invoice) as InvoiceResult 

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

2 Replies
sunny_talwar

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?

agigliotti
Partner - Champion
Partner - Champion

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.