我如何根据项目编号进行分组?

发布于 2025-02-01 00:56:39 字数 1506 浏览 3 评论 0原文

我想删除所有重复的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有影子陪我不离不弃 2025-02-08 00:56:39

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文