Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-11-08 : 12:39:31
|
| Hi All - I have two tables which I'm running a query on. Here are the simplified versions:TABLE #1 - CUSTOMER_ORDERSORDER_ID CUSTOMER_NAME 1234 JANE SMITH1235 JIM DOE1236 JOHN SMITH1237 KEITH ARTHURTABLE #2 - ORDER_DATESORDER ID SHIP_DATE1234 11/01/20111234 11/02/20111235 11/06/20111235 11/07/20111236 10/31/20111237 09/21/20111237 09/22/20111237 09/23/2011The second table has multiple Order IDs since the system generates multiple shipping dates. I'm trying to join the two tables, so that it only outputs unique OrderID based on the earliest Ship_Date. Using the DISTINCT function doesn't fix this, it outputs duplicate ORDER ID rows with different ship dates on each line. Any ideas? SELECT T1.ORDERID, T2.SHIP_DATE FROM CUSTOMER_ORDERS T1LEFT JOIN ORDER_DATES T2 ON T1.ORDER_ID = T2.ORDER_IDI would like the output as follows:ORDER_ID SHIP_DATE1234 11/01/20111235 11/06/20111236 10/31/20111237 09/21/2011 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-08 : 12:42:10
|
| select *frmo customer_orders cleft join (select order_id, ship_date = min(ship_date) from order_dates group by order_id) don c.order_id = d.order_id==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-08 : 12:45:04
|
| [code]SELECT ORDERID,SHIP_DATEFROM(SELECT T1.ORDERID, T2.SHIP_DATE,ROW_NUMBER() OVER (PARTITION BY T1.ORDERID ORDER BY T2.SHIP_DATE) AS Rn FROM CUSTOMER_ORDERS T1LEFT JOIN ORDER_DATES T2 ON T1.ORDER_ID = T2.ORDER_ID)tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-11-08 : 13:12:18
|
| THANK YOU NIGEL !!!!! WORKS AS I HOPED!Thanks anyways Vivisakh! Nigel beat you to it by a few minutes!!! |
 |
|
|
|
|
|