基动战士 发表于 2013-6-18 11:58

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

vacuumsword 发表于 2013-6-18 12:02

qualify row_number() over(partition by name order by billdate asc)=1

billrice 发表于 2013-6-18 12:11

select * from BillHead where name||billdate   in   (select name||min(billdate) from BillHead group by name )

billrice 发表于 2013-6-18 12:16

引用第1楼vacuumsword于2013-06-18 12:02发表的:
qualify row_number() over(partition by name order by billdate asc)=1 images/back.gif

这个语法好先进,学习了

mimighost 发表于 2013-6-18 12:19

用order by然后limit 1

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

shahito 发表于 2013-6-18 13:36

引用第1楼vacuumsword于2013-06-18 12:02发表的:
qualify row_number() over(partition by name order by billdate asc)=1 images/back.gif


好高级啊。

赤色彗星SE 发表于 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 BillHeadorder by billdate) b group by name
也是可以的啊....

qualify row_number()是啥?Teradata SQL?

vacuumsword 发表于 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 BillHeadorder by billdate) b group by name
....... images/back.gif


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

wowhy 发表于 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 BillHeadorder by billdate) b group by name
....... images/back.gif


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

lolface 发表于 2013-6-18 20:00

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貌似就要用这种坑爹的方法实现

赤色彗星SE 发表于 2013-6-18 20:44

引用第8楼wowhy于2013-06-18 16:30发表的:


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




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

char1st 发表于 2013-6-19 00:42

引用第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

yipansansha 发表于 2013-6-19 01:11

2楼学习了。

6楼的方法DB2能用么?

全家 发表于 2013-6-19 01:35

orderby子句

Yurita 发表于 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可以组装各种需求,现在已经绕不过去了。

yipansansha 发表于 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可以组装各种需求,现在已经绕不过去了。 images/back.gif


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

Yurita 发表于 2013-6-19 03:27

引用第15楼yipansansha于2013-06-19 03:19发表的:


不等值关联not exists走hash么?性能隐约感觉有问题[小表无所谓 images/back.gif

走hash什么意思。?name和billdate做联合索引的话会用到的。

xclin163 发表于 2013-6-19 10:28

页: [1]
查看完整版本: SQL简单问题求解,大家帮帮忙