我正在使用以下查询:
SELECT DISTINCT
DTL.DTL_ITEM_CRC DTL_ITEM_CRC,
PWK.PWK_PROMN_WEEK_KEY PWK_PROMN_WEEK_KEY,
HDR.HDR_VENDR_NBR HDR_VENDR_NBR,
HDR.HDR_VENDR_NAME HDR_VENDR_NAME
FROM
DBO.PROMWEEK PWK, DBO.BLTNHDR HDR, DBO.BLTNDET DTL, DBO.ITEMWHSE ITW
WHERE
DTL.DTL_BULTN_KEY = HDR.HDR_BULTN_KEY
AND HDR.HDR_PROMN_WEEK_KEY = PWK.PWK_PROMN_WEEK_KEY
AND HDR.HDR_BULTN_KEY = ITW.ITW_BULTN_KEY
AND DTL.DTL_ITEM_NBR = ITW.ITW_ITEM_NBR
AND DTL.DTL_ITEM_CRC IN ('2180198')
AND PWK.PWK_PROMN_WEEK_KEY IN ('31','32')
AND GETDATE() < PWK.PWK_START_DATE
AND DTL_ITEM_CRC != '0000000'
AND DTL.DTL_TBLD_CONUS_CNTRL > 0
AND DTL.DTL_TBLD_CONUS_EAST > 0
AND DTL.DTL_TBLD_CONUS_SOUTH > 0
AND DTL.DTL_TBLD_CONUS_WEST > 0
AND DTL.DTL_SPECL_SELL > 0
此查询的结果是
DTL_ITEM_CRC HDR_VENDR_NBR HDR_VENDR_NAME PROMO_WEEK
2180198 60531000 US COTTON LLC 31
2180198 60531000 US COTTON LLC 32
我想得到以下内容:
DTL_ITEM_CRC HDR_VENDR_NBR HDR_VENDR_NAME PROMO_WEEK
2180198 60531000 US COTTON LLC 31 ,32
我正在尝试以下操作,但没有用:
SELECT A.DTL_ITEM_CRC ,A.HDR_VENDR_NBR, A.HDR_VENDR_NAME,STUFF((SELECT distinct ','+ PWK1.PWK_PROMN_WEEK_KEY
FROM DBO.PROMWEEK PWK1,DBO.BLTNHDR HDR1, DBO.BLTNDET DTL1, DBO.ITEMWHSE ITW1
WHERE A.DTL_ITEM_CRC = DTL1.DTL_ITEM_CRC
AND A.HDR_VENDR_NBR = HDR1.HDR_VENDR_NBR
AND A.HDR_VENDR_NAME = HDR1.HDR_VENDR_NAME
AND A.PWK_PROMN_WEEK_KEY = PWK1.pwk_promn_week_key
GROUP BY PWK1.PWK_PROMN_WEEK_KEY
FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '') PROMO_WEEK
FROM
(SELECT DISTINCT DTL.DTL_ITEM_CRC DTL_ITEM_CRC, PWK.PWK_PROMN_WEEK_KEY PWK_PROMN_WEEK_KEY, HDR.HDR_VENDR_NBR HDR_VENDR_NBR, HDR.HDR_VENDR_NAME HDR_VENDR_NAME
FROM DBO.PROMWEEK PWK, DBO.BLTNHDR HDR, DBO.BLTNDET DTL, DBO.ITEMWHSE ITW
WHERE DTL.DTL_BULTN_KEY = HDR.HDR_BULTN_KEY AND HDR.HDR_PROMN_WEEK_KEY = PWK.PWK_PROMN_WEEK_KEY
AND HDR.HDR_BULTN_KEY = ITW.ITW_BULTN_KEY AND DTL.DTL_ITEM_NBR = ITW.ITW_ITEM_NBR
and DTL.DTL_ITEM_CRC in ('2180198')
AND PWK.PWK_PROMN_WEEK_KEY in ('31','32') AND GETDATE() < PWK.PWK_START_DATE AND DTL_ITEM_CRC != '0000000'
AND DTL.DTL_TBLD_CONUS_CNTRL > 0 AND DTL.DTL_TBLD_CONUS_EAST > 0 AND DTL.DTL_TBLD_CONUS_SOUTH > 0 AND DTL.DTL_TBLD_CONUS_WEST > 0 AND DTL.DTL_SPECL_SELL > 0 ) A
可以帮忙吗?
答案 0 :(得分:0)
较新版本的SQL Server中有一个string-agg函数,但是如果您使用的是旧版本,则可以使用XML functions获得所需的输出。该查询看起来很可怕,但这应该可以工作(没有示例数据就没有对其进行测试,因此您可能需要对其进行调整):
SELECT DISTINCT
DTL.DTL_ITEM_CRC
, PROMO_WEEK =
(
SELECT STUFF(
(SELECT ', ' + PWK_PROMN_WEEK_KEY
FROM
DBO.PROMWEEK PWK
JOIN DBO.BLTNHDR HDR
ON HDR.HDR_PROMN_WEEK_KEY = PWK.PWK_PROMN_WEEK_KEY
JOIN DBO.BLTNDET DTL
ON DTL.DTL_BULTN_KEY = HDR.HDR_BULTN_KEY
JOIN DBO.ITEMWHSE ITW
ON DTL.DTL_ITEM_NBR = ITW.ITW_ITEM_NBR
AND HDR.HDR_BULTN_KEY = ITW.ITW_BULTN_KEY
WHERE
DTL.DTL_ITEM_CRC IN ('2180198')
AND PWK.PWK_PROMN_WEEK_KEY IN ('31','32')
AND GETDATE() < PWK.PWK_START_DATE
AND DTL_ITEM_CRC != '0000000'
AND DTL.DTL_TBLD_CONUS_CNTRL > 0
AND DTL.DTL_TBLD_CONUS_EAST > 0
AND DTL.DTL_TBLD_CONUS_SOUTH > 0
AND DTL.DTL_TBLD_CONUS_WEST > 0
AND DTL.DTL_SPECL_SELL > 0
ORDER BY PWK_PROMN_WEEK_KEY
FOR XML PATH('') ,
ROOT('MyString') ,
TYPE
).value('/MyString[1]', 'varchar(max)'), 1, 2, ''
)
)
, HDR.HDR_VENDR_NBR
, HDR.HDR_VENDR_NAME
FROM DBO.PROMWEEK PWK
JOIN DBO.BLTNHDR HDR
ON HDR.HDR_PROMN_WEEK_KEY = PWK.PWK_PROMN_WEEK_KEY
JOIN DBO.BLTNDET DTL
ON DTL.DTL_BULTN_KEY = HDR.HDR_BULTN_KEY
JOIN DBO.ITEMWHSE ITW
ON DTL.DTL_ITEM_NBR = ITW.ITW_ITEM_NBR
AND HDR.HDR_BULTN_KEY = ITW.ITW_BULTN_KEY
WHERE
DTL.DTL_ITEM_CRC IN ('2180198')
AND PWK.PWK_PROMN_WEEK_KEY IN ('31','32')
AND GETDATE() < PWK.PWK_START_DATE
AND DTL_ITEM_CRC != '0000000'
AND DTL.DTL_TBLD_CONUS_CNTRL > 0
AND DTL.DTL_TBLD_CONUS_EAST > 0
AND DTL.DTL_TBLD_CONUS_SOUTH > 0
AND DTL.DTL_TBLD_CONUS_WEST > 0
AND DTL.DTL_SPECL_SELL > 0