我真的想问一下sql server中的数据透视表。我试图做一些查询结构是这样的。
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
DECLARE @tanggal_awal date
DECLARE @tanggal_akhir date
DECLARE @print nvarchar(MAX)
CREATE TABLE #datatable
(
product_id int,
product_date date,
product_ammount int
)
SET @tanggal_awal = convert(date,'02-01-2017')
SET @tanggal_akhir = convert(date,'02-27-2017')
insert into #datatable (product_id,product_date,product_ammount) VALUES
(1,getdate(),100),
(1,getdate(),900),
(2,dateadd(DD,-1,getdate()),400),
(3,DATEADD(DD,4,getdate()),300),
(1,dateadd(DD,4,getdate()),200),
(2,dateadd(DD,2,getdate()),700),
(4,dateadd(DD,-3,getdate()),1000)
--SELECT * FROM @datatable
;WITH CTE (datelist,maxdate) AS
(
select min(@tanggal_awal) datelist, max(product_date) maxdate
from #datatable
union all
select dateadd(dd, 1, datelist), @tanggal_akhir
from cte
where datelist < maxdate
) SELECT c.datelist
into #temp
from cte c
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), datelist, 120))
from #temp
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
--SELECT d.datelist, b.product_id, SUM(b.product_ammount)
-- FROM #temp d left join #datatable b on
-- d.datelist between @tanggal_awal and @tanggal_akhir
-- AND d.datelist = b.product_date
-- GROUP BY b.product_id, d.datelist
--select b.product_id, d.datelist,
-- sum(b.product_ammount) PivotDate
-- from #datatable b
-- left join #temp d
-- on d.datelist between @tanggal_awal and @tanggal_akhir
-- and d.datelist = b.product_date
--group by b.product_id, d.datelist
--order by b.product_id
SET @query = 'SELECT product_id, '+@cols+' FROM
(
select b.product_id, d.datelist, convert(CHAR(10), datelist, 120) PivotDate
from #datatable b
left join #temp d
on d.datelist between @tanggal_awal and @tanggal_akhir
and d.datelist = b.product_date
) x
pivot
(
count(datelist)
for PivotDate in (' +@cols+ ')
) p'
EXECUTE sp_executesql @query ,N'@tanggal_awal DATE, @tanggal_akhir DATE', @tanggal_awal,@tanggal_akhir
GO
drop table #temp
go
drop table #datatable
此查询的结果主要是&product;&lt; product_id&#39;在各自的日期。但是,我想要像这样得到基于product_ammount的总和。
-------------------------------------
|product_id| 2017-01-02 | 2017-02-02|
-------------------------------------
|1 | 0 | 1000 |
-------------------------------------
|2 | 900 | 0 |
-------------------------------------
|3 | 700 | 0 |
-------------------------------------
任何人都可以给我最好的方法来处理连接和聚合,特别是对于这种情况吗?非常感谢你。
- 更新 在Shakeer Mirza的查询之后,结果是这样的。
我们可以分组吗?
答案 0 :(得分:2)
请尝试以下方法。我认为您需要将您的Pivot更改为SUM
的{{1}}和qry product_ammount
以便选择<{p>}中的X
product_ammount
<强>更新强> 替换Null
SET @query = 'SELECT product_id, '+@cols+' FROM
(
select b.product_id, convert(CHAR(10), datelist, 120) PivotDate, product_ammount
from #datatable b
left join #temp d
on d.datelist between @tanggal_awal and @tanggal_akhir
and d.datelist = b.product_date
) x
pivot
(
sum(product_ammount)
for PivotDate in (' +@cols+ ')
) p'
EXECUTE sp_executesql @query ,N'@tanggal_awal DATE, @tanggal_akhir DATE', @tanggal_awal,@tanggal_akhir
GO
drop table #temp
go
drop table #datatable
在Select of yourquery中替换上面的@COLS_ISNULL。
DECLARE @COLS_ISNULL VARCHAR(MAX)='';
SELECT @COLS_ISNULL = @COLS_ISNULL +COLS_ISNULL+',' FROM (
SELECT distinct 'ISNULL('+QUOTENAME(convert(CHAR(10), datelist, 120))+',0) 'as COLS_ISNULL
from #temp)A
--to remove last comma.
SELECT @COLS_ISNULL = SUBSTRING (@COLS_ISNULL,1,LEN(@COLS_ISNULL)-1)
来自评论:
这是我SSMS的输出
答案 1 :(得分:1)
您的查询可以简化为:
SET @query =
'SELECT product_id, '+@cols+' FROM
(
select b.product_id, b.product_ammount,
convert(CHAR(10), product_date, 120) PivotDate
from #datatable b
where product_date between @tanggal_awal and @tanggal_akhir
) x
pivot
(
sum(product_ammount)
for PivotDate in (' +@cols+ ')
) p'
您不必LEFT JOIN
到#temp
,因为for
操作中的pivot
子句确实返回了所有日期的列表。
答案 2 :(得分:1)
基于Shakeer Mirza和Giorgos Betos回答这个问题,最后我解决了这个问题。这是此问题的完整查询
DECLARE @cols AS NVARCHAR(MAX)
DECLARE @colswithNoNulls AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
DECLARE @tanggal_awal date
DECLARE @tanggal_akhir date
DECLARE @print nvarchar(MAX)
CREATE TABLE #datatable
(
product_id int,
product_date date,
product_ammount int
)
SET @tanggal_awal = convert(date,'02-01-2017')
SET @tanggal_akhir = convert(date,'02-27-2017')
insert into #datatable (product_id,product_date,product_ammount) VALUES
(1,getdate(),100),
(1,getdate(),900),
(2,dateadd(DD,-1,getdate()),400),
(3,DATEADD(DD,4,getdate()),300),
(1,dateadd(DD,4,getdate()),200),
(2,dateadd(DD,2,getdate()),700),
(4,dateadd(DD,-3,getdate()),1000)
;WITH CTE (datelist,maxdate) AS
(
select min(@tanggal_awal) datelist, max(product_date) maxdate
from #datatable
union all
select dateadd(dd, 1, datelist), @tanggal_akhir
from cte
where datelist < maxdate
) SELECT c.datelist
into #temp
from cte c
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(CHAR(10), datelist, 120))
from #temp
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @colswithNoNulls = STUFF((SELECT distinct ',ISNULL(' + QUOTENAME(convert(CHAR(10), datelist, 120)) +',''0'') '+ QUOTENAME(convert(CHAR(10), datelist, 120))
from #temp
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query =
'SELECT product_id, '+ @colswithNoNulls+' FROM
(
select b.product_id, coalesce(b.product_ammount,0) as product_ammount,
convert(CHAR(10), product_date, 120) PivotDate
from #datatable b
where product_date between @tanggal_awal and @tanggal_akhir
) x
pivot
(
sum(product_ammount)
for PivotDate in (' +@cols+ ')
) p'
EXECUTE sp_executesql @query ,N'@tanggal_awal DATE, @tanggal_akhir DATE', @tanggal_awal,@tanggal_akhir
GO
drop table #temp
go
drop table #datatable
这个想法来自How to replace (null) values with 0 output in PIVOT。这个想法本身就是制作两个不同的cols,一个用于cols,另一个用于ISNULL。希望这个问题能在不久的将来解决很多问题:)