Get Identity after selecting from distinct result(从不同的结果中选择后获取身份)
问题描述
Model_Tabl ( ID, ModelName, ModelQuantity)
Items_Tabl ( I_Code, IName, ID)
将新行插入 (Model_Table) 后 - 触发器将多行插入 (Items_Table) 取决于 (Model_Table) 中的 ModelQuantity,直到现在它的工作正常
after inserting new row into (Model_Table) - Triggers insert multi row into (Items_Table) Depend on ModelQuantity from (Model_Table) , and until now its work fine
I Created "选择不同的 ModelName , Sum(ModelQuantity) group by ModelName"我的结果很好
I Created "select distinct ModelName , Sum(ModelQuantity) group by ModelName"
and i got result fine
我的问题是:
当我从 (DISTINCT) 查询中选择模型名称时,我想知道我从 (Model_Table) 中选择了哪个 (ID)
When i select model name from (DISTINCT) query i want to know which (ID) I selected from (Model_Table)
Model_ID (TO) Model_Name = 1 (TO) 许多ty
Model_ID (TO) Model_Name = 1 (TO) Many ty
推荐答案
首先,你不需要 DISTINCT 那里,所以你可以摆脱它.然后,你可以试试这个:
First of all, you don't need the DISTINCT there, so you can get rid of it. And then, you can try this:
SELECT ID, ModelName, SUM(ModelQuantity) Quantity
FROM Model_Tabl
GROUP BY ID, ModelName
好的,正如我在评论中解释的那样,如果您有多个同名的 ID,则需要选择一个,即最大 ID 或最小 ID(最终还是没有意义)仅在您实际需要识别该 ID 时选择一个 ID),但是,这里是:
Ok, as I explained on my comment, if you have multiple IDs with the same name, you need to choose one, either the max or the min ID (it still doesn't make sense that you are going to end up just choosing one ID when you actually need to identify that ID), but well, here it is:
SELECT MIN(ID) ID, ModelName, SUM(ModelQuantity) Quantity
FROM Model_Tabl
GROUP BY ModelName
这篇关于从不同的结果中选择后获取身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从不同的结果中选择后获取身份
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
