求SQL 語句

勉励前行 2007-07-25 09:34:51
表: 
Year int ,Month int, GID int, Price int
年,月,商品,采購價格
求全部商品,最後一次采購的價格。
...全文
187 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
勉励前行 2007-07-25
  • 打赏
  • 举报
回复
leo_lesley(leo) 的應該不對。
c=max(Year),c1=max(Month) 不能匹配最後的一個年月
caibaoying 2007-07-25
  • 打赏
  • 举报
回复
个人倾向于用`` leo_lesley(leo)的```
caibaoying 2007-07-25
  • 打赏
  • 举报
回复
select * from 表 a where not exists (select 1 from 表 where GID=a.GID and [Year]> a.[Year] or ([Year]=a.[Year] and [Month]>a.[Month]))
勉励前行 2007-07-25
  • 打赏
  • 举报
回复
Haiwer(海阔天空) , leo_lesley(leo) 的方法
哪個效率高點呢?
leo_lesley 2007-07-25
  • 打赏
  • 举报
回复
----是这样么?
select *
from table_name t
inner join (select GID , c=max(Year),c1=max(Month) from table_name group by GID) t1
on t.GID=t1.GID and t.Year=t1.c and t.Month=t1.c1
昵称被占用了 2007-07-25
  • 打赏
  • 举报
回复
and 优先级高于 or 可以不必加括号
昵称被占用了 2007-07-25
  • 打赏
  • 举报
回复
or:

select * from 表 a
where [Year]=(select max([Year]) from 表 where GID=a.GID)
and [Month]=(select max([Month]) from 表 where GID=a.GID and [Year]=a.[Year])
echiynn 2007-07-25
  • 打赏
  • 举报
回复
[Year]=a.[Year] and [Month]>a.[Month]
好象還應該套個括號,呵呵
昵称被占用了 2007-07-25
  • 打赏
  • 举报
回复
表: 
Year int ,Month int, GID int, Price int

select * from 表 a
where not exists (
select 1 from 表
where GID=a.GID
and ([Year]> a.[Year]
or [Year]=a.[Year]
and [Month]>a.[Month]
)
)
echiynn 2007-07-25
  • 打赏
  • 举报
回复
...

這不是自己和自己比,這是子查詢與主查詢比

沒看到比較運算符右邊的列是帶了表名前綴的嗎,那個表別名"a"就是主表,第一句"select * from 表 a"就已經給主查詢的表取了別名了
shoter193 2007-07-25
  • 打赏
  • 举报
回复
請問像這種([Year]> a.[Year]
or [Year]=a.[Year]
and [Month]>a.[Month]
自己和自己比較是怎麼個比法??

34,590

社区成员

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

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