1
我希望從表(SQL Server 2012)中選擇數據 - 將一個字段分組,並將該組中的所有副本作爲逗號分隔列表返回。將表值作爲GROUP BY中的逗號分隔列表返回
到目前爲止,我所見過的所有解決方案都不允許使用一個組,併爲整個表格返回一個單獨的逗號分隔列表作爲變量,這對我不起作用。
我需要什麼SQL?
這是我擁有的數據:
BankingSlipID CashSheetSessionID BankingSlipNumber
1 10 abcabc
2 10 xyzxyz
3 10 123465789
4 11 9999999
5 11 55555
6 12 88888
這就是我想:
CashSheetSessionID BankingSlipNumber
10 abcabc, xyzxyz, 123465789
11 9999999, 55555
12 88888
SQL創建源表(數據):
CREATE TABLE [dbo].[BankingSlips](
[BankingSlipID] [int] IDENTITY(1,1) NOT NULL,
[CashSheetSessionID] [int] NOT NULL,
[BankingSlipNumber] [varchar](50) NOT NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[BankingSlips] ON
GO
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (1, 10, N'abcabc')
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (2, 10, N'xyzxyz')
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (3, 10, N'123465789')
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (4, 11, N'9999999')
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (5, 11, N'55555')
INSERT [dbo].[BankingSlips] ([BankingSlipID], [CashSheetSessionID], [BankingSlipNumber]) VALUES (6, 12, N'88888')
SET IDENTITY_INSERT [dbo].[BankingSlips] OFF
GO
我需要寫什麼SQL來獲取?