SQL Server - Convert columns to rows - sql

I have a table on which simple select gives out put like below
I want to write a select statement to output like below
Can someone help me...

Since you are basically rotating your current columns of Sale, Income and Profit into rows and then move the month values to columns, then you will want to first unpivot the current columns, then pivot the months.
Depending on your version of SQL Server there are a few ways that you can unpivot the data. You can use the UNPIVOT function or CROSS APPLY:
select month, type, value
from yourtable
cross apply
(
select 'Sale', sale union all
select 'Income', Income union all
select 'Profit', Profit
) c (type, value)
See SQL Fiddle with Demo. This will convert your current data into:
| MONTH | TYPE | VALUE |
|-------|--------|-------|
| Jan | Sale | 100 |
| Jan | Income | 50 |
| Jan | Profit | 10 |
| Feb | Sale | 20 |
| Feb | Income | 40 |
Then you can use the PIVOT function to convert the months into your column headers.
select type, Jan, Feb, Mar, Apr
from
(
select month, type, value
from yourtable
cross apply
(
select 'Sale', sale union all
select 'Income', Income union all
select 'Profit', Profit
) c (type, value)
) d
pivot
(
sum(value)
for month in (Jan, Feb, Mar, Apr)
) piv;
See SQL Fiddle with Demo.
if you have an unknown number of months, then you can use dynamic SQL:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct N',' + QUOTENAME(Month)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = N'SELECT type, ' + #cols + N'
from
(
select month, type, value
from yourtable
cross apply
(
select ''Sale'', sale union all
select ''Income'', Income union all
select ''Profit'', Profit
) c (type, value)
) x
pivot
(
sum(value)
for month in (' + #cols + N')
) p '
execute sp_executesql #query;
See SQL Fiddle with Demo

You Can Use UNPIVOT then PIVOT
THE BEST IS TO DO QUERY EMBEDED SQL
create DISTINCT COLUMS of months with STUFF function then
replace FOR oMonth IN ([January-2013], [February-2013], [March-2013], [April-2013])
here core query
SELECT
*
FROM
( SELECT
oMonth, value,col
from (
select DATENAME(month,oDate) + '-' + CAST(YEAR( oDate) as varchar) as oMonth, Sales ,Income,Profit
FROM SalesSource
)A
unpivot
(
value for col in ( Sales ,Income,Profit)
) u
) as sourceTable
PIVOT
(
sum( value)
FOR oMonth IN ([January-2013], [February-2013], [March-2013], [April-2013])
) AS PivotTable;

Related

How can I join the results of multiple pivot queries together horizontally?

