我有2个数据集包含我想要链接在一起的不同数据。下面的两个表达式给出了我自己的正确值,但我没有成功地将它们组合起来。
表达式1:
=SUM(IIF(Fields!Staff.Value = "Trainee",1,0), "Dataset1")
表达式2:
=SUM(IIF(Fields!Grade.Value = "Pass" OR Fields!Grade.Value = "Achieved", 1, 0), "Dataset2")
我正在努力获得已经获得“通过”或“达到”等级的学员人数。两个数据集之间有一个共同字段,可用于将它们链接在一起。 最终,第一个数据集将拉出所有受训者,第二个数据集将计算他们获得的传递次数。
我不确定最好的方法是通过表达式(如上所述)还是通过查询。如果查询最好,我不知道该怎么做。任何有关这方面的帮助将不胜感激。
修改
以下是正在使用的两个查询。
查询1:
SELECT
EMP.FULLNAME,
EMP.EMP_ID,
WS_EVENT_STATUS.BRANCH AS "Department",
WS_EVENT_STATUS.STATUS AS "Competency Status"
FROM
EMP
INNER JOIN
EMPEVENT ON EMP.EMP_ID = EMPEVENT.EMP_ID
INNER JOIN
WS_PERSONAL_DETAIL ON EMP.EMP_ID = WS_PERSONAL_DETAIL.EMP_ID
LEFT OUTER JOIN
WS_EVENT_STATUS ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_STATUS.EVENT_SEQ_NO
WHERE
(EMPEVENT.EVENT_ID = 'STATUS')
查询2:
SELECT
EMP.EMP_ID,
EMPEVENT.EVENT_ID,
concat( WS_EVENT_TESTFORM1.ASSDATE, WS_EVENT_TESTFORM2.ASSDATE) AS "Assessment Date",
concat( WS_EVENT_TESTFORM1.STFNAME, WS_EVENT_TESTFORM2.STFNAME) AS "Staff Name",
concat( WS_EVENT_TESTFORM1.GRADE, WS_EVENT_TESTFORM2.GRADE) AS "Grade"
FROM
EMP
INNER JOIN
EMPEVENT ON EMP.EMP_ID = EMPEVENT.EMP_ID
INNER JOIN
WS_PERSONAL_DETAIL ON EMP.EMP_ID = WS_PERSONAL_DETAIL.EMP_ID
LEFT OUTER JOIN
WS_EVENT_TESTFORM1 ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_TESTFORM1.EVENT_SEQ_NO
LEFT OUTER JOIN
WS_EVENT_TESTFORM2 ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_TESTFORM2.EVENT_SEQ_NO
WHERE
EMP.EMP_ID = EMPEVENT.EMP_ID AND
((EMPEVENT.EVENT_ID = 'TESTFORM1') OR
(EMPEVENT.EVENT_ID = 'TESTFORM2'))
AND
((WS_EVENT_TESTFORM1.ASSDATE BETWEEN @StartDate AND @EndDate) OR
(WS_EVENT_TESTFORM2.ASSDATE BETWEEN @StartDate AND @EndDate))
答案 0 :(得分:0)
我无法对此进行测试,因为我不知道您的数据表是什么样的,但我认为这应该可以在一次查询中为您提供所有数据。
SELECT
EMP.FULLNAME,
EMP.EMP_ID,
EMPEVENT.EVENT_ID,
WS_EVENT_STATUS.BRANCH AS "Department",
WS_EVENT_STATUS.STATUS AS "Competency Status",
concat( WS_EVENT_TESTFORM1.ASSDATE, WS_EVENT_TESTFORM2.ASSDATE) AS "Assessment Date",
concat( WS_EVENT_TESTFORM1.STFNAME, WS_EVENT_TESTFORM2.STFNAME) AS "Staff Name",
concat( WS_EVENT_TESTFORM1.GRADE, WS_EVENT_TESTFORM2.GRADE) AS "Grade"
FROM
EMP
INNER JOIN
EMPEVENT ON EMP.EMP_ID = EMPEVENT.EMP_ID
INNER JOIN
WS_PERSONAL_DETAIL ON EMP.EMP_ID = WS_PERSONAL_DETAIL.EMP_ID
LEFT OUTER JOIN
WS_EVENT_TESTFORM1 ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_TESTFORM1.EVENT_SEQ_NO
LEFT OUTER JOIN
WS_EVENT_TESTFORM2 ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_TESTFORM2.EVENT_SEQ_NO
LEFT OUTER JOIN
WS_EVENT_STATUS ON EMPEVENT.EVENT_SEQ_NO = WS_EVENT_STATUS.EVENT_SEQ_NO
WHERE
EMP.EMP_ID = EMPEVENT.EMP_ID AND
((EMPEVENT.EVENT_ID = 'TESTFORM1') OR
(EMPEVENT.EVENT_ID = 'TESTFORM2'))
AND
((WS_EVENT_TESTFORM1.ASSDATE BETWEEN @StartDate AND @EndDate) OR
(WS_EVENT_TESTFORM2.ASSDATE BETWEEN @StartDate AND @EndDate))
AND
(EMPEVENT.EVENT_ID = 'STATUS')
你可以做很多事情来清理这个查询,但这是个人的事情。比如...
AND ((EMPEVENT.EVENT_ID = 'TESTFORM1') OR (EMPEVENT.EVENT_ID = 'TESTFORM2'))
变为AND EMPEVENT.EVENT_ID IN('TESTFORM1', 'TESTFORM2')
更新:由于上述方法无效,我无法说明为什么我无法看到您的数据,请尝试以下操作。
SELECT a.FullName, a.Emp_ID, a.Department, a.[Competency Status]
, b.EVENT_ID, b.[Assesment Date], b.[Staff name], b.Grade
FROM (
SELECT
e.FULLNAME,
e.EMP_ID,
es.BRANCH AS "Department",
es.STATUS AS "Competency Status"
FROM EMP e
INNER JOIN EMPEVENT ee ON e.EMP_ID = ee.EMP_ID
INNER JOIN WS_PERSONAL_DETAIL pd ON e.EMP_ID = pd.EMP_ID
LEFT OUTER JOIN WS_EVENT_STATUS es ON ee.EVENT_SEQ_NO = es.EVENT_SEQ_NO
WHERE (ee.EVENT_ID = 'STATUS')
) a
LEFT JOIN
(
SELECT
e.EMP_ID,
ee.EVENT_ID,
concat( t1.ASSDATE, t2.ASSDATE) AS "Assessment Date",
concat( t1.STFNAME, t2.STFNAME) AS "Staff Name",
concat( t1.GRADE, t2.GRADE) AS "Grade"
FROM EMP e
INNER JOIN EMPEVENT ee ON e.EMP_ID = ee.EMP_ID
INNER JOIN WS_PERSONAL_DETAIL pd ON e.EMP_ID = pd.EMP_ID
LEFT OUTER JOIN WS_EVENT_TESTFORM1 t1 ON ee.EVENT_SEQ_NO = t1.EVENT_SEQ_NO
LEFT OUTER JOIN WS_EVENT_TESTFORM2 t2 ON ee.EVENT_SEQ_NO = t2.EVENT_SEQ_NO
WHERE
ee.EVENT_ID IN ('TESTFORM1', 'TESTFORM2')
AND (
(t1.ASSDATE BETWEEN @StartDate AND @EndDate) OR
(t2.ASSDATE BETWEEN @StartDate AND @EndDate)
)
) b
ON a.EMP_ID = b.EMP_ID
基本上运行两个查询,然后通过EMP_ID
加入它们