如何按项目 ID 对项目进行分组
我有一个名为 TRNSPOINDT 的表。 TRNSPOINDT 具有三个字段,分别名为 itemid、Qty、projectname。我需要按 itemid 对项目进行分组,并显示相对于 itemid 的数量总和。我已经这样做了,查询如下,
SELECT ITMID ,SUM(QTY) AS QTY FROM TRNSPOINDT GROUP BY ITMID
问题是,我应该显示对应于itemid的项目名称。但是,当我尝试显示项目名称时,出现错误。
我尝试过这个查询,
SELECT ITMID ,SUM(QTY) AS QTY,PROJECTNAME FROM TRNSPOINDT GROUP BY ITMID
我收到一个错误,因为
Column 'TRNSPOINDT.PROJECTNAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
我知道,我收到此错误是因为单个 itemid 存在多个项目名称。但我应该在 itemid 附近显示项目名称。如何做到这一点..
提前致谢, 普拉文·T
I have a table named TRNSPOINDT. TRNSPOINDT has three fields named as itemid, Qty, projectname. I need to group the items by itemid and display the sum of Qty with respect to the itemid. I have done this, the query is as follows,
SELECT ITMID ,SUM(QTY) AS QTY FROM TRNSPOINDT GROUP BY ITMID
The problem is, i should display the project name respective to the itemid. But while iam trying to display project name, i got a error.
I have tried this query,
SELECT ITMID ,SUM(QTY) AS QTY,PROJECTNAME FROM TRNSPOINDT GROUP BY ITMID
I got a error as,
Column 'TRNSPOINDT.PROJECTNAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know that, i got this error because there is more than one project name is exist for a single itemid. But i should display the project name near to the itemid. How to do it..
Thanks in advance,
Praveen.T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出现此错误的原因是在此查询中包含项目名称没有意义。例如,假设您
想要行
但如何将项目名称附加到第一个结果行?
您在问题中提到您知道原因,但它确实没有解决方案,除了按项目和项目分组在一起,或者为项目创建一串以逗号分隔的项目名称(这不完全是“非规范化”,但也许是你想要的)。
您的意思是这样吗:
它将
按所有 itemid-projectname 组合对 It 进行分组。
The reason for this error is that it doesn't make sense to include the project name in this query. For example suppose you had
You want rows for
But how would you attach a projectname to the first result row?
You mention in your question that you know the reason, but it really doesn't have a solution, other than grouping by item and project together, or creating a string of comma-separated project names for the items (which isn't exactly "denormalized", but is maybe what you want).
Do you mean this:
It would give
It groups by all itemid-projectname combinations.
两种方式取决于您“确切”地寻找什么。首先在分组依据中添加项目名称列。
这将在结果中显示项目名称,但总和将根据 ProjectNAME 的数量计算,而不是按 ITMID 计算,就像您所说的一个 ITMID 对应多个 PROJECTNAME 一样。
如果您不希望为 PROJECTNAME 计算 Sum,而仅根据 ITMID 计算 Sum。那么你将不得不使用子查询。
尽管添加 PROJECTNAME 列没有意义,因为它不会代表正确的信息。
Two ways depending on what 'exactly' you are looking for. First add projectname column in group by.
This will display projectname in result, but the sum will be calculated for Qty for ProjectNAME and not by ITMID as you say there are more than one PROJECTNAME for one ITMID.
If you dont want Sum to be calculated for PROJECTNAME but only by ITMID. Then you will have to use subquery.
Though this does not make sense to add PROJECTNAME column as it will not represent the correct information.