I may not have phrased the question clearly so I'll clarify it here.
I've got a VB.NET system that displays data. To display data, I have the following SQL stored procedure that makes use of pivot:
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(form_column_id) from
GetFormColumns(#formTemplateId) FOR XML PATH(''),
TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set #query = N' select *
from
(select row_number as Row,fc.form_column_id, fdd.data
from
form_data_h fdh
inner join form_data_d fdd on fdd.form_data_h_id = fdh.form_data_h_id
inner join form_column fc on fc.form_column_id = fdd.form_column_id
inner join column_header c on c.column_header_id=fc.column_header_id
where fdh.is_active = 1 and fdh.form_data_h_id= ' +
CONVERT(varchar(10),#formDataHId) + ' and fc.is_active = 1
) src
pivot(
min(data)
for form_column_id in (' + #cols + N')
) piv'
execute(#query);
I had to make use of pivot because user data entry needs to be dynamic. So from this...
table results
The results now look like this.
pivot results
This data has different results filtered by particular parameters. Right now I've displayed the crop data for 2017. I want to join it with the crop data for 2018 (will change based on the Stored Procedure parameter #formDataHId).
That should result in something that looks like this...
2017 and 2018 results
Is there any way I can go about this in SQL or do I have to do that in VB.net? If so, how can I go about it?
Any ideas would be welcome because I'm a bit stumped right now. If users need to see data from let's say 2016 to 2019, it should be presented like that.
Pivot in SQL should be enough. If the structure of data in each year is the same, you can use UNION ALL between each year's data. Then use your query to pivot the combined years' data.
In my pivot function I use a table named Test to generate the pivot. I have 2 other tables Source2017 and Source2018. I insert both Source2017 and Source2018 using this query.
INSERT #Test
SELECT Date, Item, Quantity FROM #Source2017
UNION ALL
SELECT Date, Item, Quantity FROM #Source2018
If I only want data from 2017, I remove the Source2018 from the insert statement.
INSERT #Test
SELECT Date, Item, Quantity FROM #Source2017
Let's say I have more tables, Source2015 and Source2016. If I want to pivot all of them, just add the tables using UNION ALL.
INSERT #Test
SELECT Date, Item, Quantity FROM #Source2015
UNION ALL
SELECT Date, Item, Quantity FROM #Source2016
UNION ALL
SELECT Date, Item, Quantity FROM #Source2017
UNION ALL
SELECT Date, Item, Quantity FROM #Source2018
Full query:
IF OBJECT_ID('tempdb..#Test') IS NOT NUll DROP TABLE #Test
IF OBJECT_ID('tempdb..#Source2017') IS NOT NUll DROP TABLE #Source2017
IF OBJECT_ID('tempdb..#Source2018') IS NOT NUll DROP TABLE #Source2018
CREATE TABLE #Test
(
Date DATE,
Item VARCHAR(100),
Quantity INT
)
CREATE TABLE #Source2017
(
Date DATE,
Item VARCHAR(100),
Quantity INT
)
CREATE TABLE #Source2018
(
Date DATE,
Item VARCHAR(100),
Quantity INT
)
INSERT #Source2017 VALUES
('2017/01/01', 'Mango', 5),
('2017/01/01', 'Orange', 6),
('2017/01/02', 'Mango', 7),
('2017/01/02', 'Orange', 8),
('2017/01/02', 'Cherry', 9)
INSERT #Source2018 VALUES
('2018/01/01', 'Durian', 15),
('2018/01/02', 'Orange', 28),
('2018/01/03', 'Cherry', 19)
INSERT #Test
SELECT Date, Item, Quantity FROM #Source2017
UNION ALL
SELECT Date, Item, Quantity FROM #Source2018
DECLARE #SQL AS VARCHAR(MAX)
DECLARE #Columns AS VARCHAR(MAX)
DECLARE #Columns2 AS VARCHAR(MAX)
SELECT #Columns = COALESCE(#Columns + ',','') + QUOTENAME(Date)
FROM (SELECT DISTINCT Date FROM #Test) AS B
ORDER BY B.Date
SELECT #Columns2 = COALESCE(#Columns2 + ',','') + 'ISNULL(' + QUOTENAME(Date) + ', 0) AS [' + CAST(Date AS VARCHAR(100)) + ']'
FROM (SELECT DISTINCT Date FROM #Test) AS B
ORDER BY B.Date
SET #SQL = '
WITH PivotData AS
(
SELECT Date, Item, Quantity FROM #Test
)
SELECT
Item, ' + #Columns2 + '
FROM PivotData
PIVOT
(
SUM(Quantity)
FOR Date
IN (' + #Columns + ')
) AS PivotResult
ORDER BY Item'
EXEC(#SQL);
DROP TABLE #Test
DROP TABLE #Source2017
DROP TABLE #Source2018
Result:
+--------+------------+------------+------------+------------+------------+
| Item | 2017-01-01 | 2017-01-02 | 2018-01-01 | 2018-01-02 | 2018-01-03 |
+--------+------------+------------+------------+------------+------------+
| Cherry | 0 | 9 | 0 | 0 | 19 |
| Durian | 0 | 0 | 15 | 0 | 0 |
| Mango | 5 | 7 | 0 | 0 | 0 |
| Orange | 6 | 8 | 0 | 28 | 0 |
+--------+------------+------------+------------+------------+------------+

SUMIFS (Sum if with multiple conditions) with SQL Server

This should be done easy in Excel, however, I would like to have this kind of calculation done via SQL. I could use the GROUP BY , OVER() to calculate the SUM and % of a single year. But I failed to present the data 3 years at once. Any help will be appreciated.
Since you are using SQL Server, if you are using SQL Server 2005+ then you can use the PIVOT function to get the result. This solution implements both an unpivot and a pivot process to get the result. The starting point for this result is to calculate the total percent and total by type:
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
See SQL Fiddle with Demo. This will give a result with multiple columns that you want to pivot so you can unpivot the data into multiple rows using CROSS APPLY:
select type,
col = cast(year as varchar(4))+'_'+col,
value,
t_type
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select 'total', total union all
select 't_per', t_per
) c (col, value);
See Demo. Finally you can apply the PIVOT function to the values in col:
select type,
[2010_total], [2010_t_per],
[2011_total], [2011_t_per],
[2012_total], [2012_t_per],
t_type,
tot_per
from
(
select type,
col = cast(year as varchar(4))+'_'+col,
value,
t_type,
tot_per
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select 'total', total union all
select 't_per', t_per
) c (col, value)
) s
pivot
(
max(value)
for col in ([2010_total], [2010_t_per],
[2011_total], [2011_t_per],
[2012_total], [2012_t_per])
) piv
See SQL Fiddle with Demo. This could be refactored to use a CTE instead of the subqueries and this could also be converted to use dynamic SQL if the year will be unknown.
If you have an unknown number of values, then the dynamic SQL code will be:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(cast(year as varchar(4))+'_'+col)
from tablea
cross apply
(
select 'total', 1 union all
select 't_per', 2
) c (col, so)
group by year, col, so
order by year, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT type,' + #cols + ', t_type, tot_per
from
(
select type,
col = cast(year as varchar(4))+''_''+col,
value,
t_type,
tot_per
from
(
select type, year, total,
round(total / sum(total) over(partition by year)*100.0, 1) t_per,
sum(total) over(partition by type) t_type,
round(sum(total) over(partition by type)*100.0/sum(total) over(), 1) tot_per
from tablea
) d
cross apply
(
select ''total'', total union all
select ''t_per'', t_per
) c (col, value)
) x
pivot
(
max(value)
for col in (' + #cols + ')
) p '
execute sp_executesql #query;
See Demo. Both the static version and the dynamic version give the result:
| TYPE | 2010_TOTAL | 2010_T_PER | 2011_TOTAL | 2011_T_PER | 2012_TOTAL | 2012_T_PER | T_TYPE | TOT_PER |
---------------------------------------------------------------------------------------------------------
| A | 1 | 16.7 | 1 | 16.7 | 1 | 16.7 | 3 | 16.7 |
| B | 2 | 33.3 | 2 | 33.3 | 2 | 33.3 | 6 | 33.3 |
| C | 3 | 50 | 3 | 50 | 3 | 50 | 9 | 50 |
SUMIF can be replicated in SQL with SUM(case statement):
SELECT Type
,SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)'2010 Total'
,SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2010' THEN Total ELSE 0 END)) OVER () '2010 Percent of Total'
,SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)'2011 Total'
,SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2011' THEN Total ELSE 0 END)) OVER () '2011 Percent of Total'
,SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)'2012 Total'
,SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)*1.0/SUM(SUM(CASE WHEN Year = '2012' THEN Total ELSE 0 END)) OVER () '2012 Percent of Total'
,SUM(Total) 'Total'
,SUM(Total)*1.0/SUM(SUM(Total)) OVER () 'Percent of Total'
FROM Table
GROUP BY Type
For the sake of simplicity I would prefer to show this result vertically:
SELECT
Type,
Year,
SUM(Total) as Dollars,
ROUND(SUM(Total) * 100 / (SELECT SUM(TOTAL) FROM TableA t2 WHERE t2.Year = t1.Year),1) as Per
FROM TableA t1
Group By Type, Year
Output:
TYPE YEAR DOLLARS PERCENT
A 2010 1 16.7
B 2010 2 33.3
C 2010 3 50
A 2011 1 16.7
B 2011 2 33.3
C 2011 3 50
A 2012 1 16.7
B 2012 2 33.3
C 2012 3 50
Sql Fiddle Demo

