节日快乐!
几个月前,我在Oralce问题中询问过这个问题,并想重温这个问题。问题是当使用WM_CONCAT函数时(在Oracle中),结果包括前面或我想填充的字段之前的(,)。我用谷歌搜索找到了解决方案但到目前为止没有成功。我应该在WM_CONCAT函数中包含什么才能删除字段的额外空字符串?有人可以看一下吗?谢谢, 乔
我在下面有一些样本记录
CREATE TABLE #TEMP
(
COMPANY VARCHAR (50),
ID INT,
PRODUCT VARCHAR(50)
)
INSERT INTO #TEMP VALUES ('APPLES', '123', 'IPHONE4')
INSERT INTO #TEMP VALUES ('APPLES', '123', 'IPHONE4S')
INSERT INTO #TEMP VALUES ('APPLES', '123', 'IPHONE5')
INSERT INTO #TEMP VALUES ('SAMSUNG', '124', 'GALAXY S2')
INSERT INTO #TEMP VALUES ('SAMSUNG', '124', 'GALAXY S3')
INSERT INTO #TEMP VALUES ('SAMSUNG', '124', 'GALAXY S4')
INSERT INTO #TEMP VALUES ('NOKIA', '125', 'C5-02')
INSERT INTO #TEMP VALUES ('NOKIA', '125', 'C5-03')
INSERT INTO #TEMP VALUES ('NOKIA', '125', 'C5-04')
运行此查询时,它将生成为
SELECT
COMPANY,
ID,
WM_CONCAT(PRODUCT) AS PRODUCT, --STUFF PRODUCT COLUMN
FROM
(
SELECT
COMPANY,
ID,
PRODUCT
FROM #TEMP
)
GROUP BY COMPANY, ID
COMPANY ID PRODUCT
APPLES 123 IPHONE4, IPHONE4S, IPHONE5
SAMSUNG 124 ,GALAXY S2, GALAXY S3, GALAXY S4
NOKIA 125 ,C5-02, C5-03, C5-04
答案 0 :(得分:3)
只需替换:
WM_CONCAT(PRODUCT)
with:
LTRIM(WM_CONCAT(PRODUCT),',')
这将删除列中的所有主要逗号。
答案 1 :(得分:0)
尝试使用以下查询
SELECT
COMPANY,
ID,
WM_CONCAT(PRODUCT) AS PRODUCT
FROM
(
SELECT
COMPANY,
ID,
PRODUCT
FROM TEMP
)
GROUP BY COMPANY, ID
这是您要找的输出吗?
NOKIA 125 C5-02,C5-04,C5-03
APPLES 123 IPHONE4,IPHONE5,IPHONE4S
SAMSUNG 124 GALAXY S2,GALAXY S4,GALAXY S3
查询也可以这样
SELECT
COMPANY,
ID,
WM_CONCAT(PRODUCT) AS PRODUCT
FROM TEMP
--(
-- SELECT
-- COMPANY,
-- ID,
-- PRODUCT
-- FROM TEMP
-- )
GROUP BY COMPANY, ID