如何使用两端的数值正确排序varchar字符串?

时间:2013-05-29 21:39:52

标签: sql sql-server-2008 sql-order-by common-table-expression dynamic-pivot

我正在Common Table Expression (CTE)中构建SQL Server 2008以在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

6 个答案:

答案 0 :(得分:1)

我看不到示例SQL代码(该网站被列入黑名单。)

以下是按顺序排序数据的技巧,首先使用长度,然后使用值:

select col
from t
order by left(col, 6), len(col), col;

答案 1 :(得分: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

另外,请注意字符串操作会减慢您的查询速度,以便在可能的情况下避免使用它们!

答案 2 :(得分:0)

我喜欢戈登的回答,但是如果你对你的订单中的文字处理感到害怕:

ORDER BY     CAST(REPLACE(LEFT('[2012-Week48]',5),'[','')AS INT)
            ,CAST(REPLACE(RIGHT('[2012-Week48]',CHARINDEX('Week','[2012-Week48]')-4),']','') AS INT)

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:0)

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

WITH DateRange(dt) ...
SELECT DISTINCT ',' + QUOTENAM...
ORDER BY DATEPART(yyyy, dt), DATEPART(ww, dt)

答案 5 :(得分: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