SQL, the seqnum order became different after table join

Hi , Expert,

After I join #a and "makemydeal_com..dealresponse2",  the "seqnum" shows 3,1,2,4,5,6. How can I make it to 1,2,3,4,5,6.
I tried left join, right join, inner join . Also, I tried a.dealresponseid desc, a.timestamp desc and a.seqnum desc.  It still returns the wrong seqnum order. Thank you.

# A

declare @StartDate date
declare @EndDate date
set @StartDate ='2015-06-1'
set @EndDate ='2015-7-14'

select dealid, seqnum, timestamp, dealresponseid,
case when ShopperResponseStatus in ('Sent','Accepted','Archived') then 'Shopper' else 'Dealer' end as Actor
into #a
from mmd_feed..dealresponsesequence 
where dealid=30357
order by seqnum asc


#B
select a.*,  dr.sentbydealercontactid
into #b
from #a a 
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
order by a.dealresponseid desc

Open in new window


Capture.JPG
tanj1035Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Just use an appropriate ORDER BY in your second query:

#B
select a.*,  dr.sentbydealercontactid
into #b
from #a a 
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
order by seqnum desc

Open in new window

0
Deepak ChauhanSQL Server DBACommented:
You want it ASC order why are you using desc ?

#B
select a.*,  dr.sentbydealercontactid
into #b
from #a a
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
order by a.seqnum desc

Or

select * into #b from (
select a.*,  dr.sentbydealercontactid
from #a a
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
) T order by seqnum ASC
0
tanj1035Author Commented:
Hi Deepark, Sten5an, Sorry, desc is a type error.

I tried both of your quries. but the seqnum order still returns 3,1,2,4,5,6. Do you have any suggestions? Thanks.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Deepak ChauhanSQL Server DBACommented:
Did you tried this.

select * into #b from (
 select a.*,  dr.sentbydealercontactid 
from #a a 
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
 ) T order by seqnum ASC 

Open in new window

0
tanj1035Author Commented:
Yes, I did, Deepak, it returns 3,1,2,4,5,6.
0
ste5anSenior DeveloperCommented:
Yup, haven't read your post carfullly enough. Your second query is a tabel creation query.

Thus you have not posted the query which retrieves the data. Without ORDER BY in the query which retrieves the data, there is no guarantee, that the data is returned in any predictible order.
0
tanj1035Author Commented:
hi Ste5an,

Please correct me if I understand correctly. Do you mean I should change the query b with a correct syntax "asc", like this

select a.*,  dr.sentbydealercontactid
into #b
from #a a
left join makemydeal_com..dealresponse2 dr on a.dealid=dr.dealid and a.dealresponseid=dr.dealresponseid
order by seqnum asc

If so, #b still returns me a wrong seqnum order
0
ste5anSenior DeveloperCommented:
No. This query creates the table #b. The ORDER BY is here irrelevant.

To get the order right you need a simple

SELECT * FROM #b ORDER BY seqnum;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tanj1035Author Commented:
Yes, you are right. Thx for sharing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.