0

提供的代码工作正常,输出如下表所示

SELECT * FROM
(
SELECT   setsTbl.s_id, setsTbl.setName, trainingTbl.t_date, userProfilesTbl.No + ' ' + userProfilesTbl.surname AS Name, userProfilesTbl.st_id, userProfilesTbl.wa_id, userAssessmentTbl.o_id, row_number() over
(
partition by setsTbl.s_id

                order by trainingTbl.t_date ASC
            ) r
FROM         userProfilesTbl RIGHT OUTER JOIN
                      userAssessmentTbl ON userProfilesTbl.UserId = userAssessmentTbl.UserId RIGHT OUTER JOIN
                      trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id RIGHT OUTER JOIN
                      setsTbl ON trainingTbl.s_id = setsTbl.s_id 

                      )x
WHERE x.r =1


s_id   setName  Name       o_id      
-----  -----   -----      ------
1       100     Barnes      2
2       100     Beardsley   3
3       101     Aldridge    1
4       102     Molby       2
5       102     Whelan      3

我在 ASP.NET 中使用一个数据表,再加上一个 DropDownlist,它控制日期和 2 个字段 userProfilestbl.st 和 userProfilesTbl.wa,它允许我过滤商店并转移人员/用户的工作。

如何将我的过滤器添加到当前子查询中。

这是我正在使用的过滤器/参数

   (userProfilesTbl.st_id=2 AND userProfilesTbl.wa_id=2) AND (DATEPART(yyyy,t_date) = 2013 )
4

1 回答 1

0

您可以在 from 子句之后的子查询中插入此子句。请查看以下查询以获取一些想法:

    SELECT * FROM
(
SELECT   setsTbl.s_id, setsTbl.setName, trainingTbl.t_date, userProfilesTbl.No + ' ' + userProfilesTbl.surname AS Name, userProfilesTbl.st_id, userProfilesTbl.wa_id, userAssessmentTbl.o_id, row_number() over
(
partition by setsTbl.s_id

                order by trainingTbl.t_date ASC
            ) r
FROM         userProfilesTbl RIGHT OUTER JOIN
                      userAssessmentTbl ON userProfilesTbl.UserId = userAssessmentTbl.UserId RIGHT OUTER JOIN
                      trainingTbl ON userAssessmentTbl.tt_id = trainingTbl.tt_id RIGHT OUTER JOIN
                      setsTbl ON trainingTbl.s_id = setsTbl.s_id 

WHERE (userProfilesTbl.st_id=2 AND userProfilesTbl.wa_id=2) AND (DATEPART(yyyy,t_date) = 2013 ))x
WHERE x.r =1
于 2013-10-22T15:56:37.977 回答