0

我有两个来自两个不同表的选择语句和一个公共连接字段一个日期字段一个日期字段名称是 JobFinishedDate,它来自 MyJobs 表,另一个日期字段名称是 createddate,它来自 DailyFinishJobsHistory 表

这是我的两个独立的sql

sql 1

SELECT  CONVERT(varchar,createddate,101) as [Date],
                                SUM([No of Jobs]) as [No of Jobs],
                                SUM([Turnaround Time]) as [Turnaround Time],
                                SUM([One Day Per]) as [One Day Per],
                                ROUND((SUM([No of Jobs]) * SUM([Turnaround Time])),2) as [TOTAL hours]

from DailyFinishJobsHistory d
WHERE CONVERT(varchar,createddate,112)>='20131015' AND CONVERT(varchar,createddate,112)<='20131021' 
group by createddate

sql 2

Select 
  convert(varchar,JobFinishedDate,112) as JobFinishedDate
, Count(*) [Number of techs]
From
(
    select 
    convert(varchar,JobFinishedDate,112) JobFinishedDate,Specialist
    from MyJobs
    where convert(varchar,JobFinishedDate,112)>='20131015' 
    and convert(varchar,JobFinishedDate,112) <='20131021'  
    and JobState='FINISHED'
    group by convert(varchar,JobFinishedDate,112) , Specialist
) t1    
Group By 
  JobFinishedDate

现在我必须加入。我尝试过这种方式,但我并没有想到正确的语法来加入这种方式。

SELECT  CONVERT(varchar,createddate,101) as [Date],
                                SUM([No of Jobs]) as [No of Jobs],
                                SUM([Turnaround Time]) as [Turnaround Time],
                                SUM([One Day Per]) as [One Day Per],
                                ROUND((SUM([No of Jobs]) * SUM([Turnaround Time])),2) as [TOTAL hours],
(
Select 
  convert(varchar,JobFinishedDate,112) as JobFinishedDate
, Count(*) [Number of techs]
From
(
    select 
    convert(varchar,JobFinishedDate,112) JobFinishedDate,Specialist
    from bbajobs
    where convert(varchar,JobFinishedDate,112)>='20131015' 
    and convert(varchar,JobFinishedDate,112) <='20131021'  
    and JobState='FINISHED'
    group by convert(varchar,JobFinishedDate,112) , Specialist
) t1    
Group By 
  JobFinishedDate
) a
from DailyFinishJobsHistory d
WHERE CONVERT(varchar,createddate,112)>='20131015' AND CONVERT(varchar,createddate,112)<='20131021' 
group by createddate

所以请帮我建立连接。谢谢

编辑

我也尝试了这个脚本,但 [技术人数] 变得 NULL

    SELECT  CONVERT(varchar,createddate,101) as [Date],
            SUM([No of Jobs]) as [No of Jobs],
            SUM([Turnaround Time]) as [Turnaround Time],
            SUM([One Day Per]) as [One Day Per],
            ROUND((SUM([No of Jobs]) * SUM([Turnaround Time])),2) as [TOTAL hours],
(
        SELECT 
        --CONVERT(varchar,JobFinishedDate,112) as JobFinishedDate,
        COUNT(*) [Number of techs]
    From
    (
        SELECT  CONVERT(varchar,JobFinishedDate,112) JobFinishedDate,
                Specialist
        FROM bbajobs
        WHERE d.createddate=JobFinishedDate AND
        JobState = 'FINISHED'  
        GROUP BY CONVERT(varchar,JobFinishedDate,112), Specialist
    ) t1    
    GROUP BY JobFinishedDate
) [Number of techs]


    FROM DailyFinishJobsHistory d
    WHERE CONVERT(varchar,createddate,112) >= '20131015' AND CONVERT(varchar,createddate,112) <= '20131021' 
    GROUP BY createddate
4

1 回答 1

1

这样的事情应该可以解决问题:

    SELECT *
FROM
(
    SELECT  CONVERT(varchar,createddate,112) as [Date],
            SUM([No of Jobs]) as [No of Jobs],
            SUM([Turnaround Time]) as [Turnaround Time],
            SUM([One Day Per]) as [One Day Per],
            ROUND((SUM([No of Jobs]) * SUM([Turnaround Time])),2) as [TOTAL hours]
    FROM DailyFinishJobsHistory
    WHERE CONVERT(varchar,createddate,112) >= '20131015'
    AND CONVERT(varchar,createddate,112) <= '20131021' 
    GROUP BY createddate
) A
JOIN
(
    SELECT 
        JobFinishedDate,
        COUNT(*) [Number of techs]
    From
    (
        SELECT CONVERT(varchar,JobFinishedDate,112) JobFinishedDate
        FROM MyJobs
        WHERE CONVERT(varchar,JobFinishedDate,112) >= '20131015' 
        AND CONVERT(varchar,JobFinishedDate,112) <='20131021'  
        AND JobState = 'FINISHED'
    ) t1    
    GROUP BY JobFinishedDate
) B
ON B.JobFinishedDate = A.[Date]

有关更简单的示例,请参见连接两个 select 语句

于 2013-10-22T14:23:55.510 回答