所以我在这里有http://sqlfiddle.com/#!6/5836b(类似)及以下的表结构。 PForecast表将在顶级(父级)productsku上进行预测。一旦我获得给定时期内的预测项目列表,我需要检索具有最高优先级(Ppriority)编号的子项的产品详细信息(对于该父项sku)。还要在单独的列中列出所有其他子sku的连接(可选)。
create TABLE PForecast (ID INT, sku INT, qty int, entrydate datetime);
INSERT INTO PForecast (ID, sku, qty, entrydate)
VALUES(10, 29490, 1, '2016-09-04 00:00:00.000'),
(19, 112407, 1, '2016-09-04 00:00:00.000'),
(11, 112407, 1, '2016-11-14 00:00:00.000'),
(12, 112344, 12, '2016-01-24 00:00:00.000'),
(13, 112344, 17, '2016-02-05 00:00:00.000'),
(14, 112344, 11, '2016-06-03 00:00:00.000'),
(15, 264856, 7, '2016-05-09 00:00:00.000'),
(16, 26480, 9, '2016-07-18 00:00:00.000'),
(17, 264856, 4, '2016-09-27 00:00:00.000'),
(18, 29490, 2, '2016-12-02 00:00:00.000');
create TABLE AllProd2 (sku INT, ID INT, descrip varchar(100), dept varchar(100),
SubDept varchar(100), class varchar(100), SubClass varchar(100),
Vcode INT, PID INT, Ppriority INT, parentFlag VARCHAR(1));
INSERT INTO AllProd2 (sku, ID, descrip, dept, SubDept, class, SubClass, Vcode, PID, Ppriority, parentFlag)
VALUES
(169243, 21177, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043419, 1, 'C'),
(136643, 21394, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043419, 2, 'C'),
(112344, 1043419, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043419, NULL, 'P'),
(104516, 122, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, 2, 'C'),
(111508, 130, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, 1, 'C'),
(112407, 1043428, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, NULL, 'P'),
(115106, 17043838, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, 3, 'C'),
(161835, 116050567, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, 4, 'C'),
(238777, 1198119585, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043428, 5, 'C'),
(255210, 2092139762, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043555, 2, 'C'),
(264856, 1043555, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043555, NULL, 'P'),
(264863, 1043556, '14 DIA PC/RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043555, 1, 'C'),
(239124, 1995137332, '14 RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043890, 1, 'C'),
(266473, 1043889, '14 RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043890, 2, 'C'),
(266480, 1043890, '14 RD', 'abc', 'def', 'xyz', 'gty', 0004, 1043890, NULL, 'P'),
(26480, 10430, '14 PC/RD', 'abc', 'def', 'xyz', 'gty', 0007, 10430, NULL, 'P'),
(29490, 14480, '14 PC/RD hkj', 'abc', 'def', 'xyz', 'gty', 0004, 14480, NULL, 'N')
;
如果我可以使用数据集构建临时表会更好,因为我需要获取更多信息并在此步骤后加入其他表。
感谢任何帮助。
答案 0 :(得分:0)
通过阅读您的评论,我认为您正在寻找以下内容:
DECLARE @startDate DATE = '2016-06-01', @endDate DATE = '2016-12-01'
SELECT A.*, STUFF((
SELECT ', ' + CASE WHEN PPriority IS NULL THEN 'P' + CONVERT(NVARCHAR, SKU) ELSE CONVERT(NVARCHAR, SKU) END
FROM AllProd2
WHERE PID = A.PID
AND ISNULL(PPriority, 0) <> A.PPriority
ORDER BY CASE WHEN PPriority IS NULL THEN 0 ELSE 1 END, PPriority
FOR XML PATH ('')), 1, 2, '') OtherSKU
-- INTO #tmpResults
FROM (SELECT SKU
FROM PForecast
WHERE entrydate >= @startDate
AND entrydate <= @endDate
GROUP BY SKU) P
LEFT JOIN AllProd2 A ON P.SKU = (SELECT SKU FROM AllProd2 WHERE ID = A.PID)
WHERE ISNULL(PPriority, 0) = (SELECT MAX(ISNULL(PPriority, 0)) FROM AllProd2 WHERE PID = A.PID)
这为指定的日期范围提供了以下内容:
+--------+------------+--------------+-----+-----+-----+-----+---+---------+------+---+-----------------------------------------+
| 26480 | 10430 | 14 PC/RD | abc | def | xyz | gty | 7 | 10430 | NULL | P | NULL |
| 29490 | 14480 | 14 PC/RD hkj | abc | def | xyz | gty | 4 | 14480 | NULL | N | NULL |
| 136643 | 21394 | 14 DIA PC/RD | abc | def | xyz | gty | 4 | 1043419 | 2 | C | P112344, 169243 |
| 238777 | 1198119585 | 14 DIA PC/RD | abc | def | xyz | gty | 4 | 1043428 | 5 | C | P112407, 111508, 104516, 115106, 161835 |
| 255210 | 2092139762 | 14 DIA PC/RD | abc | def | xyz | gty | 4 | 1043555 | 2 | C | P264856, 264863 |
+--------+------------+--------------+-----+-----+-----+-----+---+---------+------+---+-----------------------------------------+