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
这篇关于从不同的结果中选择后获取身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从不同的结果中选择后获取身份
基础教程推荐
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01