我想获得两个不同的临时表for pivot table的完整日期范围。查询是这样的:
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)
DECLARE @querycount AS 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,DATEADD(dd,-1,(DATEADD(mm,1,@tanggal_awal))))
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),
(4, DATEADD(MM, 1, GETDATE()), 200)
;WITH CTE (datelist,maxdate) AS
(
SELECT
CONVERT(INT, (MIN(DATEPART(day, @tanggal_awal)))) datelist,
CONVERT(INT, MAX(DATEPART(day, product_date))) maxdate
FROM
#datatable
UNION ALL
SELECT
CONVERT(INT, (DATEPART(day, datelist))),
CONVERT(INT, (DATEPART(day, @tanggal_akhir)))
FROM
cte
WHERE
datelist < maxdate
)
SELECT c.datelist
INTO #temp
FROM cte c
ORDER BY c.datelist
OPTION (maxrecursion 0)
SELECT * FROM #temp
SELECT
@cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(CONVERT(int, datelist))
FROM #temp
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
PRINT @cols
然而,结果并不像我期待的那样。根据打印结果,它显示如下:
[1],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[2],[20],[21],[22],[23],[24],[25],[26],[27],[28],[3],[4],[5],[6],[7],[8],[9]
我想要的结果是这样的
[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28]
我该怎么做才能解决这个问题?非常感谢你:))
答案 0 :(得分:2)
删除DISTINCT
关键字并在ORDER BY
语句中添加SELECT
子句:
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(int, datelist))
FROM #temp
ORDER BY CONVERT(int, datelist)
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,''
)
答案 1 :(得分:1)
这是一种方法:
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(int, datelist))
FROM #temp
GROUP BY datelist
ORDER BY CONVERT(int, datelist)
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,''
)
我已将DISTINCT
更改为GROUP BY
,以便我可以在CONVERT(int, datelist)
条款中使用ORDER BY
。
答案 2 :(得分:1)
Getting TheOutput as
[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28]
Begin TRAN
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)
DECLARE @querycount AS 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,DATEADD(dd,-1,(DATEADD(mm,1,@tanggal_awal))))
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),
(4,DATEADD(MM,1,GETDATE()),200)
;WITH CTE (datelist,maxdate) AS
(
SELECT CONVERT(INT,(MIN(DATEPART(day,@tanggal_awal)))) datelist, CONVERT(INT,MAX(DATEPART(day,product_date))) maxdate
FROM #datatable
UNION ALL
SELECT CONVERT(INT,(DATEPART(day,datelist))), CONVERT(INT,(DATEPART(day,@tanggal_akhir)))
FROM cte
WHERE datelist < maxdate
) SELECT c.datelist
INTO #temp
FROM cte c
ORDER BY c.datelist
OPTION (maxrecursion 0)
--select * from #temp
SELECT @cols = STUFF((SELECT ',' + QUOTENAME(CONVERT(nvarchar(20), datelist))
FROM #temp t1
FOR XML PATH (''))
, 1, 1, '') from #temp t2
SELECT @cols
PRINT @cols
ROLLBACK TRAN