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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help with query

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2007-09-05 : 03:21:44
Hi,

I have these 2 queries that return the desired results, is there a way I can combine them into one query that returns all columns on 1 row?

Thanks very much
mike123


SELECT
voteForID AS voteForID_1,
voterID AS VoterID_1,
date AS Date_1,
points AS points_1


FROM tblUserVote WHERE (voteForID = 500 and voterID = 1000)


SELECT
voteForID AS voteForID_2,
voterID AS VoterID_2,
date AS Date_2,
points AS points_2

FROM tblUserVote WHERE (voteForID = 100 and voterID = 500)

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2007-09-05 : 03:29:26
SELECT
A.voteForID AS voteForID_1,
A.voterID AS VoterID_1,
A.date AS Date_1,
A.points AS points_1,
B.voteForID AS voteForID_2,
B.voterID AS VoterID_2,
B.date AS Date_2,
B.points AS points_2

FROM tblUserVote A, tblUserVote B

WHERE (A.voteForID = 500 and A.voterID = 1000) and (B.voteForID = 100 and B.voterID = 500)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-05 : 03:51:46
You got the same answer here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=88718
five days ago...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2007-09-05 : 16:07:16
Hey Peso,

I thought the query you gave me worked perfectly, but after playing with some different data it didnt. I think my initial explanation was just over complicating things. I posted a new thread to not be confusing, this new query given by Koji Matsumura works perfectly.

Sorry for the confusion , and thanks for all the help!

cheers,
mike123
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-05 : 16:16:54
Why did it fail? What was the result?
If you get duplicate records, add a DISTINCT.

You can then make my suggestion as a view too!



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2007-09-05 : 18:16:30
Hey Peso,

The results were correct, but it brought back duplicate records sometimes. I figured it wasnt the correct approach, but great to know I could have fixed it completely with a DISTINCT. It was kinda over my head so I thought I was barkin up the wrong tree :)

Thanks again!
mike123
Go to Top of Page
   

- Advertisement -