SUMIFS (Sum if with multiple conditions) with SQL Server - sql

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

Related

SQL Server - Convert columns to rows

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;

Need to select table data into distinct columns based on a date in a row

I am not sure what I need here, looks sort of like I could use a pivot but I don't think it's that complicated and would like to avoid pivot if I can as I haven't used it much (er, at all).
I have data like this:
ID score notes CreateDate
1661 9.2 8.0 on Sept 2010 7/22/2010
1661 7.6 11/4/2010
1661 7.9 6/10/2011
1661 8.3 9/28/2011
1661 7.9 1/20/2012
I want to organize all that data on to one row with the oldest date being first and then use the next oldest date, then next oldest...until I use 4 or 5 dates. So the end result would look something like this:
ID score1 notes1 date1 score2 notes2 date2 score3 notes3 date3 score4 notes4 date4
1661 9.2 8.0 on Sept 2010 7/22/2010 7.6 blah 11/4/2010 7.9 blah2 6/10/2011 8.3 blah3 9/28/2011
PIVOT would be tricky in this situation, since you have more than one column per test (PIVOT works well if you only wanted to show Score1, Score2, Score3, etc). Fortunately, you can create a simple (if long-winded) solution with CASE statements:
select
ID,
max(case when RowNum = 1 then Score else null end) as Score1,
max(case when RowNum = 1 then Notes else null end) as Notes1,
max(case when RowNum = 1 then CreateDate else null end) as Date1,
max(case when RowNum = 2 then Score else null end) as Score2,
max(case when RowNum = 2 then Notes else null end) as Notes2,
max(case when RowNum = 2 then CreateDate else null end) as Date2,
max(case when RowNum = 3 then Score else null end) as Score3,
max(case when RowNum = 3 then Notes else null end) as Notes3,
max(case when RowNum = 3 then CreateDate else null end) as Date3,
max(case when RowNum = 4 then Score else null end) as Score4,
max(case when RowNum = 4 then Notes else null end) as Notes4,
max(case when RowNum = 4 then CreateDate else null end) as Date4,
max(case when RowNum = 5 then Score else null end) as Score5,
max(case when RowNum = 5 then Notes else null end) as Notes5,
max(case when RowNum = 5 then CreateDate else null end) as Date5
from
(
select
*, row_number() over (partition by ID order by CreateDate) as RowNum
from
mytable
) tt
group by
ID
This is hard-coded to cover 5 tests. It will be OK with less, but won't display a 6th. You can obviously create more CASE statements to handle more tests.
Just because I love pivots, I will show you how this can be done using the PIVOT function. In order to get the result with the PIVOT function you will first want to UNPIVOT your multiple columns score, notes and createdate. The unpivot process will convert the multiple columns into multiple rows.
Since you are using SQL Server 2008 you can use CROSS APPLY to unpivot your data, the first part of the query will be similar to:
;with cte as
(
select id, score, notes, createdate,
row_number() over(partition by id order by createdate) seq
from yourtable
)
select id, col, value
from
(
select t.id,
col = col + cast(seq as varchar(10)),
value
from cte t
cross apply
(
values
('score', cast(score as varchar(10))),
('notes', notes),
('date', convert(varchar(10), createdate, 120))
) c (col, value)
) d;
See SQL Fiddle with Demo. Doing this gets your data in the format:
| ID | COL | VALUE |
| 1661 | score1 | 9.20 |
| 1661 | notes1 | 8.0 on Sept 2010 |
| 1661 | date1 | 2010-07-22 |
| 1661 | score2 | 7.60 |
| 1661 | notes2 | (null) |
| 1661 | date2 | 2010-11-04 |
| 1661 | score3 | 7.90 |
Now you can apply the PIVOT function:
;with cte as
(
select id, score, notes, createdate,
row_number() over(partition by id order by createdate) seq
from yourtable
)
select id, col, value
from
(
select t.id,
col = col + cast(seq as varchar(10)),
value
from cte t
cross apply
(
values
('score', cast(score as varchar(10))),
('notes', notes),
('date', convert(varchar(10), createdate, 120))
) c (col, value)
) d
pivot
(
max(value)
for col in (score1, notes1, date1, score2, notes2, date2,
score3, notes3, date3, score4, notes4, date4,
score5, notes5, date5)
) piv;
See SQL Fiddle with Demo.
Then if you were going to have an unknown number of values for each id, you could implement dynamic SQL to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(col + cast(seq as varchar(10)))
from
(
select row_number() over(partition by id order by createdate) seq
from yourtable
) d
cross apply
(
select 'score', 1 union all
select 'notes', 2 union all
select 'date', 3
) c (col, so)
group by seq, col, so
order by seq, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT id, ' + #cols + '
from
(
select t.id,
col = col + cast(seq as varchar(10)),
value
from
(
select id, score, notes, createdate,
row_number() over(partition by id order by createdate) seq
from yourtable
) t
cross apply
(
values
(''score'', cast(score as varchar(10))),
(''notes'', notes),
(''date'', convert(varchar(10), createdate, 120))
) c (col, value)
) x
pivot
(
max(value)
for col in (' + #cols + ')
) p '
execute sp_executesql #query;
See SQL Fiddle with Demo. Both versions give the result:
| ID | SCORE1 | NOTES1 | DATE1 | SCORE2 | NOTES2 | DATE2 | SCORE3 | NOTES3 | DATE3 | SCORE4 | NOTES4 | DATE4 | SCORE5 | NOTES5 | DATE5 |
| 1661 | 9.20 | 8.0 on Sept 2010 | 2010-07-22 | 7.60 | (null) | 2010-11-04 | 7.90 | (null) | 2011-06-10 | 8.30 | (null) | 2011-09-28 | 7.90 | (null) | 2012-01-20 |

