我似乎无法弄清楚为什么这在MS SQL Server中不起作用。它似乎没有分组线。具体来说,我看到了:
1036 SC
1036 S1
1094 VO
1094 V1
1094 V2
我希望看到:
1036 SC,S1
1094 VO,V1,V2
有人可以看到语法错误吗?
SELECT DISTINCT oa.acct_cd AS [Account],
STUFF((SELECT ',' + CASE WHEN o.trans_type like 'BUY%' then 'buy of ' else 'sell of ' end + s.ticker AS [text()]
FROM [dbo].[synCRtblTS_ORDER] o INNER JOIN [dbo].[synCRtblCSM_SECURITY] s
ON o.SEC_ID = s.SEC_ID
WHERE o.ORDER_ID = oa.ORDER_ID AND o.status IN ('OPEN','WORK','PENDING')
FOR XML PATH('')), 1, 1, '') [buy/sell]
FROM [dbo].[synCRtblTS_ORDER_ALLOC] oa INNER JOIN tblPortfolio p
ON oa.ACCT_CD = p.Account INNER JOIN tblInvestmentObjective io
ON io.Code = p.InvestmentObjective
WHERE p.AsOfDate = (SELECT AsOfDate FROM tblDateAsOf) and io.CashMgmtStrategy IN ('SC','VO')
GROUP BY oa.ORDER_ID, oa.acct_cd
order by 1
答案 0 :(得分:0)
您提供的信息不够......在您的STUFF中,您创建(并连接)应包含“buy of”或“sell of”的字符串。您在输出中缺少此信息。
猜测您的SC,S1,VO值是CashMgmtStrategy条目。你没有连接它们......
如果您对表格没有更深入的了解,我无法解决您的问题,但在此您可以通过FOR XML
和STUFF
此代码将在逗号分隔列表中列出所有表的名称及其列。
SELECT DISTINCT tbls.name AS TableName,
STUFF(
(
SELECT ', ' + cols.name
FROM sys.columns AS cols
WHERE cols.object_id = tbls.object_id
FOR XML PATH('')
), 1, 2, '') AS ColumnList
FROM sys.tables AS tbls