SQL Server: Dynamic pivot with headers to include column name and date

I am trying to use dynamic pivot and need help on converting rows to columns
The table looks like:
ID expense revenue date
1 43 45 12-31-2012
1 32 32 01-01-2013
3 64 56 01-31-2013
4 31 32 02-31-2013
and I need for reporting purposes like
ID expense12-31-2012 expense01-01-2013 expense01-31-2013 revenue12-31-2013
1 43 32
3 64
In order to get both the expense and revenue columns as headers with the date, I would recommend applying both the UNPIVOT and the PIVOT functions.
The UNPIVOT will convert the expense and revenue columns into rows that you can append the date to. Once the date is added to the column names, then you can apply the PIVOT function.
The UNPIVOT code will be:
select id,
col+'_'+convert(varchar(10), date, 110) new_col,
value
from yt
unpivot
(
value
for col in (expense, revenue)
) un
See SQL Fiddle with Demo. This produces a result:
| ID | NEW_COL | VALUE |
-----------------------------------
| 1 | expense_12-31-2012 | 43 |
| 1 | revenue_12-31-2012 | 45 |
| 2 | expense_01-01-2013 | 32 |
As you can see the expense/revenue columns are now rows with a new_col that has been created by concatenating the date to the end. This new_col is then used in the PIVOT:
select id,
[expense_12-31-2012], [revenue_12-31-2012],
[expense_01-01-2013], [revenue_01-01-2013],
[expense_01-31-2013], [revenue_01-31-2013],
[expense_03-03-2013], [revenue_03-03-2013]
from
(
select id,
col+'_'+convert(varchar(10), date, 110) new_col,
value
from yt
unpivot
(
value
for col in (expense, revenue)
) un
) src
pivot
(
sum(value)
for new_col in ([expense_12-31-2012], [revenue_12-31-2012],
[expense_01-01-2013], [revenue_01-01-2013],
[expense_01-31-2013], [revenue_01-31-2013],
[expense_03-03-2013], [revenue_03-03-2013])
) piv;
See SQL Fiddle with Demo.
The above version will work great if you have a known number of dates to turn into columns but if you have an unknown number of dates, then you will want to use dynamic SQL to generate the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(c.col+'_'+convert(varchar(10), yt.date, 110))
from yt
cross apply
(
select 'expense' col union all
select 'revenue'
) c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT id,' + #cols + '
from
(
select id,
col+''_''+convert(varchar(10), date, 110) new_col,
value
from yt
unpivot
(
value
for col in (expense, revenue)
) un
) src
pivot
(
sum(value)
for new_col in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo. Both queries generate the same result.

SQL, multiple rows to one column

So I have the following date
ID NAME MONTH COUNT
1 David December2012 500
2 Rob December2012 320
1 David January2013 400
2 Rob January2013 280
I am trying to make this.......
ID Name December2012 January2013
1 David 500 400
2 Rob 320 280
Where I get confused is how I want to keep two of the columns and just pivot the two other fields. Anyone know how I would do this.
Thank you so much for your help/time. I have never posted one of these, and responses are greatly appreciated!
You did not specify what RDBMS you are using. You can pivot the data in all databases using an aggregate function with a CASE expression:
select id, name,
sum(case when month = 'December2012' then "count" end) December2012,
sum(case when month = 'January2013' then "count" end) January2013
from yourtable
group by id, name
See SQL Fiddle with Demo
If you are using SQL Server 2005+ or Oracle 11g then you can use the PIVOT function:
select *
from
(
select id, name, month, [count]
from yourtable
) src
pivot
(
sum([count])
for month in (December2012, January2013)
) piv
See SQL Fiddle with Demo.
In SQL Server, if the values of the month are unknown then you can use dynamic SQL similar to this:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(month)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT id, name,' + #cols + ' from
(
select id, name, month, [count]
from yourtable
) x
pivot
(
sum([count])
for month in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo
All versions yield the result:
| ID | NAME | DECEMBER2012 | JANUARY2013 |
-------------------------------------------
| 1 | David | 500 | 400 |
| 2 | Rob | 320 | 280 |
Since you didn't specify what RDBMS you are using, then you can do this:
SELECT
ID,
NAME,
MAX(CASE WHEN MONTH = 'December2012' THEN "COUNT" END) AS "December2012",
MAX(CASE WHEN MONTH = 'January2013' THEN "COUNT" END) AS "January2013"
FROM Tablename
GROUP BY ID, Name;

SQL sum by year report, looking for an elegant solution

I have a table with 3 columns: ItemCode, Quantity and DocDate.
I would like to create the following report in a more "elegant" way:
SELECT T0.ItemCode,
(SELECT SUM(QUANTITY) FROM MyTable T1 WHERE YEAR(T0.DocDate) = 2011 AND T0.ItemCode = T1.ItemCode) AS '2011',
(SELECT SUM(QUANTITY) FROM MyTable T1 WHERE YEAR(T0.DocDate) = 2012 AND T0.ItemCode = T1.ItemCode) AS '2012'
FROM MyTable T0
GROUP BY T0.ItemCode, YEAR(T0.DocDate)
I'm pretty sure there's a better, more efficient way to write this but I can't come up with the right syntax. Any ideas?
You can try this:
SELECT T0.ItemCode,
SUM(CASE WHEN YEAR(T0.DocDate) = 2011 THEN QUANTITY ELSE 0 END) AS '2011',
SUM(CASE WHEN YEAR(T0.DocDate) = 2012 THEN QUANTITY ELSE 0 END) AS '2012'
FROM MyTable T0
GROUP BY
T0.ItemCode
This type of data transformation is known as a PIVOT. There are several ways that you can perform this operation. You can use the PIVOT function or you can use an aggregate function with a CASE statement:
Static Pivot Version: This is where you hard-code all of the values into the query
select ItemCode, [2011], [2012]
from
(
SELECT ItemCode,
QUANTITY,
YEAR(DocDate) Year
FROM MyTable
) src
pivot
(
sum(quantity)
for year in ([2011], [2012])
) piv
See SQL Fiddle with Demo
Case with Aggregate:
SELECT ItemCode,
SUM(CASE WHEN YEAR(DocDate) = 2011 THEN QUANTITY ELSE 0 END) AS '2011',
SUM(CASE WHEN YEAR(DocDate) = 2012 THEN QUANTITY ELSE 0 END) AS '2012'
FROM MyTable
GROUP BY ItemCode;
See SQL Fiddle with Demo
Dynamic Pivot: The previous two versions will work great is you have a known number of year values to transform, but it you have an unknown number then you can use dynamic sql:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(YEAR(DocDate))
from mytable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT itemcode, ' + #cols + ' from
(
select itemcode, quantity, year(docdate) year
from mytable
) x
pivot
(
sum(quantity)
for year in (' + #cols + ')
) p '
execute(#query)
See SQL Fiddle with Demo
All three versions will produce the same result:
| ITEMCODE | 2011 | 2012 |
--------------------------
| 1 | 200 | 45 |
| 2 | 89 | 0 |
| 3 | 0 | 7 |