您好,我希望这是一个有趣的问题(确切地说是几个问题),我希望有人可以帮助我。我目前正在使用Crystal Reports 2008中的SQL,它从Oracle数据库获取数据。
我需要获得一份工作人员名单,这些工作人员在给定的时间内完成了一定数量的测试,比如超过10次测试。为了进一步解决这个问题,它必须是最近的10个测试,我还需要一个每个独特测试参考的列表。
我已经开始使用ROW NUMBER()函数开始(参见第一段代码及其输出),但这仅显示单个员工编号的10条最新信息,并且不会检查是否需要10测试已经进行。
我还制作了另一段代码(见第二段代码及其输出),其中包含了一个count函数,并且只显示了至少进行了10次测试的员工。
我真正需要的内容显示在Required Output表中:
所以我的问题是:
有没有办法修改我的原始代码,以便它可以使用多个员工编号?
有没有办法合并两段代码,以便检查所需的测试数量是否已经完成,并且只显示符合所需条件的员工编号和测试参考(这是首选选项)
我是否必须废弃这个想法并重新开始?
不幸的是,我目前没有管理员访问数据库的权限,也无法创建任何新数据表来存储任何这些数据,因此我需要在单个SQL操作中完成此操作,如果可能的话。
我附上了一个电子表格,如果有帮助的话,会显示可用数据的快照。
你可以给我任何帮助,我们将不胜感激。
WITH TESTNUMBER AS
(SELECT TESTER.STAFF_NO, TESTER.TEST_REF, PERIOD. DATE_OF_TEST, TESTSLOT.SLOT_TIME,
ROW NUMBER() OVER (ORDER BY TESTER.STAFF_NO DESC, PERIOD.DATE_OF_TEST DESC,
TESTSLOT.SLOT_TIME DESC)
AS RowNumber
FROM dB1.TEST_EVENT TESTER
INNER JOIN dB1.PERIOD PERIOD ON TESTER.PERIOD_ID=PERIOD.PERIOD_ID
INNER JOIN dB1.TEST_ SLOT_TIME TESTSLOT ON TESTER.SLOT_TIME_ID=TESTSLOT.SLOT_TIME_ID)
WHERE TESTER.STAFF_NO=405405 AND
(PERIOD.DATE_OF_TEST>={ts '2013-08-01 00:00:00'} AND
PERIOD.DATE_OF_TEST<={ts '2013-08-10 00:00:00'})
SELECT * FROM TESTNUMBER
WHERE RowNumber BETWEEN 1 AND 10;`
STAFF_NO TEST_REF DATE_OF_TEST SLOT_TIME
405405 01 05/08/2013 08:40:00
405405 02 05/08/2013 09:40:00
405405 03 05/08/2013 10:40:00
405405 04 06/08/2013 08:40:00
405405 05 06/08/2013 09:40:00
405405 06 06/08/2013 10:40:00
405405 07 06/08/2013 11:40:00
405405 08 07/08/2013 08:40:00
405405 09 07/08/2013 09:40:00
405405 10 07/08/2013 10:40:00
查询:
SELECT TESTER.STAFF_NO, count(TESTER.STAFF_NO) AS TOTALTESTS FROM
FROM dB1.TEST_EVENT TESTER
INNER JOIN dB1.PERIOD PERIOD ON TESTER.PERIOD_ID=PERIOD.PERIOD_ID
INNER JOIN dB1.TEST_ SLOT_TIME TESTSLOT ON TESTER.SLOT_TIME_ID=TESTSLOT.SLOT_TIME_ID)
WHERE (PERIOD.DATE_OF_TEST>={ts '2013-08-01 00:00:00'} AND PERIOD.DATE_OF_TEST<=
{ts '2013-08-10 00:00:00'})
GROUP BY TESTER.STAFF_NO
HAVING count(TESTER.STAFF_NO)>=10
ORDER BY TESTER.STAFF_NO DESC`
STAFF_NO TEST_REF
405405 12
366255 14
总体要求输出
STAFF_NO TEST_REF DATE_OF_TEST SLOT_TIME
405405 03 05/08/2013 10:40:00
405405 04 06/08/2013 08:40:00
405405 05 06/08/2013 09:40:00
405405 06 06/08/2013 10:40:00
405405 07 06/08/2013 11:40:00
405405 08 07/08/2013 08:40:00
405405 09 07/08/2013 09:40:00
405405 10 07/08/2013 10:40:00
405405 11 08/08/2013 08:40:00
405405 12 08/08/2013 09:40:00
366255 17 06/08/2013 09:40:00
366255 18 06/08/2013 10:40:00
366255 19 06/08/2013 11:40:00
366255 20 06/08/2013 12:40:00
366255 21 09/08/2013 08:40:00
366255 22 09/08/2013 09:40:00
366255 23 09/08/2013 10:40:00
366255 24 09/08/2013 12:40:00
366255 25 09/08/2013 14:40:00
366255 26 09/08/2013 15:40:00
很抱歉,如果这看起来不太专业,但我是新手。
再次感谢您的帮助。
答案 0 :(得分:0)
除了合并版本的两个查询之外什么都没有。
问题#1
WITH TESTNUMBER AS
(SELECT TESTER.STAFF_NO, TESTER.TEST_REF, PERIOD. DATE_OF_TEST, TESTSLOT.SLOT_TIME,
ROW NUMBER() OVER (PARTITION BY TESTER.STAFF_NO ORDER BY TESTER.STAFF_NO DESC, PERIOD.DATE_OF_TEST DESC,
TESTSLOT.SLOT_TIME DESC)
AS RowNumber
FROM dB1.TEST_EVENT TESTER
INNER JOIN dB1.PERIOD PERIOD ON TESTER.PERIOD_ID=PERIOD.PERIOD_ID
INNER JOIN dB1.TEST_ SLOT_TIME TESTSLOT ON TESTER.SLOT_TIME_ID=TESTSLOT.SLOT_TIME_ID)
WHERE TESTER.STAFF_NO in (405405, 366255) AND
(PERIOD.DATE_OF_TEST>={ts '2013-08-01 00:00:00'} AND
PERIOD.DATE_OF_TEST<={ts '2013-08-10 00:00:00'})
SELECT * FROM TESTNUMBER
WHERE RowNumber BETWEEN 1 AND 10
ORDER BY STAFF_NO,RowNumber
问题#2
WITH TESTNUMBER AS
(
SELECT TESTER.STAFF_NO, TESTER.TEST_REF, PERIOD. DATE_OF_TEST, TESTSLOT.SLOT_TIME,
ROW NUMBER() OVER (PARTITION BY TESTER.STAFF_NO ORDER BY TESTER.STAFF_NO DESC, PERIOD.DATE_OF_TEST DESC,
TESTSLOT.SLOT_TIME DESC)
AS RowNumber
FROM dB1.TEST_EVENT TESTER
INNER JOIN dB1.PERIOD PERIOD ON TESTER.PERIOD_ID=PERIOD.PERIOD_ID
INNER JOIN dB1.TEST_ SLOT_TIME TESTSLOT ON TESTER.SLOT_TIME_ID=TESTSLOT.SLOT_TIME_ID)
WHERE TESTER.STAFF_NO in (
SELECT TESTER.STAFF_NO FROM
FROM dB1.TEST_EVENT TESTER
INNER JOIN dB1.PERIOD PERIOD
ON TESTER.PERIOD_ID = PERIOD.PERIOD_ID
INNER JOIN dB1.TEST_ SLOT_TIME TESTSLOT
ON TESTER.SLOT_TIME_ID = TESTSLOT.SLOT_TIME_ID)
WHERE (PERIOD.DATE_OF_TEST >= {ts '2013-08-01 00:00:00'}
AND PERIOD.DATE_OF_TEST <= {ts '2013-08-10 00:00:00'})
GROUP BY TESTER.STAFF_NO
HAVING count(TESTER.STAFF_NO)>=10
)
AND (PERIOD.DATE_OF_TEST>={ts '2013-08-01 00:00:00'}
AND PERIOD.DATE_OF_TEST<={ts '2013-08-10 00:00:00'}
)
SELECT * FROM TESTNUMBER
WHERE RowNumber BETWEEN 1 AND 10
ORDER BY STAFF_NO,RowNumber