SQL Server Rows to Multi-Columns

I have the following table and data:
CREATE TABLE SourceTbl ([Code] varchar(3), [Total] decimal, [Date] datetime );
INSERT INTO SourceTbl ([Code], [Total], [Date])
VALUES ('AA', 100, '2012-12-01'), ('AA', 200, '2013-02-01'), ('BB', 50, '2012-01-01');
A simple select will return
Code | Total | Date
'AA' | 100 | 2012-12-01
'AA' | 200 | 2013-02-01
'BB' | 50 | 2012-01-01
but what I need is the following
Code | Total | Date | Total | Date
'AA | 200 | 2013-02-01 | 100 | 2012-12-01
'BB | 50 | 2012-01-01 | null | null
I have been trying to do this using a PIVOT operator but without success (based on the question SQL Server Pivot multiple columns based on one column).
Using that example, all I get are two rows with null values.
The Total/Date columns can be repeated 13 times and they must be ordered by Date DESC.
SQL Fiddle: http://sqlfiddle.com/#!3/f37a1/2
Any help is appreciated!
Thanks!
If you need just two columns:
with cte as (
select *, row_number() over(partition by Code order by Date) as rn
from SourceTbl
)
select
code,
max(case when rn = 1 then Total end) as Total1,
max(case when rn = 1 then Date end) as Date1,
max(case when rn = 2 then Total end) as Total2,
max(case when rn = 2 then Date end) as Date2
from cte
group by code
=> sql fiddle demo
dynamic solution:
declare #stmt nvarchar(max)
;with cte as (
select distinct
cast(row_number() over(partition by Code order by Date) as nvarchar(max)) as rn
from SourceTbl
)
select #stmt = isnull(#stmt + ', ', '') +
'max(case when rn = ' + rn + ' then Total end) as Total' + rn + ',' +
'max(case when rn = ' + rn + ' then Date end) as Date' + rn
from cte
order by rn
select #stmt = '
with cte as (
select *, row_number() over(partition by Code order by Date) as rn
from SourceTbl
)
select
code, ' + #stmt + ' from cte group by code'
exec sp_executesql
#stmt = #stmt
=> sql fiddle demo
Are you trying to dynamically create columns in your result set?
If you had a third record of 'AA' with a total of 300 and the date of 03/01/2013 would you that mean you would want something like this displayed?
Code | Total | Date | Total | Date | Total | Date
AA | 200 | 2013-02-01 | 100 | 2012-12-01| 300 | 03-01-13
BB | 50 | 2012-01-01 | null | null | null | null

