我如何根据项目编号分组?
How could I Group according to Item Number?
我想删除 i.[No] 的所有重复项,因为它与其他表或字段具有一对多关系。我已经尝试过这段代码,但它不会让我分组,尽管它位于 where 子句之后。知道如何按 [No_] 分组吗?
SELECT i.[No_]
,i.[Description]
,i.[Manufacturer Code]
,m.[Name] as [Manufacturer Name]
,i.[Second Hand]
,i.[Set Item]
,FORMAT(p.[Direct Unit Cost], 'N') as [Direct Unit Cost]
,CONVERT(VARCHAR, p.[Starting Date], 104) as [Starting Date]
,CONVERT(VARCHAR, p.[Ending Date], 104) as [Ending Date]
,si.[Saleable]
,FORMAT(il.[SumRemQ], 'N') as [SumRemQ]
FROM [0815Onlinehandel_90].[dbo].[0815GMBH$Item] as i
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Manufacturer] as m
ON i.[Manufacturer Code] = m.[Code]
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Purchase Price] as p
ON i.No_ = p.[Item No_]
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Shop Item] as si
ON p.[Item No_] = si.[Item No_]
INNER JOIN (SELECT [Item No_], SUM([Remaining Quantity]) AS [SumRemQ]
FROM [0815Onlinehandel_90].[dbo].[0815GMBH$Item Ledger Entry]
GROUP BY [Item No_]) as il
ON si.[Item No_] = il.[Item No_]
WHERE i.[Second Hand] = 0
AND i.[Set Item] = 0
AND si.Saleable = 1
AND ([Starting Date] < '01.01.2022' AND [Ending Date] = '01.01.1753')
AND (i.[Manufacturer Code] = '631763'
OR i.[Manufacturer Code] = '631773'
OR i.[Manufacturer Code] = 'HER000002'
OR i.[Manufacturer Code] = '631813'
OR i.[Manufacturer Code] = '18'
OR i.[Manufacturer Code] = '631917'
OR i.[Manufacturer Code] = '4'
OR i.[Manufacturer Code] = 'HER000426')
GROUP BY i.[No_]
Group by i.No
要求最终结果每个 i.No.
只有一行
因此,您需要预先说明您希望使用其他 selected 列 return 可能不同的值发生什么。例如,对于 i.No=1,SQL 处理器不知道您的结果中是否有多行,并且(例如)si.Saleable 是否可以在不同的行上具有不同的值,它希望你告诉它你想要发生什么。大多数时候,你希望 'aggregation' 发生在那些事情上;比如 SUM 或 AVERAGE 它们,或者取最小的 (MIN(si.Saleable)) 或最大的等等。除非你这样做,否则你不能按 i.No.
分组
因此,如果您想按 i.No 进行分组,请决定您要对 select 列表中的其他列执行的操作,然后使用 SUM/MIN/MAX/AVG 等对它们进行聚合., 或者也将它们添加到 GROUP BY 列表中(在这种情况下,分组依据中这些值的每个唯一组合都会有一行结果)
我想删除 i.[No] 的所有重复项,因为它与其他表或字段具有一对多关系。我已经尝试过这段代码,但它不会让我分组,尽管它位于 where 子句之后。知道如何按 [No_] 分组吗?
SELECT i.[No_]
,i.[Description]
,i.[Manufacturer Code]
,m.[Name] as [Manufacturer Name]
,i.[Second Hand]
,i.[Set Item]
,FORMAT(p.[Direct Unit Cost], 'N') as [Direct Unit Cost]
,CONVERT(VARCHAR, p.[Starting Date], 104) as [Starting Date]
,CONVERT(VARCHAR, p.[Ending Date], 104) as [Ending Date]
,si.[Saleable]
,FORMAT(il.[SumRemQ], 'N') as [SumRemQ]
FROM [0815Onlinehandel_90].[dbo].[0815GMBH$Item] as i
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Manufacturer] as m
ON i.[Manufacturer Code] = m.[Code]
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Purchase Price] as p
ON i.No_ = p.[Item No_]
INNER JOIN [0815Onlinehandel_90].[dbo].[0815GMBH$Shop Item] as si
ON p.[Item No_] = si.[Item No_]
INNER JOIN (SELECT [Item No_], SUM([Remaining Quantity]) AS [SumRemQ]
FROM [0815Onlinehandel_90].[dbo].[0815GMBH$Item Ledger Entry]
GROUP BY [Item No_]) as il
ON si.[Item No_] = il.[Item No_]
WHERE i.[Second Hand] = 0
AND i.[Set Item] = 0
AND si.Saleable = 1
AND ([Starting Date] < '01.01.2022' AND [Ending Date] = '01.01.1753')
AND (i.[Manufacturer Code] = '631763'
OR i.[Manufacturer Code] = '631773'
OR i.[Manufacturer Code] = 'HER000002'
OR i.[Manufacturer Code] = '631813'
OR i.[Manufacturer Code] = '18'
OR i.[Manufacturer Code] = '631917'
OR i.[Manufacturer Code] = '4'
OR i.[Manufacturer Code] = 'HER000426')
GROUP BY i.[No_]
Group by i.No
要求最终结果每个 i.No.
因此,您需要预先说明您希望使用其他 selected 列 return 可能不同的值发生什么。例如,对于 i.No=1,SQL 处理器不知道您的结果中是否有多行,并且(例如)si.Saleable 是否可以在不同的行上具有不同的值,它希望你告诉它你想要发生什么。大多数时候,你希望 'aggregation' 发生在那些事情上;比如 SUM 或 AVERAGE 它们,或者取最小的 (MIN(si.Saleable)) 或最大的等等。除非你这样做,否则你不能按 i.No.
分组因此,如果您想按 i.No 进行分组,请决定您要对 select 列表中的其他列执行的操作,然后使用 SUM/MIN/MAX/AVG 等对它们进行聚合., 或者也将它们添加到 GROUP BY 列表中(在这种情况下,分组依据中这些值的每个唯一组合都会有一行结果)