Friday, 15 June 2012

sql server - Select top N with joins -



sql server - Select top N with joins -

i looking bring together 2 tables top n results of other table explained below.

orderheader

oh_id orderdate ---------------------- 1 2014-06-01 2 2014-06-02 3 2014-06-03 4 2014-06-04 5 2014-06-05

orderproducts

op_id oh_id quantity ------------------------------ 1 1 1 2 1 2 3 2 1 4 3 3 5 4 4 6 4 1 7 4 2 8 5 2 9 5 1

i expecting result top 3 orders (4 rows).

oh_id orderdate op_id quantity ------------------------------------------------ 1 2014-06-01 1 1 1 2014-06-01 2 2 2 2014-06-02 3 1 3 2014-06-03 4 3

note: looking bring together 2 tables rather writing sp or looped queries.

select top 3 o.oh_id, o.orderdate, oo.op_id, oo.quantity orderheader o bring together orderproducts oo on o.oh_id = oo.oh_id

sql-server

No comments:

Post a Comment