【发布时间】:2010-10-18 08:33:45
【问题描述】:
我有 2 个表格要以特定方式加入。我认为我的查询是正确的,但不确定。
select t1.userID, t3.Answer Unit, t5.Answer Demo
FROM
table1 t1
inner join (select * from table2) t3 ON t1.userID = t3.userID
inner join (select * from table2) t5 ON t1.userID = t5.userID
where
NOT EXISTS (SELECT * FROM table1 t2 WHERE t2.userID = t1.userID AND t2.date > t1.date)
and NOT EXISTS (SELECT * FROM table2 t4 WHERE t4.userID = t3.userID and t4.counter > t3.counter)
and NOT EXISTS (SELECT * FROM table2 t6 WHERE t6.userID = t5.userID and t6.counter > t5.counter)
and t1.date_submitted >'1/1/2009'
and t3.question = Unit
and t5.question = Demo
order by
t1.userID
从 table1 我想要不同的用户 ID,其中日期 > 1/1/2009
table1
userID Date
1 1/2/2009
1 1/2/2009
2 1/2/2009
3 1/2/2009
4 1/1/2008
所以我想从 table1 得到的结果应该是这样的:
userID
1
2
3
然后我想在 userID 上用 table2 加入这个,看起来像这样:
table2
userID question answer counter
1 Unit A 1
1 Demo x 1
1 Prod 100 1
2 Unit B 1
2 Demo Y 1
3 Prod 100 1
4 Unit A 1
1 Unit B 2
1 Demo x 2
1 Prod 100 2
2 Unit B 2
2 Demo Z 2
3 Prod 100 2
4 Unit A 2
我想用这个结果将 table1 与 table2 连接起来:
userID Unit Demo
1 B X
2 B Z
换句话说,
从 table2 中选择不同的用户 ID,其中问题 = 最高计数器的单位
然后
从 table2 中选择不同的 userID,其中 question = Demo 为最高计数器。
我认为我所做的是 3 个自连接,然后将这 3 个连接在一起。
你觉得对吗?
【问题讨论】:
-
“你觉得对吗?” - 你运行它时它给出了正确的答案吗?
-
我认为它给了我正确的答案,我只是想知道你是否能看出它有什么问题。我们使用的是 SQL 2005。谢谢
-
我的查询肯定不对。还是有问题。
标签: sql sql-server sql-server-2005 join