0

我正在构建一个Common Table Expression (CTE)在查询SQL Server 2008中使用的 in PIVOT

我很难正确地对输出进行排序,因为中间存在将字符串数据夹在中间的数值。是否有可能做到这一点?

这是一个快速而肮脏的例子,真正的查询将跨越几年的价值。

例子:

Declare @startdate as varchar(max);
Declare @enddate as varchar(max);
Set @startdate = cast((DATEPART(yyyy, GetDate())-1) as varchar(4))+'-12-01';
Set @enddate = cast((DATEPART(yyyy, GetDate())) as varchar(4))+'-03-15';

WITH DateRange(dt) AS
    (
        SELECT CONVERT(datetime, @startdate) dt
        UNION ALL
        SELECT DATEADD(dd,1,dt) dt FROM DateRange WHERE dt < CONVERT(datetime, @enddate)
    )
    SELECT DISTINCT ',' + QUOTENAME((cast(DATEPART(yyyy, dt) as varchar(4)))+'-Week'+(cast(DATEPART(ww, dt) as varchar(2)))) FROM DateRange

电流输出:

,[2012-Week48]
,[2012-Week49]
,[2012-Week50]
,[2012-Week51]
,[2012-Week52]
,[2012-Week53]
,[2013-Week1]
,[2013-Week10]
,[2013-Week11]
,[2013-Week2]
,[2013-Week3]
,[2013-Week4]
,[2013-Week5]
,[2013-Week6]
,[2013-Week7]
,[2013-Week8]
,[2013-Week9]

期望的输出:

,[2012-Week48]
,[2012-Week49]
,[2012-Week50]
,[2012-Week51]
,[2012-Week52]
,[2012-Week53]
,[2013-Week1]
,[2013-Week2]
,[2013-Week3]
,[2013-Week4]
,[2013-Week5]
,[2013-Week6]
,[2013-Week7]
,[2013-Week8]
,[2013-Week9]
,[2013-Week10]
,[2013-Week11]

编辑

当然,在我发布问题后,我的大脑开始工作。我将其更改DATEADD为添加 1 周而不是 1 天,然后在选择中取出DISTINCT它并且它起作用了。

DECLARE @startdate AS VARCHAR(MAX);
DECLARE @enddate AS VARCHAR(MAX);
SET @startdate = CAST((DATEPART(yyyy, GetDate())-1) AS VARCHAR(4))+'-12-01';
SET @enddate = CAST((DATEPART(yyyy, GetDate())) AS VARCHAR(4))+'-03-15';

WITH DateRange(dt) AS
    (
            SELECT CONVERT(datetime, @startdate) dt
            UNION ALL
            SELECT DATEADD(ww,1,dt) dt FROM DateRange WHERE dt < CONVERT(datetime, @enddate)
    )
    SELECT ',' + QUOTENAME((CAST(DATEPART(yyyy, dt) AS VARCHAR(4)))+'-Week'+(CAST(DATEPART(ww, dt) AS VARCHAR(2)))) FROM DateRange
4

6 回答 6

1

我看不到示例 SQL 代码(该站点在我所在的位置被列入黑名单)。

这是按正确顺序对数据进行排序的技巧,即先使用长度,然后使用值:

select col
from t
order by left(col, 6), len(col), col;
于 2013-05-29T21:46:13.217 回答
1

您是否考虑过对两个临时列进行排序(smallint 中的年和 tinyint 中的周以节省空间……或者如果空间对您来说不是问题并且您更喜欢快速运行,则直接使用 datepart 整数)以及使用“按年排序” , 星期” ?

如果您使用更合适的类型(我的建议)存储日期,它将变为:

WITH [Define the CTE expression name and column list]
AS
  (
    SELECT CAST(DATEPART(yyyy, dt) as smallint(4)) year, cast(DATEPART(ww, dt) as tinyint(2)) week, [your columns here]
    FROM DateRange WHERE dt < @enddate
  )
[Define the outer query referencing the CTE name]
ORDER BY year, week;
GO

另外,请注意字符串操作会减慢您的查询速度,因此请尽可能避免使用它们!

于 2013-05-29T21:51:59.280 回答
0

我需要更改DATEADD查询部分并删除DISTINCT. 一旦更改了订单,它就会自行正确排序

DECLARE @startdate AS VARCHAR(MAX);
DECLARE @enddate AS VARCHAR(MAX);
SET @startdate = CAST((DATEPART(yyyy, GetDate())-1) AS VARCHAR(4))+'-12-01';
SET @enddate = CAST((DATEPART(yyyy, GetDate())) AS VARCHAR(4))+'-03-15';

WITH DateRange(dt) AS
    (
            SELECT CONVERT(datetime, @startdate) dt
            UNION ALL
            SELECT DATEADD(ww,1,dt) dt FROM DateRange WHERE dt < CONVERT(datetime, @enddate)
    )
    SELECT ',' + QUOTENAME((CAST(DATEPART(yyyy, dt) AS VARCHAR(4)))+'-Week'+(CAST(DATEPART(ww, dt) AS VARCHAR(2)))) FROM DateRange
于 2013-05-30T04:31:24.990 回答
0

由于您dt用于生成字符串,因此您应该使用日期的部分进行排序:

WITH DateRange(dt) ...
SELECT DISTINCT ',' + QUOTENAM...
ORDER BY DATEPART(yyyy, dt), DATEPART(ww, dt)
于 2013-05-29T21:56:01.677 回答
0

我喜欢 Gordon 的回答,但如果您一心想要按顺序进行文本操作:

ORDER BY     CAST(REPLACE(LEFT('[2012-Week48]',5),'[','')AS INT)
            ,CAST(REPLACE(RIGHT('[2012-Week48]',CHARINDEX('Week','[2012-Week48]')-4),']','') AS INT)
于 2013-05-29T21:50:17.360 回答
0

这是另一个将列的开始和结束部分转换为整数的选项。

SELECT *
FROM YourTable
ORDER BY CAST(SUBSTRING(yourcolumn,1,4) as int), 
  CAST(SUBSTRING(yourcolumn,CHARINDEX('Week',yourcolumn)+4,len(yourcolumn)) as int)

假设数据的格式始终相同,这将起作用。

于 2013-05-29T21:52:37.160 回答