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 |
|
yumyum113
Starting Member
31 Posts |
Posted - 2010-05-10 : 12:35:50
|
| Hi all,I've been trying to figure out a query but have not been sucessfull for the last two days. Hopefully somebody could help me, I tried looking for similar problem poster here but failed to find one. I appriciate any help in advance. Please see belowdeclare @topictbl table(topicid int,updateid int,topicname varchar(50),topicdetails varchar(100))INSERT INTO @topictblSELECT 1,2,'topic 1','topic 1 details' union allSELECT 2,2,'topic 2','topic 2 details'declare @topicagreement table(topicid int,userid int,understand bit)INSERT INTO @topicagreementSELECT 1,1,1 union allSELECT 2,2,0--result that i want to gettopicid userid understand1 1 12 1 null1 2 null2 2 0--First query i've triedselect t.topicid, ta.userid, ta.understandfrom @topictbl tleft join @topicagreement ta on t.topicid = ta.topicid--2nd query i've triedselect t.topicid, ta.userid, ta.understandfrom @topictbl tfull outer join @topicagreement ta on ta.topicid =t.topicid |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-10 : 13:02:44
|
| [code]SELECT m.topicid,m.userid,ta1.understandFROM(SELECT t.topicid,ta.useridFROM(SELECT DISTINCT topicid FROM @topictbl ) tCROSS JOIN (SELECT DISTINCT userid FROM @topicagreement ) ta)mLEFT JOIN @topicagreement ta1ON ta1.topicid = m.topicidAND ta1.userid = m.userid[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-05-10 : 13:04:31
|
It's not very neat, but...select t.topicid, ta.userid, (SELECT understand FROM @topicagreement s WHERE s.topicid = t.topicid AND s.userid = ta.userid)from @topictbl touter apply (SELECT userid FROM @topicagreement) ta ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-05-10 : 13:04:59
|
| You did not describe the result you want is based on what. So I will guess that you want a row for every topic an user combination. But the users may come from a different table (users) or come from the same table (topicagreement). So can confirm that and decide from where users come. |
 |
|
|
yumyum113
Starting Member
31 Posts |
Posted - 2010-05-10 : 13:21:02
|
Thank you all for taking time to reply. Both visak and dba's answer did give me the result that i'm looking for, i'll try to add more sample data and see if it still comes up with the expected result. Just wish there is a simplier way to do this. Thank you so much.To answer malpashaa, basically topictbl table contains several topic and this topic belong to an update. The topicagreement table contain information if users, understand a topic from topictbl. One user may have read or not read the topic at one time. Thus when a user have not yet read a topic he/she will not be able to reply if he/she understood the topic, in this case he/she will not have a record in topicagreement for that particular topic. Hope i was able to explain it for you. |
 |
|
|
|
|
|
|
|