在SQL查询中需要帮助。请

时间:2014-03-08 16:07:17

标签: sql sql-server sql-server-2008

我有一张这样的桌子。

+----------+-----------+
| UserId   |   Name    |
+----------+-----------+
| 1        |  Asar     |
| 2        |  Bozz     |
+----------+-----------+

+---------+-----------+-------------+
| TestId  |  UserId   |  TestResult |
+---------+-----------+-------------+
| 1       |  1        |  10.0       |
| 2       |  1        |  11.0       |
| 3       |  1        |  33         |
| 4       |  1        |  14.0       |
| 5       |  1        |  16.0       |
+---------+-----------+-------------+

我需要的最终结果是:

+------------+-----------+-----------+-----------+-----------+----------+
| Name       |  Result1  |  Result2  |  Result3  |  Result4  |  Result5 |
+------------+-----------+-----------+-----------+-----------+----------+
| Asar       |  10.0     |  11.0     |  33       |  14.0     |  16.0    |
| Bozz       |  0        |  0        |  0        |  0        |  0       |
+------------+-----------+-----------+-----------+-----------+----------+

2 个答案:

答案 0 :(得分:0)

提供表结构,您正在寻找的SQL查询是:

SELECT Name, TestResult
FROM <TABLE A>, <TABLE B>
WHERE <TABLE A>.UserId = <TABLE B>.UserId
GROUP BY Name

虽然您应该编辑帖子并添加您正在使用的表的名称。希望它有所帮助!

答案 1 :(得分:0)

将2个表命名为tbl1和tbl2,并假设只有5个测试:

select tbl1.name,
       test1.testresult as result1,
       test2.testresult as result2,
       test3.testresult as result3,
       test4.testresult as result4,
       test5.testresult as result5
  from tbl1
  join tbl2 test1
    on test1.userid = tbl1.userid
  join tbl2 test2
    on test2.userid = tbl1.userid
  join tbl2 test3
    on test3.userid = tbl1.userid
  join tbl2 test4
    on test4.userid = tbl1.userid
  join tbl2 test5
    on test5.userid = tbl1.userid
 where test1.testid =
       (select y.testid
          from tbl2 y
         where y.testresult = (select top 1 testresult
                                 from tbl2 x
                                where x.userid = y.userid
                                order by testresult desc))
   and test2.testid =
       (select y.testid
          from tbl2 y
         where y.testresult =
               (select min(testresult)
                  from (select top 2 testresult
                          from tbl2 x
                         where x.userid = y.userid
                         order by testresult desc)))
   and test3.testid =
       (select y.testid
          from tbl2 y
         where y.testresult =
               (select min(testresult)
                  from (select top 3 testresult
                          from tbl2 x
                         where x.userid = y.userid
                         order by testresult desc)))
   and test4.testid =
       (select y.testid
          from tbl2 y
         where y.testresult =
               (select min(testresult)
                  from (select top 4 testresult
                          from tbl2 x
                         where x.userid = y.userid
                         order by testresult desc)))
   and test5.testid =
       (select y.testid
          from tbl2 y
         where y.testresult =
               (select min(testresult)
                  from (select top 5 testresult
                          from tbl2 x
                         where x.userid = y.userid
                         order by testresult desc)))