我有3个表,我试图从中获取信息: 表1为c, 表2为s, 表3为sr
我正在尝试显示表1中的名称和描述,最后一次来自表2,以及表3中step_id的数量,状态= 2
结束目标:
|c.Name|c.Description|s.EXecuted_On|Total Count(STEP_ID) from sr where status = 2|
这是我的问题:
SELECT c.NAME,
c.DESCRIPTION,
MAX(s.EXECUTED_ON),
(select COUNT(sr.STEP_ID) from sr WHERE STATUS = '2')
WHERE s.CYCLE_ID = c.ID
AND s.ID = sr.SCHEDULE_ID
GROUP BY c.NAME,c.DESCRIPTION
ORDER BY c.NAME
我在count列得到相同的计数,可能需要一个内部联接,但无法弄明白。
感谢。
答案 0 :(得分:0)
我认为这就是你所追求的目标:
SELECT c.NAME, c.DESCRIPTION, MAX(s.EXECUTED_ON), COUNT(sr.STEP_ID) as STEP_ID
FROM table1 c, table2 s, table3 sr
WHERE s.CYCLE_ID = c.ID
AND sr.ID = c.CYCLE_ID
AND STATUS = '2'
AND s.ID = sr.SCHEDULE_ID
GROUP BY c.NAME,c.DESCRIPTION
ORDER BY c.NAME
答案 1 :(得分:0)
目前还不清楚你想要什么,但也许这可以帮助你。
SELECT
c.Name,
c.Description,
MAX(s.EXecuted_On) as MaxEXecuted_On,
Count(CASE WHEN sr.status = 2 THEN sr.STEP_ID END) as TotalCount
FROM table1 as c
LEFT JOIN table2 as s
ON s.CYCLE_ID = c.ID
LEFT JOIN table3 as sr
ON s.ID = sr.SCHEDULE_ID
GROUP BY c.ID