我需要使用相同的CLIENT,DATE和STATUS创建一个查询来执行下一个查询,以选择最大执行日期并对其进行计数。我想通过一个例子,它将更容易理解。
以此表为例,
Client Status Date Date Execution
------- ---------- ------ ---------------------
AB 100 22/07/2014 23/07/2014 21:35
AB 100 22/07/2014 23/07/2014 05:00
AB 100 22/07/2014 23/07/2014 19:32
AB 100 23/07/2014 24/07/2014 04:15
AB 100 23/07/2014 24/07/2014 20:19
BC 400 24/07/2014 25/07/2014 20:56
BC 400 24/07/2014 25/07/2014 03:36
BC 400 24/07/2014 25/07/2014 17:58
BC 400 24/07/2014 25/07/2014 14:51
我想获得,
Client Status Date Date Execution COUNT(Distint)
------- ---------- ------ --------------------- ----------------------
AB 100 22/07/2014 23/07/2014 21:35 3
AB 100 23/07/2014 24/07/2014 20:19 2
BC 400 24/07/2014 25/07/2014 20:56 4
目前我执行此查询以查找最大日期执行,但我不知道如何为COUNT(DISTINCT)执行操作。
SELECT ip.CLIENT, cps.DATE, cpst.STATUS, cpst.DATE_EXE
FROM IP ip, CPS cps, CPST cpst INNER JOIN (SELECT PF_GUID, MAX(DATE_EXE)
FROM CTR_PORTFOLIOS_TRG
GROUP BY PF_GUID) cpst2 ON cpst.PF_GUID=cpst2.PF_GUID
WHERE ip.PF_ID = cps.PF_ID AND cps.PF_GUID = cpst.PF_GUID
AND cpst.STATUS IN (100,400,550)
AND cps.DATE BETWEEN TO_DATE('01/07/2014', 'DD/MM/YYYY') AND TO_DATE('30/09/2014', 'DD/MM/YYYY')
ORDER BY CLIENT, STATUS, DATE
有任何帮助请完成查询吗?非常感谢你
答案 0 :(得分:1)
SELECT Client,
[Status],
[Date],
MAX([Date Execution]) [Date Execution],
COUNT(*) [Count(Distinct)]
FROM YourTable
GROUP BY Client,
[Status],
[Date]
答案 1 :(得分:1)
如果我理解正确,这是一个基本的聚合查询。我想你可能会困惑自己:
SELECT ip.CLIENT, cpst.STATUS, MIN(cps.date), MAX(cpst.DATE_EXE), COUNT(*)
FROM IP ip JOIN
CPS cps
ON ip.PF_ID = cps.PF_ID JOIN
CPST cpst
ON cps.PF_GUID = cpst.PF_GUID
WHERE cpst.STATUS IN (100,400,550) AND
cps.DATE BETWEEN DATE '2014-07-01' and DATE '2014-09-30'
GROUP BY ip.CLIENT, cps.STATUS;
注意:
from
子句中使用逗号。DATE
标识符作为常量。MAX()
获取最新的日期/时间。COUNT(*)
获取计数。我不确定distinct
的含义,但对于示例结果,count(*)
就足够了。