从 sql 中的每个客户获取第一条记录
Take the First record from each customer in sql
我在存储过程中创建了临时文件 table,并在该 table 中存储了以下条目。请参阅下面的条目
Table 姓名 - CustomerOrderList
customerid | customer Name | address | Order id | price | quantity
---------------------------------------------------------------------
2343 | xxxx | address1| 3123 | 34 | 3
2343 | xxxx | address1| 3123 | 35 | 2
2343 | xxxx | address1| 3122 | 23 | 1
4343 | YYYY | address2| 3234 | 65 | 5
4343 | YYYY | address2| 3433 | 34 | 4
4555 | ZZZZ | address3| 3232 | 45 | 3
需要得到如下结果
customerid | customer Name | address | Order id | price | quantity
---------------------------------------------------------------------
2343 | xxxx | address1| 3123 | 34 | 3
4343 | YYYY | address2| 3234 | 65 | 5
4555 | ZZZZ | address3| 3232 | 45 | 3
请建议 sql 查询。
我使用下面的查询来获取每个客户的第一条记录
select * 来自@TempProductInfo
按客户 ID 分组
得到如下错误
在 select 列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。
请帮助我满足我的要求
在 SQL-Server 你可以使用 ROW_NUMBER
类似的东西:
select *
from (
select customerid, [customer Name], address, [Order id], price, quantity,
row_number() over(partition by customerid order by customerid, price) rn
from @TempProductInfo
) x
where rn = 1
我在存储过程中创建了临时文件 table,并在该 table 中存储了以下条目。请参阅下面的条目
Table 姓名 - CustomerOrderList
customerid | customer Name | address | Order id | price | quantity
---------------------------------------------------------------------
2343 | xxxx | address1| 3123 | 34 | 3
2343 | xxxx | address1| 3123 | 35 | 2
2343 | xxxx | address1| 3122 | 23 | 1
4343 | YYYY | address2| 3234 | 65 | 5
4343 | YYYY | address2| 3433 | 34 | 4
4555 | ZZZZ | address3| 3232 | 45 | 3
需要得到如下结果
customerid | customer Name | address | Order id | price | quantity
---------------------------------------------------------------------
2343 | xxxx | address1| 3123 | 34 | 3
4343 | YYYY | address2| 3234 | 65 | 5
4555 | ZZZZ | address3| 3232 | 45 | 3
请建议 sql 查询。
我使用下面的查询来获取每个客户的第一条记录 select * 来自@TempProductInfo 按客户 ID 分组
得到如下错误
在 select 列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。
请帮助我满足我的要求
在 SQL-Server 你可以使用 ROW_NUMBER
类似的东西:
select *
from (
select customerid, [customer Name], address, [Order id], price, quantity,
row_number() over(partition by customerid order by customerid, price) rn
from @TempProductInfo
) x
where rn = 1