找回密码
 立即注册
搜索
查看: 1757|回复: 17

[软件] SQL简单问题求解,大家帮帮忙

[复制链接]
发表于 2013-6-18 11:58 | 显示全部楼层 |阅读模式
有以下表(BillHead),我想要所有人(name)单据日期(billdate)最早的资料
name    billno        billdate   
Mike    000001        2013/11/1
Mike    000002        2013/10/1
Lucy    000003        2013/4/1
Rose    000004        2013/7/1
Lucy    000005        2013/6/1

明显以下sql是运行不了的,group by 加上billno又不是要的结果,有其他办法吗?
select name,billno,min(billdate) from BillHead
group by name
回复

使用道具 举报

     
发表于 2013-6-18 12:02 | 显示全部楼层
qualify row_number() over(partition by name order by billdate asc)=1
回复

使用道具 举报

     
发表于 2013-6-18 12:11 | 显示全部楼层
select * from BillHead where name||billdate   in   (select name||min(billdate) from BillHead group by name )
回复

使用道具 举报

     
发表于 2013-6-18 12:16 | 显示全部楼层
引用第1楼vacuumsword于2013-06-18 12:02发表的  :
qualify row_number() over(partition by name order by billdate asc)=1
这个语法好先进,学习了
回复

使用道具 举报

发表于 2013-6-18 12:19 | 显示全部楼层
用order by然后limit 1

select name,billno,billdate from BillHead order by billdate limit 1;
回复

使用道具 举报

发表于 2013-6-18 13:36 | 显示全部楼层
引用第1楼vacuumsword于2013-06-18 12:02发表的  :
qualify row_number() over(partition by name order by billdate asc)=1

好高级啊。
回复

使用道具 举报

     
发表于 2013-6-18 14:10 | 显示全部楼层

select name,billno,min(billdate) from BillHead group by name运行不了?mysql上坨坨的没问题
而且如果你的billdate是顺序的,那么这样搜索billno也应该是对的
如果billdate不是顺序的改成
select name,billno,min(billdate) from (select * from BillHead  order by billdate) b group by name
也是可以的啊....

qualify row_number()是啥?Teradata SQL?
回复

使用道具 举报

     
发表于 2013-6-18 14:18 | 显示全部楼层
引用第6楼赤色彗星SEXY于2013-06-18 14:10发表的  :

select name,billno,min(billdate) from BillHead group by name运行不了?mysql上坨坨的没问题
而且如果你的billdate是顺序的,那么这样搜索billno也应该是对的
如果billdate不是顺序的改成
select name,billno,min(billdate) from (select * from BillHead  order by billdate) b group by name
.......

对哦,忘了这个是TD自己的了。不过window function真心好用。
回复

使用道具 举报

     
发表于 2013-6-18 16:30 | 显示全部楼层
引用第6楼赤色彗星SEXY于2013-06-18 14:10发表的  :

select name,billno,min(billdate) from BillHead group by name运行不了?mysql上坨坨的没问题
而且如果你的billdate是顺序的,那么这样搜索billno也应该是对的
如果billdate不是顺序的改成
select name,billno,min(billdate) from (select * from BillHead  order by billdate) b group by name
.......

mysql 这样的语法没有错误??  billno 不在 group by 子句里啊
回复

使用道具 举报

     
发表于 2013-6-18 20:00 | 显示全部楼层
select bh1,name,bh1,billdate   
from BillHead bh1
where billdate  in
(select max(bh2.billdate) from BillHead bh2 where bh2.name = bh1.name group by hb2.name)
group by bh1.name,bh1.billdate
不知道对不对 oracle貌似就要用这种坑爹的方法实现
回复

使用道具 举报

     
发表于 2013-6-18 20:44 | 显示全部楼层
引用第8楼wowhy于2013-06-18 16:30发表的  :


mysql 这样的语法没有错误??  billno 不在 group by 子句里啊



没有,可以正确实现,应该是取第一个bilno
回复

使用道具 举报

     
发表于 2013-6-19 00:42 | 显示全部楼层
引用第8楼wowhy于2013-06-18 16:30发表的  :


mysql 这样的语法没有错误??  billno 不在 group by 子句里啊
MySQL extends the use of GROUP BY so that you can use non-aggregated columns or calculations in the SELECT list that do not appear in the GROUP BY clause. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. For example, you do not need to group on customer.name in the following query:
SELECT order.custid, customer.name, MAX(payments)
  FROM order,customer
  WHERE order.custid = customer.custid
  GROUP BY order.custid;

In standard SQL, you would have to add customer.name to the GROUP BY clause. In MySQL, the name is redundant
回复

使用道具 举报

发表于 2013-6-19 01:11 | 显示全部楼层
2楼学习了。

6楼的方法DB2能用么?
回复

使用道具 举报

发表于 2013-6-19 01:35 | 显示全部楼层
orderby子句
回复

使用道具 举报

发表于 2013-6-19 03:14 | 显示全部楼层
select * from billhead a where not exists (
select * from billhead b where a.name = b.name and b.billdate > a.billdate
);
因为你是billdate最高的一条所以可以这么取巧,最麻烦是最每个name下的前N条,以前写那种查询用两个exists可以组装各种需求,现在已经绕不过去了。
回复

使用道具 举报

发表于 2013-6-19 03:19 | 显示全部楼层
引用第14楼Yurita于2013-06-19 03:14发表的  :

select * from billhead a where not exists (
select * from billhead b where a.name = b.name and b.billdate > a.billdate
);
因为你是billdate最高的一条所以可以这么取巧,最麻烦是最每个name下的前N条,以前写那种查询用两个exists可以组装各种需求,现在已经绕不过去了。

不等值关联not exists走hash么?性能隐约感觉有问题[小表无所谓
回复

使用道具 举报

发表于 2013-6-19 03:27 | 显示全部楼层
引用第15楼yipansansha于2013-06-19 03:19发表的  :


不等值关联not exists走hash么?性能隐约感觉有问题[小表无所谓
走hash什么意思。?name和billdate做联合索引的话会用到的。
回复

使用道具 举报

头像被屏蔽
发表于 2013-6-19 10:28 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|上海互联网违法和不良信息举报中心|网上有害信息举报专区|962110 反电信诈骗|举报电话 021-62035905|Stage1st ( 沪ICP备13020230号-1|沪公网安备 31010702007642号 )

GMT+8, 2026-3-23 11:47 , Processed in 0.163109 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表