我正在创建一个视图,但是我需要执行另一个选择来填充该视图的一个列。以下是我要用于视图的查询:
SELECT JOB, REL, PHASE, AVG(TCOUNT) AS AVGCOUNT, @NUMFRAMES AS NUMFRAMES, @NUMFRAMES/AVG(TCOUNT) AS REMAIN_DAYS
FROM
(SELECT
vwFRAME_TOTALS.JOB
,vwFRAME_TOTALS.REL
,vwFRAME_TOTALS.PHASE
,vwFRAME_TOTALS.[DAY]
,COUNT(vwFRAME_TOTALS.FR_ID) AS TCOUNT
FROM
vwFRAME_TOTALS
GROUP BY JOB,REL,PHASE,DAY)
AS COUNTS
GROUP BY JOB, REL, PHASE
和@NUMFRAMES来自另一个SELECT
,如下所示:
SELECT @NUMFRAMES=count(*) FROM dbo.WFR
我怎样才能在一个声明中做到这一点?
答案 0 :(得分:1)
SELECT JOB, REL, PHASE, AVG(TCOUNT) AS AVGCOUNT, (SELECT count(*) FROM dbo.WFR) AS NUMFRAMES, (SELECT count(*) FROM dbo.WFR)/AVG(TCOUNT) AS REMAIN_DAYS
FROM
(SELECT
vwFRAME_TOTALS.JOB
,vwFRAME_TOTALS.REL
,vwFRAME_TOTALS.PHASE
,vwFRAME_TOTALS.[DAY]
,COUNT(vwFRAME_TOTALS.FR_ID) AS TCOUNT
FROM
vwFRAME_TOTALS
GROUP BY JOB,REL,PHASE,DAY)
AS COUNTS
GROUP BY JOB, REL, PHASE
答案 1 :(得分:0)
我很难测试,因为我没有你的架构,但是这样的工作有用吗?
SELECT JOB, REL, PHASE, AVG(TCOUNT) AS AVGCOUNT, NUMFRAMES, @NUMFRAMES/AVG(TCOUNT) AS REMAIN_DAYS
FROM
(SELECT
vwFRAME_TOTALS.JOB
,vwFRAME_TOTALS.REL
,vwFRAME_TOTALS.PHASE
,vwFRAME_TOTALS.[DAY]
,COUNT(vwFRAME_TOTALS.FR_ID) AS TCOUNT
FROM
vwFRAME_TOTALS
GROUP BY JOB,REL,PHASE,DAY)
AS COUNTS
INNER JOIN (SELECT count(*) as NUMFRAMES FROM dbo.WFR) AS FRAMES
ON 1=1
GROUP BY JOB, REL, PHASE