我如何根据项目编号进行分组?
我想删除所有重复的i。我已经尝试了此代码,但是它不会让我组成,尽管它是在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_]
I want to erase all duplicates i.[No] has, since it has a one to many relation to other tables or fields. I have tried this code, but it will not let me group by, although, it comes after the where clause. Any ideas how to group by [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_]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i.No
组的组将要求最终结果每行只有一个行。因此,您需要提前分辨出您想使用可能不同的值其他选定列会返回的情况。例如,对于i.no = 1,SQL处理器不知道您的结果中是否有多个行,以及(例如)si.sabalable是否可以在不同的行上具有不同的值,它希望您告诉它。你想发生。在大多数情况下,您希望在这些过程中发生“聚合”;像总和或平均水平,或采取最小的(SI.Si.sale)或最大等。除非您这样做,否则您不能按I.NO进行分组。
因此,如果您想通过i.no进行分组,请确定您要使用选择列表中的其他列进行的操作,然后使用sum/min/min/max/avg等将它们汇总,或者通过列表也(在这种情况下,您将在小组中这些值的唯一组合有一排结果)
Group by i.No
would require the final result to have only one row per i.No.Because of this you need to tell, up front, what you want to happen with potentially different values other selected columns would return. For example, for i.No=1 the SQL processor doesn't know if there are multiple rows in your results and if (as an example) si.Saleable can have different values on different rows, and it wants you to tell it what you want happen. Most of the time you want an 'aggregation' to happen on those; like SUM or AVERAGE them, or take the smallest (MIN(si.Saleable)) or largest etc. Unless you do so, you can't group by i.No.
Therefore, if you want to GROUP BY i.No, decide what you want to do with the other columns in your select list, and either aggregate them using SUM/MIN/MAX/AVG, etc., or add them to the GROUP BY list, as well (in which case you would have one row of result per unique combination of those values in the group by)