如何将数量字段按ASC顺序排列?

时间:2018-10-10 14:54:19

标签: sql-server sql-server-2012

我正在使用MS SQL Server2012。由于GROUP_CONCAT并不是问题,所以我最终还是使用XML方法(向Sean大喊大叫;),这个芽上没有屏幕截图:))。

当我尝试执行ORDER BY子句时,出现此错误:

  

如果指定了SELECT DISTINCT,则消息145,级别15,状态1,行1的ORDER BY项目必须出现在选择列表中。   总执行时间:00:00:00.068

非常感谢您的帮助。

解决方案:

SELECT DISTINCT [ID], [STATUS], [EXPECTED_WIN_DATE], [CUSTOMER_ID],[CUSTOMER_PART_ID] As [Style #],
(
    SELECT STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR, FLOOR(QTY))
        FROM QUOTE_PRICE
        JOIN QUOTE_LINE
        ON QUOTE_PRICE.QUOTE_ID = QUOTE_LINE.QUOTE_ID
        WHERE QUOTE_PRICE.QUOTE_ID = QUOTE.ID
        ORDER BY 1 ASC
        FOR XML PATH('')) ,1,1,'')
) AS Quantity

FROM QUOTE

JOIN QUOTE_LINE
ON QUOTE.ID = QUOTE_LINE.QUOTE_ID

WHERE PPV8.dbo.QUOTE.STATUS = 'A'
AND PPV8.dbo.QUOTE.EXPECTED_WIN_DATE > '20170101'
AND PPV8.dbo.QUOTE.CUSTOMER_ID IS NOT NULL
Order By PPV8.dbo.QUOTE.ID ASC
-- Order By PPV8.dbo.QUOTE.EXPECTED_WIN_DATE ASC

这是我的示例数据表:

CREATE TABLE mytable (
    ID INT NOT NULL PRIMARY KEY
    ,STATUS VARCHAR(1) NOT NULL
    ,EXPECTED_WIN_DATE VARCHAR(23) NOT NULL
    ,CUSTOMER_ID VARCHAR(6) NOT NULL
    ,Style_ VARCHAR(12) NOT NULL
    ,Quantity VARCHAR(36) NOT NULL
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04631
    ,'A'
    ,'2018-10-10 00:00:00.000'
    ,'PEP100'
    ,'PEP1008991Q'
    ,'50'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04640
    ,'A'
    ,'2018-10-03 00:00:00.000'
    ,'PRO300'
    ,'PRO3007848'
    ,'100'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04651
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009121Q'
    ,'1000,1250,1500,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04651
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009122Q'
    ,'1000,1250,1500,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04652
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009119Q'
    ,'1000,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04652
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009120Q'
    ,'1000,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04653
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009117Q'
    ,'1000,1500,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04653
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009118Q'
    ,'1000,1500,250,500,750'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04654
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009115Q'
    ,'1000,250,2500,4000,500,5000,750,8000'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04654
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009116Q'
    ,'1000,250,2500,4000,500,5000,750,8000'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04655
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'PEP100'
    ,'PEP1009114Q'
    ,'994'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04666
    ,'A'
    ,'2018-10-12 00:00:00.000'
    ,'CVS100'
    ,'CVS1009128S'
    ,'6500,7500,8000'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04667
    ,'A'
    ,'2018-10-16 00:00:00.000'
    ,'CVS100'
    ,'CVS1009129D'
    ,'6000'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04668
    ,'A'
    ,'2018-10-10 00:00:00.000'
    ,'EAR100'
    ,'EAR1009093S'
    ,'150,250,500'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04669
    ,'A'
    ,'2018-10-16 00:00:00.000'
    ,'DUR200'
    ,'DUR2008478'
    ,'1000,1500,600'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04670
    ,'A'
    ,'2018-10-10 00:00:00.000'
    ,'HAY100'
    ,'HAY1009098S1'
    ,'1000,500'
    );

INSERT INTO mytable (
    ID
    ,STATUS
    ,EXPECTED_WIN_DATE
    ,CUSTOMER_ID
    ,Style_
    ,Quantity
    )
VALUES (
    04670
    ,'A'
    ,'2018-10-10 00:00:00.000'
    ,'HAY100'
    ,'HAY1009098S2'
    ,'1000,500'
    );

这是我的查询:

    SELECT DISTINCT [ID], [STATUS], [EXPECTED_WIN_DATE], [CUSTOMER_ID],[CUSTOMER_PART_ID] As [Style #],
(
    SELECT STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR, FLOOR(QTY))
        FROM QUOTE_PRICE
        JOIN QUOTE_LINE
        ON QUOTE_PRICE.QUOTE_ID = QUOTE_LINE.QUOTE_ID
        WHERE QUOTE_PRICE.QUOTE_ID = QUOTE.ID
        ORDER BY QUOTE_PRICE.QTY ASC
        FOR XML PATH('')) ,1,1,'')
) AS Quantity

FROM QUOTE

JOIN QUOTE_LINE
ON QUOTE.ID = QUOTE_LINE.QUOTE_ID

WHERE PPV8.dbo.QUOTE.STATUS = 'A'
AND PPV8.dbo.QUOTE.EXPECTED_WIN_DATE > '20170101'
AND PPV8.dbo.QUOTE.CUSTOMER_ID IS NOT NULL
Order By PPV8.dbo.QUOTE.ID ASC
-- Order By PPV8.dbo.QUOTE.EXPECTED_WIN_DATE ASC

2 个答案:

答案 0 :(得分:0)

一种简单(但皱眉)的方法是在您的订单中使用列号。在您的查询中,数量为第6列,因此请使用ORDER BY 6。如果您只是作为临时查询运行它,那可能就可以了。

另一种非常简单的方法是将查询包裹在select *中,然后按如下所示对该查询下订单:

select *
from
(
SELECT DISTINCT [ID], [STATUS], [EXPECTED_WIN_DATE], [CUSTOMER_ID],

[CUSTOMER_PART_ID] As [Style #],
(
    SELECT STUFF((SELECT DISTINCT ',' + CONVERT(VARCHAR, FLOOR(QTY))
        FROM QUOTE_PRICE
        JOIN QUOTE_LINE
        ON QUOTE_PRICE.QUOTE_ID = QUOTE_LINE.QUOTE_ID
        WHERE QUOTE_PRICE.QUOTE_ID = QUOTE.ID
        ORDER BY 1 ASC
        FOR XML PATH('')) ,1,1,'')
) AS Quantity

FROM QUOTE

JOIN QUOTE_LINE
ON QUOTE.ID = QUOTE_LINE.QUOTE_ID

WHERE PPV8.dbo.QUOTE.STATUS = 'A'
AND PPV8.dbo.QUOTE.EXPECTED_WIN_DATE > '20170101'
AND PPV8.dbo.QUOTE.CUSTOMER_ID IS NOT NULL
) q
ORDER BY Quantity

答案 1 :(得分:0)

尝试将不同的数量查询放入子查询中,然后对其进行排序

SELECT STUFF((SELECT ',' + CONVERT(VARCHAR, FLOOR(QTY))
        FROM (select distinct QUOTE_PRICE.QTY
        from QUOTE_PRICE
        JOIN QUOTE_LINE
        ON QUOTE_PRICE.QUOTE_ID = QUOTE_LINE.QUOTE_ID
        WHERE QUOTE_PRICE.QUOTE_ID = QUOTE.ID) t
        ORDER BY QTY asc
        FOR XML PATH('')) ,1,1,'')