SQL简单问题求解,大家帮帮忙
有以下表(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 qualify row_number() over(partition by name order by billdate asc)=1 select * from BillHead where name||billdate in (select name||min(billdate) from BillHead group by name ) 引用第1楼vacuumsword于2013-06-18 12:02发表的:
qualify row_number() over(partition by name order by billdate asc)=1 images/back.gif
这个语法好先进,学习了 用order by然后limit 1
select name,billno,billdate from BillHead order by billdate limit 1; 引用第1楼vacuumsword于2013-06-18 12:02发表的:
qualify row_number() over(partition by name order by billdate asc)=1 images/back.gif
好高级啊。
select name,billno,min(billdate) from BillHead group by name运行不了?mysql上坨坨的没问题
而且如果你的billdate是顺序的,那么这样搜索billno也应该是对的
如果billdate不是顺序的改成
select name,billno,min(billdate) from (select * from BillHeadorder by billdate) b group by name
也是可以的啊....
qualify row_number()是啥?Teradata SQL? 引用第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 BillHeadorder by billdate) b group by name
....... images/back.gif
对哦,忘了这个是TD自己的了。不过window function真心好用。 引用第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 BillHeadorder by billdate) b group by name
....... images/back.gif
mysql 这样的语法没有错误??billno 不在 group by 子句里啊 select bh1,name,bh1,billdate
from BillHead bh1
where billdatein
(select max(bh2.billdate) from BillHead bh2 where bh2.name = bh1.name group by hb2.name)
group by bh1.name,bh1.billdate
不知道对不对 oracle貌似就要用这种坑爹的方法实现 引用第8楼wowhy于2013-06-18 16:30发表的:
mysql 这样的语法没有错误??billno 不在 group by 子句里啊
images/back.gif
没有,可以正确实现,应该是取第一个bilno 引用第8楼wowhy于2013-06-18 16:30发表的:
mysql 这样的语法没有错误??billno 不在 group by 子句里啊
images/back.gif
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 2楼学习了。
6楼的方法DB2能用么? orderby子句 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可以组装各种需求,现在已经绕不过去了。 引用第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可以组装各种需求,现在已经绕不过去了。 images/back.gif
不等值关联not exists走hash么?性能隐约感觉有问题[小表无所谓 引用第15楼yipansansha于2013-06-19 03:19发表的:
不等值关联not exists走hash么?性能隐约感觉有问题[小表无所谓 images/back.gif
走hash什么意思。?name和billdate做联合索引的话会用到的。
页:
[1]