Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

fmazzarelli
Contributor 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
MVP
MVP

Re: howto: pivot table using sql

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
Honored Contributor II

Re: howto: pivot table using sql

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.