这是没有group by的查询,它完全显示数据
SELECT
[b].[FullName]
, STUFF(
(SELECT
','+CONVERT(VARCHAR(30), [t].[pdate], 120)
FROM [DBTrans] [t]
WHERE [t].[dbID] = [uk].[ID] FOR
XML PATH('')), 1, 1, '') AS [ins_Dates]
, [InvNo]
, [AdCaption]
, CONCAT([AdCM], 'x', [AdCOL]) AS [SIZE]
, [NetAmt]
, [RecievedAmount]
, [NetAmt] - [RecievedAmount] AS [O_S]
FROM [DailyBooking] [uk]
INNER JOIN [DBTrans]
ON [uk].[ID] = [DBTrans].[dbID]
INNER JOIN [Publication] [b]
ON [uk].[AdPub] = [b].[ID]
WHERE [b].[FullName] LIKE '%a%';
当我尝试为某些目的进行分组时,它会在不同的更改中显示不同的错误
例如:
Msg 8120,Level 16,State 1,Line 2 专栏' DailyBooking.ID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
SELECT
[b].[FullName]
, STUFF(
(SELECT
','+CONVERT(VARCHAR(30), [t].[pdate], 120)
FROM [DBTrans] [t]
WHERE [t].[dbID] = [uk].[ID] FOR
XML PATH('')), 1, 1, '') AS [ins_Dates]
, [InvNo]
, [AdCaption]
, CONCAT([AdCM], 'x', [AdCOL]) AS [SIZE]
, [NetAmt]
, [RecievedAmount]
, [NetAmt] - [RecievedAmount] AS [O_S]
FROM [DailyBooking] [uk]
INNER JOIN [DBTrans]
ON [uk].[ID] = [DBTrans].[dbID]
INNER JOIN [Publication] [b]
ON [uk].[AdPub] = [b].[ID]
WHERE [b].[FullName] LIKE '%a%'
GROUP BY
[b].[FullName];
答案 0 :(得分:0)
假设其他列在功能上依赖于分组列,最简单的答案是
一个。按其他列分组:
Select r.*
FROM
(SELECT
[b].[FullName]
, STUFF(
(SELECT
','+CONVERT(VARCHAR(30), [t].[pdate], 120)
FROM [DBTrans] [t]
WHERE [t].[dbID] = [uk].[ID] FOR
XML PATH('')), 1, 1, '') AS [ins_Dates]
, [InvNo]
, [AdCaption]
, CONCAT([AdCM], 'x', [AdCOL]) AS [SIZE]
, [NetAmt]
, [RecievedAmount]
, [NetAmt] - [RecievedAmount] AS [O_S]
FROM [DailyBooking] [uk]
INNER JOIN [DBTrans]
ON [uk].[ID] = [DBTrans].[dbID]
INNER JOIN [Publication] [b]
ON [uk].[AdPub] = [b].[ID]
WHERE [b].[FullName] LIKE '%a%') r
GROUP BY
[r].[FullName],
[r].ins_Dates,
[r].InvNo,
[r].AdCaption,
[r].SIZE,
[r].NetAmt,
[r].RecievedAmount,
[r].O_S
或
湾使用聚合函数,例如max:
SELECT
max([b].[FullName])
, STUFF(
(SELECT
','+CONVERT(VARCHAR(30), [t].[pdate], 120)
FROM [DBTrans] [t]
WHERE [t].[dbID] = [uk].[ID] FOR
XML PATH('')), 1, 1, '') AS [ins_Dates]
, max([InvNo])
, max([AdCaption])
, CONCAT([AdCM], 'x', [AdCOL]) AS [SIZE]
, max([NetAmt])
, max([RecievedAmount])
, [NetAmt] - [RecievedAmount] AS [O_S]
FROM [DailyBooking] [uk]
INNER JOIN [DBTrans]
ON [uk].[ID] = [DBTrans].[dbID]
INNER JOIN [Publication] [b]
ON [uk].[AdPub] = [b].[ID]
WHERE [b].[FullName] LIKE '%a%'