SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to order
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rauken
Posting Yak Master

Sweden
108 Posts

Posted - 08/13/2013 :  07:59:09  Show Profile  Reply with Quote
Hi,

I have a table called DeliveryPosition. To simplify my question I've stripped a number of fields.

Table and data looks like this:

DeliveryPosId Desc PrecisionId
22591 Main1 NULL
22592 Main2 NULL
22872 test1 22591
22873 test2 22591
22874 test11 22592
22875 test22 22592

I want to have DeliveryPosId = 22591 and then the rows with PrecisionId = 22591. Then main row no 2 which is DeliveryPosId = 22592 and the corresponding rows.

I'm stuck! Is there an easy way to do this in t-sql or do I need view, temporary table etc?

/Magnus

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 08/13/2013 :  08:19:07  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT * FROM dbo.Table1
ORDER BY COALESCE(PrecisionID, DeliveryPosID), DeliveryPosID;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Rauken
Posting Yak Master

Sweden
108 Posts

Posted - 08/13/2013 :  08:26:07  Show Profile  Reply with Quote
WOW! Thanks it worked. I forgot about using COALESCE.
Go to Top of Page

Rauken
Posting Yak Master

Sweden
108 Posts

Posted - 08/16/2013 :  07:10:45  Show Profile  Reply with Quote
Hi again, I found a new problem. Sometimes PrecisionId can be 0 instead of NULL then using COALSCE won't work. Any ideas?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 08/16/2013 :  07:12:59  Show Profile  Reply with Quote
ORDER BY COALESCE(NULLIF(PrecisionID,0), DeliveryPosID), DeliveryPosID;


--
Chandu
Go to Top of Page

Rauken
Posting Yak Master

Sweden
108 Posts

Posted - 08/16/2013 :  07:31:28  Show Profile  Reply with Quote
Thanks! It works perfect.
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 08/16/2013 :  07:39:57  Show Profile  Reply with Quote
quote:
Originally posted by Rauken

Thanks! It works perfect.


Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.47 seconds. Powered By: Snitz Forums 2000