Я использую MS SQL Server 2012. Поскольку GROUP_CONCAT
- это не то, что я закончил тем, что использовал метод XML (кричите Шон;), на этом одном скриншоте нет скриншотов:)).
Когда я пытаюсь выполнить предложение ORDER BY
, я получаю эту ошибку:
Msg 145, Уровень 15, Состояние 1, Строка 1 Элементы ORDER BY должны появиться в списке выбора, если SELECTDISTINCT указан.Общее время выполнения: 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