Redian新闻
>
急问一个关于T-SQL的问题,谢谢
avatar
急问一个关于T-SQL的问题,谢谢# Database - 数据库
B*a
1
new to SQL, please help...thanks!
2 tables join, A left join B
A has id column
B has id and date columns
in table B, each id has multiple rows of different dates
I need to get the MAX date for each id
and link back to table A
how to do this in T-SQL...?
thanks a lot...
avatar
B*g
2
group by

【在 B****a 的大作中提到】
: new to SQL, please help...thanks!
: 2 tables join, A left join B
: A has id column
: B has id and date columns
: in table B, each id has multiple rows of different dates
: I need to get the MAX date for each id
: and link back to table A
: how to do this in T-SQL...?
: thanks a lot...

avatar
B*a
3
but I need to select a lot more fields from other tables as well, how to
just select and group by ids from table B and get a new field of MAX date?

【在 B*****g 的大作中提到】
: group by
avatar
B*g
4
why do you want to have group by on table B instead of on table A?

【在 B****a 的大作中提到】
: but I need to select a lot more fields from other tables as well, how to
: just select and group by ids from table B and get a new field of MAX date?

avatar
B*a
5
coz only table B has the date variable I need for each id...

【在 B*****g 的大作中提到】
: why do you want to have group by on table B instead of on table A?
avatar
B*g
6
group by is after join.
below is oracle sql, should be ok for sql server.
SELECT a.id, MAX (b.date)
FROM tab_a a LEFT OUTER JOIN tab_b b ON a.id = b.id
GROUP BY a.id

【在 B****a 的大作中提到】
: coz only table B has the date variable I need for each id...
avatar
B*a
7
谢谢。。。
我试了,但是不work
我一共有三个tables
就是说,A left join B on id
A left join C on seq (another field)
我要B里面的date的MAX
和A里的id,还有C里的一些其他的fields
我晕了。。。

【在 B*****g 的大作中提到】
: group by is after join.
: below is oracle sql, should be ok for sql server.
: SELECT a.id, MAX (b.date)
: FROM tab_a a LEFT OUTER JOIN tab_b b ON a.id = b.id
: GROUP BY a.id

avatar
B*g
8
You did not mention table C before.
SELECT d.ID, d.maxdate, c.*
FROM (SELECT a.id, a.seqno, MAX (b.date) maxdate
FROM tab_a a LEFT OUTER JOIN tab_b b ON a.id = b.id
GROUP BY a.id, a.seqno) d LEFT OUTER JOIN tab_c c ON d.
seqno = c.seqno

【在 B****a 的大作中提到】
: 谢谢。。。
: 我试了,但是不work
: 我一共有三个tables
: 就是说,A left join B on id
: A left join C on seq (another field)
: 我要B里面的date的MAX
: 和A里的id,还有C里的一些其他的fields
: 我晕了。。。

avatar
B*a
9
thanks a lot! but I am a little confused here...the d table in select
statement should be a?

【在 B*****g 的大作中提到】
: You did not mention table C before.
: SELECT d.ID, d.maxdate, c.*
: FROM (SELECT a.id, a.seqno, MAX (b.date) maxdate
: FROM tab_a a LEFT OUTER JOIN tab_b b ON a.id = b.id
: GROUP BY a.id, a.seqno) d LEFT OUTER JOIN tab_c c ON d.
: seqno = c.seqno

avatar
B*g
10
d is the join of a and b

【在 B****a 的大作中提到】
: thanks a lot! but I am a little confused here...the d table in select
: statement should be a?

avatar
B*a
11
I C
thanks a lot! you are so nice! :)

【在 B*****g 的大作中提到】
: d is the join of a and b
avatar
c*e
12
you can do a qualify here
select a.whatever ,
b.whatever
from table_a a
left outer join table_b b
on a.id = b.id
qualify row_number() over (partition by a.id order by b.dt desc) = 1

【在 B****a 的大作中提到】
: but I need to select a lot more fields from other tables as well, how to
: just select and group by ids from table B and get a new field of MAX date?

avatar
m*2
13
我用self join,
SELECT a.*,b.*, c.* (--whatever field you want)
FROM tbla a,tblb b1,tblc c
where a.id = b1.id
and a.seqno = c.seqno
and b1.date =
(SELECT MAX (b2.date) from tblb b2
where b2.id = b1.id
group by b2.id)

【在 B****a 的大作中提到】
: 谢谢。。。
: 我试了,但是不work
: 我一共有三个tables
: 就是说,A left join B on id
: A left join C on seq (another field)
: 我要B里面的date的MAX
: 和A里的id,还有C里的一些其他的fields
: 我晕了。。。

avatar
c*e
14
如果table b里面有不止一个max(date)的话,就会出现多个结果

【在 m**********2 的大作中提到】
: 我用self join,
: SELECT a.*,b.*, c.* (--whatever field you want)
: FROM tbla a,tblb b1,tblc c
: where a.id = b1.id
: and a.seqno = c.seqno
: and b1.date =
: (SELECT MAX (b2.date) from tblb b2
: where b2.id = b1.id
: group by b2.id)

avatar
B*g
15
1. 虽然人家要left join
2. 虽然你的performance更好
但是,原题说。。。
in table B, each id has multiple rows of different dates

【在 c*******e 的大作中提到】
: 如果table b里面有不止一个max(date)的话,就会出现多个结果
avatar
c*e
16
multiple rows of differnt dates我很怀疑LZ具体想说什么

【在 B*****g 的大作中提到】
: 1. 虽然人家要left join
: 2. 虽然你的performance更好
: 但是,原题说。。。
: in table B, each id has multiple rows of different dates

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。