求教这个sql怎么写?

tababy 2001-12-21 08:23:28
product_name saled_num sale_date
------------ --------- ---------
toy 100 2001-12-12
toy 50 2001-12-02
ink 60 2001-12-05
ink 70 2001-12-09
线在我想得到每个产品最近的销售记录,如
toy 100 2001-12-12
ink 70 2001-12-09
这个sql怎么写?
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanyu010 2001-12-22
  • 打赏
  • 举报
回复
如果你的日期字段还有时分秒的话(在oracle中有)可:
select * from tablename a where (product_name,sale_date) in (select
product_name,max(sale_date) from tablename b group by product_name);
huang_sir 2001-12-22
  • 打赏
  • 举报
回复
Sorry,It Is Right:
Select TableName.Prodcut_Name,TableName.Saled_Num,TableName.Sale_Date From
TableName,(Select Product_Name,Max(Sale_Date) As NewDate From TableName
Group By Product_Name) As S
Where TableName.Product_Name=S.Product_Name And TableName.Sale_Date=S.NewDate
huang_sir 2001-12-22
  • 打赏
  • 举报
回复
Select TableName.Prodcut_Name,TableName.Saled_Num,TableName.Sale_Date From
TableName,(Select Product_Name,Max(Sale_Date) As NewDate From TableName) As S
Where TableName.Product_Name=S.Product_Name And TableName.Sale_Date=S.NewDate
whitegourd 2001-12-21
  • 打赏
  • 举报
回复
试试:select distinct product_name from table order by sale_date
rwq_ 2001-12-21
  • 打赏
  • 举报
回复
select product_name,saled_num,sale_date from table where (sale_date,product_name) in(select max(sale_date),product_name from table group by product_name;
试试看!
smartbigcat 2001-12-21
  • 打赏
  • 举报
回复
select * from <tablename> a
where sale_date=
(select max(sale_date) from <tablename> b
where a.product_name=b.product_name
group by product_name)
acnode 2001-12-21
  • 打赏
  • 举报
回复
select distinct product_name,saled_num,sale_date from tablename order by sale_date

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