How do I select sum with other table if I have data like below:
Table Member
MemberID Name DateJoin
M0001 John 01/01/2015
M0002 Willy 03/20/2016
M0003 Teddy 02/01/2017
etc....
Table Transaction
MemberID TransDate Total
M0002 02/01/2015 100000
M0002 02/28/2015 222000
M0001 01/01/2016 150000
M0001 01/26/2017 160000
M0002 01/25/2017 160000
M0003 02/01/2017 9000
I want the result as a sum of how many times the member transaction in shop in years 2015-2017
The result I want it's:
MemberID 2015 2016 2017
M0001 0 1 1
M0002 2 0 1
M0003 0 0 1
How many members will appear in Result although don't have transaction too.
try dynamic sql .
--load in #temp table
select MemberID , datepart (yyyy ,TransDate ) as TransDate ,COUNT(*)as cnt into #temp from [Transaction]
group by MemberID , datepart (yyyy ,TransDate )
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(c.TransDate)
FROM #temp c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT MemberID, ' + #cols + ' from
(
select MemberID
, cnt
, TransDate
from #temp
) x
pivot
(
max(cnt)
for TransDate in (' + #cols + ')
) p '
execute(#query)
drop #temp -- cleanup of #temp table
CREATE TABLE #Table1
([MemberID] varchar(5), [Name] varchar(5), [DateJoin] datetime)
;
INSERT INTO #Table1
([MemberID], [Name], [DateJoin])
VALUES
('M0001', 'John', '2015-01-01 00:00:00'),
('M0002', 'Willy', '2016-03-20 00:00:00'),
('M0003', 'Teddy', '2017-02-01 00:00:00')
;
CREATE TABLE #Table2
([MemberID] varchar(5), [TransDate] datetime, [Total] int)
;
INSERT INTO #Table2
([MemberID], [TransDate], [Total])
VALUES
('M0002', '2015-02-01 00:00:00', 100000),
('M0002', '2015-02-28 00:00:00', 222000),
('M0001', '2016-01-01 00:00:00', 150000),
('M0001', '2017-01-26 00:00:00', 160000),
('M0002', '2017-01-25 00:00:00', 160000),
('M0003', '2017-02-01 00:00:00', 9000)
;
select MemberID,[2015], [2016], [2017]
from
(
select a.MemberID,a.name,a.DateJoin,year(b.TransDate)[year],b.Total from #Table1 A join
#Table2 B on a.MemberID=b.MemberID
) src
pivot
(
count(total)
for year in ([2015], [2016], [2017])
) piv;
output
MemberID 2015 2016 2017
M0001 0 1 1
M0002 2 0 1
M0003 0 0 1
IN 2000
SELECT MEMBERID, COUNT(CASE WHEN YEAR=2015 THEN YEAR END ) AS [2015],
COUNT(CASE WHEN YEAR=2016 THEN YEAR END ) AS [2016],
COUNT(CASE WHEN YEAR=2017 THEN YEAR END ) AS [2017]
FROM (
SELECT A.MEMBERID,A.NAME,A.DATEJOIN,YEAR(B.TRANSDATE)[YEAR],B.TOTAL FROM #TABLE1 A JOIN
#TABLE2 B ON A.MEMBERID=B.MEMBERID)A
GROUP BY MEMBERID
It seems there is no information you need from table member. So select from table transaction alone and count conditionally.
select
memberid,
count(case when year(transdate) = 2015 then 1 end) as [2015],
count(case when year(transdate) = 2016 then 1 end) as [2016],
count(case when year(transdate) = 2017 then 1 end) as [2017]
from transaction
group by memberid
order by memberid;
If you want to include members that don't have any transaction, then you do need a join (an outer join that is):
select
m.memberid,
count(case when year(t.transdate) = 2015 then 1 end) as [2015],
count(case when year(t.transdate) = 2016 then 1 end) as [2016],
count(case when year(t.transdate) = 2017 then 1 end) as [2017]
from member m
left join transaction t on t.memberid = m.memberid
group by m.memberid
order by m.memberid;
Related
Having this data:
Name
Date
John
2021-03-01 10:00
Paul
2021-03-01 11:00
Paul
2021-03-01 14:20
John
2021-03-01 15:00
Paul
2021-03-01 17:00
How can I obtain this result (Dates ordered ASC)
Name
Date1
Date2
Date2
John
2021-03-01 10:00
2021-03-01 15:00
NULL
Paul
2021-03-01 11:00
2021-03-01 14:20
2021-03-01 17:00
Thank you.
If you want to make your query dynamic that means no matter how many dates you have for any given name this query will generate that number of columns automatically try below query:
Schema:
create table mytable (Name varchar(50),[Date] Datetime);
insert into mytable values('John' , '2021-03-01 10:00');
insert into mytable values('Paul' , '2021-03-01 11:00');
insert into mytable values('Paul' , '2021-03-01 14:20');
insert into mytable values('John' , '2021-03-01 15:00');
insert into mytable values('Paul' , '2021-03-01 17:00');
Query:
declare #cols as varchar(max), #colsForSelect as varchar(max), #query as varchar(max);
select #colsForSelect=string_agg(concat(quotename(rn),' ', datename),',' )from(
select distinct concat('Date',rn) datename,rn from
(SELECT row_number()over(partition by name order by [date])rn from mytable)t)a
select #cols =string_agg(quotename(rn),',') from (
select distinct rn from
(SELECT row_number()over(partition by name order by [date])rn from mytable)t)a
set #query = 'Select Name, ' + #colsForSelect + ' from
(
SELECT *,row_number()over(partition by name order by [date])rn
from mytable
) x
pivot
(
max([date])
for rn in (' + #cols + ')
) p
group by Name,' + #cols
execute(#query);
Output:
Name
Date1
Date2
Date3
John
2021-03-01 10:00:00.000
2021-03-01 15:00:00.000
null
Paul
2021-03-01 11:00:00.000
2021-03-01 14:20:00.000
2021-03-01 17:00:00.000
db<>fiddle here
Based on Larnu's help, This worked:
WITH RNs AS(
SELECT [Name],
[DateTime],
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY (SELECT NULL)) AS RN
FROM dbo.Punch
WHERE Date = '2016-04-18'
)
SELECT Name,
MAX(CASE RN WHEN 1 THEN [DateTime] END) AS Result1,
MAX(CASE RN WHEN 2 THEN [DateTime] END) AS Result2,
MAX(CASE RN WHEN 3 THEN [DateTime] END) AS Result3,
MAX(CASE RN WHEN 4 THEN [DateTime] END) AS Result4
FROM RNs R
GROUP BY Name
I have tried with Stuff function instead of sting_agg which was introduced in 2017 server. If you are using below 2017 version you can use the below query.
declare #column_name varchar(5000)
declare #col_name varchar(5000)
set #column_name = (select stuff((select ','+'['+cast(rn as varchar(1000))+']' from(select distinct row_number()over(partition by name order by (select null))as rn from mytable)a
for xml path('')), 1,1,''))
set #col_name = (select stuff((select ','+'['+cast(rn as varchar(1000))+']' +' Date'+cast(rn as varchar(1000)) from(select distinct row_number()over(partition by name order by (select null))as rn from mytable)a
for xml path('')), 1,1,''))
exec('select name, '+#col_name +'
from (
select row_number()over(partition by name order by (select null))rn, year([date]) yr, *
from mytable
)a
pivot
(
max([date]) for [rn] in ('+#column_name+' )
)pv')
I have a table like this
Date County Location
2020-01-01 abc west
2020-01-02 abc north
2020-02-01 xzy west
2020-02-02 xzy east
2020-02-03 xyz east
Can we group and count so it can become
County jan feb
abc 2
xyz 3
Location jan feb
west 1
north 1
west 1
east 2
Thank you
Try this as the base query and then write a pivot query based on this query result as shown in the demo link.
For your reference FROM - Using PIVOT and UNPIVOT.
Select country
, FORMAT([date], 'MMMM') as Month
, count(*) as Tot
from YourTable
group by country, FORMAT([date], 'MMMM')
Pivot query needed an aggregate function. Here is the complete query.
create table YourTable
([Date] Date
, Country varchar(20)
, Location varchar(20))
insert into YourTable values
('2020-01-01', 'abc', 'west'),
('2020-01-02', 'abc', 'north'),
('2020-02-01', 'xzy', 'west'),
('2020-02-02', 'xzy', 'east'),
('2020-02-03', 'xyz', 'east')
Select * into #temp from(
Select country
, FORMAT([date], 'MMMM') as Month
, count(*) as Tot
from YourTable
group by country, FORMAT([date], 'MMMM')
)a
--Select * from #temp
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(c.Month)
FROM #temp c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT country, ' + #cols + ' from
(
select Country
, Month
, Tot
from #temp
) x
pivot
(
max(Tot)
for Month in (' + #cols + ')
) p '
execute(#query)
Live db<>fiddle demo.
Use conditional aggregation along with grouping sets:
select county, location,
sum(case when date >= '2020-01-01' and date < '2020-02-01' then 1 end) as jan,
sum(case when date >= '2020-02-01' and date < '2020-03-01' then 1 end) as feb
from t
group by grouping sets ( (country), (location) );
I have table of products and their sales quantity in months.
Product Month Qty
A 2018-01-01 5
A 2018-02-01 3
A 2018-05-01 5
B 2018-08-01 10
B 2018-10-01 12
...
I'd like to first fill in the data gap between each product's min and max dates like below:
Product Month Qty
A 2018-01-01 5
A 2018-02-01 3
A 2018-03-01 0
A 2018-04-01 0
A 2018-05-01 5
B 2018-08-01 10
B 2018-09-01 0
B 2018-10-01 12
...
Then I would need to perform an accumulation of each product's sales quantity by month.
Product Month total_Qty
A 2018-01-01 5
A 2018-02-01 8
A 2018-03-01 8
A 2018-04-01 8
A 2018-05-01 13
B 2018-08-01 10
B 2018-09-01 10
B 2018-10-01 22
...
I fumbled over the "cross join" clause, however it seems to generate some unexpected results for me. Could someone help to give a hint how I can achieve this in SQL?
Thanks a lot in advance.
I think a recursive CTE is a simple way to do this. The code is just:
with cte as (
select product, min(mon) as mon, max(mon) as end_mon
from t
group by product
union all
select product, dateadd(month, 1, mon), end_mon
from cte
where mon < end_mon
)
select cte.product, cte.mon, coalesce(qty, 0) as qty
from cte left join
t
on t.product = cte.product and t.mon = cte.mon;
Here is a db<>fiddle.
Hi i think this example can help you and perform what you excepted :
CREATE TABLE #MyTable
(Product varchar(10),
ProductMonth DATETIME,
Qty int
);
GO
CREATE TABLE #MyTableTempDate
(
FullMonth DATETIME
);
GO
INSERT INTO #MyTable
SELECT 'A', '2019-01-01', 214
UNION
SELECT 'A', '2019-02-01', 4
UNION
SELECT 'A', '2019-03-01', 50
UNION
SELECT 'B', '2019-01-01', 214
UNION
SELECT 'B', '2019-02-01', 10
UNION
SELECT 'C', '2019-04-01', 150
INSERT INTO #MyTableTempDate
SELECT '2019-01-01'
UNION
SELECT '2019-02-01'
UNION
SELECT '2019-03-01'
UNION
SELECT '2019-04-01'
UNION
SELECT '2019-05-01'
UNION
SELECT '2019-06-01'
UNION
SELECT '2019-07-01';
------------- FOR NEWER SQL SERVER VERSION > 2005
WITH MyCTE AS
(
SELECT T.Product, T.ProductMonth AS 'MMonth', T.Qty
FROM #MyTable T
UNION
SELECT T.Product, TD.FullMonth AS 'MMonth', 0 AS 'Qty'
FROM #MyTable T, #MyTableTempDate TD
WHERE NOT EXISTS (SELECT 1 FROM #MyTable TT WHERE TT.Product = T.Product AND TD.FullMonth = TT.ProductMonth)
)
-- SELECT * FROM MyCTE;
SELECT Product, MMonth, Qty, SUM( Qty) OVER(PARTITION BY Product ORDER BY Product
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as 'TotalQty'
FROM MyCTE
ORDER BY Product, MMonth ASC;
DROP TABLE #MyTable
DROP TABLE #MyTableTempDate
I have other way to perform this in lower SQL Server Version (like 2005 and lower)
It's a SELECT on SELECT if it's your case let me know and i provide some other example.
You can create the months with a recursive CTE
DECLARE #MyTable TABLE
(
ProductID CHAR(1),
Date DATE,
Amount INT
)
INSERT INTO #MyTable
VALUES
('A','2018-01-01', 5),
('A','2018-02-01', 3),
('A','2018-05-01', 5),
('B','2018-08-01', 10),
('B','2018-10-01', 12)
DECLARE #StartDate DATE
DECLARE #EndDate DATE
SELECT #StartDate = MIN(Date), #EndDate = MAX(Date) FROM #MyTable
;WITH dates AS (
SELECT #StartDate AS Date
UNION ALL
SELECT DATEADD(Month, 1, Date)
FROM dates
WHERE Date < #EndDate
)
SELECT A.ProductID, d.Date, COALESCE(Amount,0) AS Amount, COALESCE(SUM(Amount) OVER(PARTITION BY A.ProductID ORDER BY A.ProductID, d.Date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),0) AS Total
FROM
(
SELECT ProductID, MIN(date) as DateStart, MAX(date) as DateEnd
FROM #MyTable
GROUP BY ProductID -- As I read in your comments that you need different min and max dates per product
) A
JOIN dates d ON d.Date >= A.DateStart AND d.Date <= A.DateEnd
LEFT JOIN #MyTable T ON A.ProductID = T.ProductID AND T.Date = d.Date
ORDER BY A.ProductID, d.Date
Try this below
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
;WITH CTE(Product,[Month],Qty)
AS
(
SELECT 'A','2018-01-01', 5 UNION ALL
SELECT 'A','2018-02-01', 3 UNION ALL
SELECT 'A','2018-05-01', 5 UNION ALL
SELECT 'B','2018-08-01', 10 UNION ALL
SELECT 'D','2018-10-01', 12
)
SELECT ct.Product,[MonthDays],ct.Qty
INTO #Temp
FROM
(
SELECT c.Product,[Month],
ISNULL(Qty,0) AS Qty
FROM CTE c
)ct
RIGHT JOIN
(
SELECT -- This code is to get month data
CONVERT(VARCHAR(10),'2018-'+ RIGHT('00'+CAST(MONTH(DATEADD(MM, s.number, CONVERT(DATETIME, 0)))AS VARCHAR),2) +'-01',120) AS [MonthDays]
FROM master.dbo.spt_values s
WHERE [type] = 'P' AND s.number BETWEEN 0 AND 11
)DT
ON dt.[MonthDays] = ct.[Month]
SELECT
MAX(Product)OVER(ORDER BY [MonthDays])AS Product,
[MonthDays],
ISNULL(Qty,0) Qty,
SUM(ISNULL(Qty,0))OVER(ORDER BY [MonthDays]) As SumQty
FROM #Temp
Result
Product MonthDays Qty SumQty
------------------------------
A 2018-01-01 5 5
A 2018-02-01 3 8
A 2018-03-01 0 8
A 2018-04-01 0 8
A 2018-05-01 5 13
A 2018-06-01 0 13
A 2018-07-01 0 13
B 2018-08-01 10 23
B 2018-09-01 0 23
D 2018-10-01 12 35
D 2018-11-01 0 35
D 2018-12-01 0 35
First of all, i would divide month and year to get easier with statistics.
I will give you an example query, not based on your table but still helpful.
--here i create the table that will be used as calendar
Create Table MA_MonthYears (
Month int not null ,
year int not null
PRIMARY KEY ( month, year) )
--/////////////////
-- here i'm creating a procedure to fill the ma_monthyears table
declare #month as int
declare #year as int
set #month = 1
set #year = 2015
while ( #year != 2099 )
begin
insert into MA_MonthYears(Month, year)
select #month, #year
if #month < 12
set #month=#month+1
else
set #month=1
if #month = 1
set #year = #year + 1
end
--/////////////////
--here you are the possible result you are looking for
select SUM(Ma_saledocdetail.taxableamount) as Sold, MA_MonthYears.month , MA_MonthYears.year , item
from MA_MonthYears left outer join MA_SaleDocDetail on year(MA_SaleDocDetail.DocumentDate) = MA_MonthYears.year
and Month(ma_saledocdetail.documentdate) = MA_MonthYears.Month
group by MA_SaleDocDetail.Item, MA_MonthYears.year , MA_MonthYears.month
order by MA_MonthYears.year , MA_MonthYears.month
Here is my data
date amount
2017-07-10 15.00
2017-07-10 15.00
2017-07-28 25.00
2017-08-01 100.00
2017-08-12 15.00
2017-08-29 200.00
2017-09-18 105.00
2017-09-21 200.00
2017-09-23 25.00
2017-10-12 15.00
2017-10-14 500.00
2017-11-01 200.00
2017-11-02 200.00
I want to add it by month so what we will get that in June i got a total of 55, August i will get 315, September 330, October 515, November 400 and the past dates with no amount will be 0 how will i do that?
Here is my temporary table codes:
create table #TempTable
(month varchar(50),
amount decimal(18,2))
insert into #TempTable (month)
SELECT TOP 12
DATENAME(MONTH, DATEADD(MONTH,ROW_NUMBER() OVER (ORDER BY object_id) - 1,0))
FROM sys.columns
create table #Data
(date date,
amount decimal(18,2))
insert into #Data(date,amount) values('2017-07-10',15.00)
insert into #Data(date,amount) values('2017-07-10',15.00)
insert into #Data(date,amount) values('2017-07-28',25.00)
insert into #Data(date,amount) values('2017-08-01',100.00)
insert into #Data(date,amount) values('2017-08-12',15.00)
insert into #Data(date,amount) values('2017-08-29',200.00)
insert into #Data(date,amount) values('2017-09-18',105.00)
insert into #Data(date,amount) values('2017-09-21',200.00)
insert into #Data(date,amount) values('2017-09-23',25.00)
insert into #Data(date,amount) values('2017-10-12',15.00)
insert into #Data(date,amount) values('2017-10-14',500.00)
insert into #Data(date,amount) values('2017-11-01',200.00)
insert into #Data(date,amount) values('2017-11-02',200.00)
select * from #Data
select * from #TempTable
drop table #TempTable
drop table #Data
PS. Just update the #TempTable and put the total on it thank you :)
Use the below code:
SELECT Res1.[Month]
,ISNULL(Res2.Amount,0)
FROM #TempTable Res1
LEFT JOIN
(
select DATENAME(MONTH,Res1.date) AS [Month]
,SUM(amount) AS Amount
from #Data Res1
GROUP BY DATENAME(MONTH,Res1.date)
)Res2 ON Res2.[Month] = Res1.[Month]
SELECT a.Month,SUM(ISNULL(b.Amount,0))
FROM #TempTable a
LEFT JOIN #Data b
ON a.Month = DATENAME(MONTH,date)
GROUP BY a.Month
for update
UPDATE a
SET Amount = ISNULL(GroupSuma,0)
FROM #TempTable a
LEFT JOIN
(
select DATENAME(MONTH,date) as month, SUM(b.Amount) as GroupSuma
FROM #Data b
GROUP BY DATENAME(MONTH,date)
) as c
ON a.Month = c.Month
update temptable from data table
update #TempTable
set amount = d.amount from ( select datename(month, date) date
, SUM(amount) amount from #Data
group by datename(month, date)
) d inner join #TempTable t on t.month = d.date
select * from #TempTable
One of your requirements is:
past dates with no amount will be 0
Your dataset didn't include this so I've added two additional rows in the example code below. I also added a different year to illustrate what happens with different years. I UNION ALL the values from #data with a CTE that select years, every month# of year, and 0. When SUM'ed, most of the 0 entries are eliminated, but months in the year without values in #Data remain with amount = 0. This result is subqueried to eliminate the 0 rows out of range of the min and max dates in #data.
create table #Data (date date, amount decimal(18,2))
insert into #Data(date,amount) values('2017-07-10',15.00)
insert into #Data(date,amount) values('2017-07-10',15.00)
insert into #Data(date,amount) values('2017-07-28',25.00)
insert into #Data(date,amount) values('2017-08-01',100.00)
insert into #Data(date,amount) values('2017-08-12',15.00)
insert into #Data(date,amount) values('2017-08-29',200.00)
insert into #Data(date,amount) values('2017-09-18',105.00)
insert into #Data(date,amount) values('2017-09-21',200.00)
insert into #Data(date,amount) values('2017-09-23',25.00)
insert into #Data(date,amount) values('2017-10-12',15.00)
insert into #Data(date,amount) values('2017-10-14',500.00)
insert into #Data(date,amount) values('2017-11-01',200.00)
insert into #Data(date,amount) values('2017-11-02',200.00)
--additional test values
insert into #Data(date,amount) values('2017-05-04',5.00)
insert into #Data(date,amount) values('2018-02-22',1.00)
DECLARE #minYear int = (SELECT YEAR(MIN(date)) FROM #data)
,#minMonth int = (SELECT MONTH(MIN(date)) FROM #data)
,#maxYear int = (SELECT MAX(YEAR(date)) FROM #data)
,#maxMonth int = (SELECT MONTH(MAX(date)) FROM #data);
WITH cteYear AS
(
SELECT #minYear AS num
UNION ALL
SELECT num + 1 FROM cteYear WHERE num + 1 <= #maxYear
)
SELECT dT.dateyear
,DateName( month , DateAdd( month , dT.datemonth, -1 ) ) AS [month]
,SUM(dT.amount) AS [Sum Amount]
FROM (
SELECT YEAR(date) AS [dateyear], MONTH(date) AS [datemonth], amount -- AS [SumAmount]
FROM #Data D
UNION ALL
SELECT num, monthlist.month, 0
FROM cteYear CROSS JOIN (SELECT 1 AS [month] UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) AS monthlist
) AS dT
WHERE (#minYear <> #maxYear AND dateyear = #minYear AND datemonth >= #minMonth)
OR
(#minYear <> #maxYear AND dateyear = #maxYear AND datemonth <= #maxMonth)
OR
(#minYear <> #maxYear AND dateyear <> #minYear AND dateyear <> #maxYear)
OR
(#minYear = #maxYear AND datemonth >= #minMonth AND datemonth <= #maxMonth)
GROUP BY dateyear, datemonth
ORDER BY dateyear, datemonth
Gives output:
dateyear month Sum Amount
2017 May 5.00
2017 June 0.00
2017 July 55.00
2017 August 315.00
2017 September 330.00
2017 October 515.00
2017 November 400.00
2017 December 0.00
2018 January 0.00
2018 February 1.00
How to get the data based on the given below format:
Id name year month amount
1 A 2012 jan 100
1 A 2012 jan 900
1 A 2012 jan 300
1 A 2012 apr 100
1 A 2012 apr 500
2 B 2013 may 100
Output would be in the below mentioned form, if name, year, and month in parameter,
Id name Jan feb mar Apr may jun ...... jan .....may total
1 A 1300 0 0 600 0 0 ..... 0 ...... 0 1900
2 B 0 0 0 0 0 0.........0.......100 100
declare #t table (Id INT,name VARCHAR(10),years VARCHAR(10),months VARCHAR(10),amt INT )
insert into #t (Id,name,years,months,amt)values (1,'A','2012','jan',100)
insert into #t (Id,name,years,months,amt)values (2,'A','2012','jan',100)
insert into #t (Id,name,years,months,amt)values (3,'A','2012','apr',200)
insert into #t (Id,name,years,months,amt)values (4,'A','2012','apr',100)
insert into #t (Id,name,years,months,amt)values (5,'B','2013','may',200)
Select id,
name,
ISNULL(jan,0) As Jan,
ISNULL(feb,0) As FEb,
ISNULL(mar,0) As Mar,
ISNULL(apr,0)As Apr,ISNULL(JUn,0)As Jun,ISNULL(jul,0)As jul,ISNULL(aug,0)As aug
from
(Select distinct t.ID,t.name,t.years,t.months As Months,t.amt
from #t t)t
PIVOT (SUM(amt)FOR Months IN( [jan],
[feb],
[mar],
[apr],[JUn],[jul],[aug]))p
You need to use PIVOT to get result like you mentioned
Here is one example to use PIVOT
and your sql syntex like
SELECT * FROM (SELECT t.id,t.name,t.month,t.amount,(SELECT SUM(t2.amount) FROM dbo.test AS t2 GROUP BY t2.name HAVING t2.name= t.name ) AS total FROM dbo.test AS t) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pivots
Example
I think this might need a dynamic pivot?
CREATE TABLE #Data (
Id INT,
name VARCHAR(1),
[year] INT,
[month] VARCHAR(3),
amount INT);
INSERT INTO #Data VALUES (1, 'A', 2012, 'jan', 100);
INSERT INTO #Data VALUES (1, 'A', 2012, 'jan', 900);
INSERT INTO #Data VALUES (1, 'A', 2012, 'jan', 300);
INSERT INTO #Data VALUES (1, 'A', 2012, 'apr', 100);
INSERT INTO #Data VALUES (1, 'A', 2012, 'apr', 500);
INSERT INTO #Data VALUES (2, 'B', 2013, 'may', 100);
DECLARE #cols VARCHAR(1024);
SELECT
#Cols = STUFF((
SELECT DISTINCT
',' + QUOTENAME(CONVERT(VARCHAR(4), [year]) + '/' + [month])
FROM
#Data
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1 ,1 ,'');
DECLARE #Query VARCHAR(MAX);
SELECT #Query = '
WITH Aggregated AS (
SELECT
Id,
name,
CONVERT(VARCHAR(4), [year]) + ''/'' + [month] AS YearMonth,
SUM(amount) AS amount
FROM
#Data
GROUP BY
Id,
name,
[year],
[month])
SELECT
*
FROM
Aggregated
PIVOT (
SUM(amount)
FOR YearMonth IN (' + #cols + ')
) p;';
EXEC (#Query);
Results look like this:
Id name 2012/apr 2012/jan 2013/may
1 A 600 1300 NULL
2 B NULL NULL 100