Display multiple rows and column values into a single row, multiple column values

I have to show multiple incomes, type of income and employer name values for a single individual in a single row. So, if 'A' has three different incomes from three different sources,
id | Name | Employer | IncomeType | Amount
123 | XYZ | ABC.Inc | EarningsformJob | $200.00
123 | XYZ | Self | Self Employment | $300.00
123 | XYZ. | ChildSupport| Support | $500.00
I need to show them as
id | Name | Employer1 | Incometype1| Amount1 | Employer2 | incometype2 | Amount2| Employer3 | Incometype3| Amount3.....
123 |XYZ | ABC.Inc |EarningsformJob | $200.00|Self | Self Employment | $300.00|ChildSupport| Support | $500.00.....
I need both 'fixed number of columns' (where we know how many times employer, incometype and amount colums are going to repeat)logic and 'dynamic display of columns' ( unknown number of times these columns are going to repeat)
Thanks.
Since you are using SQL Server there are several ways that you can transpose the rows of data into columns.
Aggregate Function / CASE: You can use an aggregate function with a CASE expression along with row_number(). This version would require that you have a known number of values to become columns:
select id,
name,
max(case when rn = 1 then employer end) employer1,
max(case when rn = 1 then IncomeType end) IncomeType1,
max(case when rn = 1 then Amount end) Amount1,
max(case when rn = 2 then employer end) employer2,
max(case when rn = 2 then IncomeType end) IncomeType2,
max(case when rn = 2 then Amount end) Amount2,
max(case when rn = 3 then employer end) employer3,
max(case when rn = 3 then IncomeType end) IncomeType3,
max(case when rn = 3 then Amount end) Amount3
from
(
select id, name, employer, incometype, amount,
row_number() over(partition by id order by employer) rn
from yourtable
) src
group by id, name;
See SQL Fiddle with Demo.
PIVOT/UNPIVOT: You could use the UNPIVOT and PIVOT functions to get the result. The UNPIVOT converts your multiple columns of Employer, IncomeType and Amount into multiples rows before applying the pivot. You did not specific what version of SQL Server, assuming you have a known number of values then you could use the following in SQL Server 2005+ which uses CROSS APPLY with UNION ALL to unpivot:
select id, name,
employer1, incometype1, amount1,
employer2, incometype2, amount2,
employer3, incometype3, amount3
from
(
select id, name, col+cast(rn as varchar(10)) col, value
from
(
select id, name, employer, incometype, amount,
row_number() over(partition by id order by employer) rn
from yourtable
) t
cross apply
(
select 'employer', employer union all
select 'incometype', incometype union all
select 'amount', cast(amount as varchar(50))
) c (col, value)
) src
pivot
(
max(value)
for col in (employer1, incometype1, amount1,
employer2, incometype2, amount2,
employer3, incometype3, amount3)
) piv;
See SQL Fiddle with Demo.
Dynamic Version: Lastly, if you have an unknown number of values then you will need to use dynamic SQL to generate the result.
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(col+cast(rn as varchar(10)))
from
(
select row_number() over(partition by id order by employer) rn
from yourtable
) d
cross apply
(
select 'employer', 1 union all
select 'incometype', 2 union all
select 'amount', 3
) c (col, so)
group by col, rn, so
order by rn, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT id, name,' + #cols + '
from
(
select id, name, col+cast(rn as varchar(10)) col, value
from
(
select id, name, employer, incometype, amount,
row_number() over(partition by id order by employer) rn
from yourtable
) t
cross apply
(
select ''employer'', employer union all
select ''incometype'', incometype union all
select ''amount'', cast(amount as varchar(50))
) c (col, value)
) x
pivot
(
max(value)
for col in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo. All versions give a result:
| ID | NAME | EMPLOYER1 | INCOMETYPE1 | AMOUNT1 | EMPLOYER2 | INCOMETYPE2 | AMOUNT2 | EMPLOYER3 | INCOMETYPE3 | AMOUNT3 |
-------------------------------------------------------------------------------------------------------------------------------------
| 123 | XYZ | ABC.Inc | EarningsformJob | 200 | ChildSupport | Support | 500 | Self | Self Employment | 300 |

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 |