如何从SQL Server查询中获得所需的内容

时间:2014-12-15 07:41:46

标签: sql sql-server-2008

以下查询是我编写的用于生成每周状态报告的查询。

因为“Union”上方查询的上半部分给出了每个BU名称和产品名称的总体状态。

查询的其他部分详细分析了整体状态,它给出了每个项目的结果以及每个工程师的名称。

以下查询从头到尾为我提供了完整的数据库结果。

但是当用户选择日期时,我只希望从该日期到日期的结果。因为这是每周报告。

我希望结果中的列数据更少,比如上周完成的部件数量以及本周每个工程师完成的部件数量。

每当我在代码中添加一些内容时,我都会在group by中添加列名称时出错,如果我添加它,我就无法获得所需的结果。

  1. 从给定的日期开始 - 完成零件的7天计数。
  2. 通过包含上述内容,总体状态编号应与个别状态编号匹配 喜欢(所有个人的+ =总体)

  3.  select BU.bu_desc as [BU], prod.Product_Desc as [Product Name], NULL as [SC-ID], 
     'OVERALL PROJECT STATUS' as [Project Name],
     NULL as [Requestor], NULL as [Request Date],
    
     case
     when BU.bu_desc in ('DSM','Synexis','ALD') then 'GW'
     when BU.bu_desc in ('Etch','SRP','FEP') then 'MS'
     when BU.bu_desc in ('CMP','MDP') then 'RS'
     else 'PT'
     end as [PM],
    
     NULL as [SCE],
     (count(parts.part_id_num)-count(parts.cancelled_date)) as [Total Parts Requested],
     '' as [No. Of Parts Completed Last Week],
     '' as [No. Of Parts Completed This Week],
     count(parts.analysis_Complete_date) as [Total No.Of Parts Completed], 
    
     case 
     when (((count(parts.part_id_num)-count(parts.cancelled_date))-(count(parts.analysis_Complete_date)))=0) then 100
     when ((count(parts.analysis_Complete_date))=0) then 0
     else
     ((count(parts.analysis_Complete_date)*100)/(count(parts.part_id_num)-
     count(parts.cancelled_date)))
     end as [SC Analysis Completed (%)],
    
     NULL as [Request Completion Date],
     NULL as [Committed Date],
     NULL as Notes
     from scn_project_details as proj 
     left join scn_part_details as parts on proj.project_id=parts.project_id 
     left join SCN_BU bu on bu.bu_id=proj.bu_id 
     left join dbo.SCN_Product_Name prod on prod.Product_Id=proj.Product_Id 
    
     where proj.status_id<>12 and (proj.analysis_complete_date between '2014-12-10' and getdate()
     or proj.status_id between 4 and 8) and parts.sc_id is not null
    
     group by BU.bu_desc,prod.Product_Desc
    
     union
    
     select BU.bu_desc as [BU], prod.Product_Desc as [Product Name], proj.project_id as [SC-ID], 
     proj.project_name as [Project Name],usr1.fname+' '+usr1.lname as [Requestor], 
     proj.created_date as [Request Date],left(proj.pm_id,2) as [PM],
     usr2.fname+' '+usr2.lname as [SCE],
     (count(parts.part_id_num)-count(parts.cancelled_date)) as [Total Parts Requested],
     '' as [No. Of Parts Completed Last Week],
     '' as [No. Of Parts Completed This Week],
     count(parts.analysis_Complete_date) as [Total No.Of Parts Completed], 
    
     case 
     when (((count(parts.part_id_num)-count(parts.cancelled_date))-(count(parts.analysis_Complete_date)))=0) then 100
     when ((count(parts.analysis_Complete_date))=0) then 0
     else
     ((count(parts.analysis_Complete_date)*100)/(count(parts.part_id_num)-  count(parts.cancelled_date)))
     end as [SC Analysis Completed (%)],
    
     proj.project_completition_date as [Request Completion Date],
     proj.original_commit_date as [Committed Date],
     NULL as Notes
     from scn_project_details as proj 
     left join scn_part_details as parts on proj.project_id=parts.project_id 
     left join SCN_users usr1 on proj.created_by=usr1.[user_id] 
     left join SCN_users usr2 on parts.sc_id=usr2.[user_id] 
     left join SCN_BU bu on bu.bu_id=proj.bu_id 
     left join dbo.SCN_Product_Name prod on prod.Product_Id=proj.Product_Id 
    
     where proj.status_id<>12 and (proj.analysis_complete_date between '2014-12-10' and getdate()
     or proj.status_id between 4 and 8) and parts.sc_id is not null
    
     group by BU.bu_desc,prod.Product_Desc,proj.project_id,proj.project_name,usr1.fname+'      '+usr1.lname, proj.created_date,proj.project_completition_date,proj.original_commit_date,
     proj.pm_id,usr2.fname+' '+usr2.lname,proj.analysis_complete_date
    

    如何获得上周分析的部分数量以及本周完成的部分数量。

    我想要结果中每一行的计数,而不是总计。

        (select count(parts1.analysis_Complete_date) from scn_part_details as parts1 
        where parts1.analysis_Complete_date between '2014-12-10' and getdate()
        ) as [No. Of Parts Completed Last Week],
    
        (select count(parts1.analysis_Complete_date) from scn_part_details as parts1 
        where parts1.analysis_Complete_date between '2014-12-01' and '2014-12-09'
        ) as [No. Of Parts Completed This Week],
    

    查询中包含的此代码为我提供了每行的总计数。像所有行中的125一样。但我想个人数。

2 个答案:

答案 0 :(得分:0)

您可以将整个查询放入括号中,并使用SELECT WHERE应用其他过滤条件,按等排序。

 SELECT x.*  -- choose fields
 FROM (
  -- The whole working query comes here 
 ) as x
 WHERE x.[Request Completion Date]>'20141201' -- filter

最好从基本查询中创建表值函数或视图,以获得代码重用性。

答案 1 :(得分:0)

我终于通过创建存储过程实现了它,并对其进行了如下解释。

创建一个包含数据和[No.上周完成的零件],[没有。本周完成的部分]列为NULL。然后写了一个更新语句来获取计数并更新记录。最后从临时表中选择数据并将其加载到数据表中。

Create PROCEDURE  [dbo].[SCN_SP_SCE_Weekly_Report]
@startdate as datetime

AS
BEGIN
SET NOCOUNT ON;


declare @date datetime
declare @LastWeekStart datetime
declare @LastWeekEnd datetime
declare @ThisWeekStart datetime
declare @ThisWeekEnd datetime

set @date=(select dateadd(w, -7, @startdate))

set @LastWeekStart=(SELECT  DATEADD(DAY, 1 - DATEPART(WEEKDAY, @date), CAST(@date AS DateTime)))
set @LastWeekEnd=(DATEADD(DAY, 7 - DATEPART(WEEKDAY, @date), CAST(@date AS DateTime)))

set @ThisWeekStart=(SELECT  DATEADD(DAY, 1 - DATEPART(WEEKDAY, @startdate), CAST(@startdate AS DateTime)))
set @ThisWeekEnd=(DATEADD(DAY, 7 - DATEPART(WEEKDAY, @startdate), CAST(@startdate AS DateTime)))

select * into #a from(
select BU.bu_desc as [BU], prod.Product_Desc as [Product Name], NULL as [SC-ID], 
'OVERALL PROJECT STATUS' as [Project Name],'' as [Requestor], NULL as [Request Date],
case
when BU.bu_desc in ('DSM','Synexis','ALD') then 'GW'
when BU.bu_desc in ('Etch','SRP','FEP') then 'MS'
when BU.bu_desc in ('CMP','MDP') then 'RS'
else 'PT'
end as [PM],
'' as [SCE],
(count(parts.part_id_num)-count(parts.cancelled_date)) as [Total Parts Requested],
null as [No. Of Parts Completed Last Week],
null as [No. Of Parts Completed This Week],
count(parts.analysis_Complete_date) as [Total No.Of Parts Completed], 
case 
when (((count(parts.part_id_num)-count(parts.cancelled_date))-(count(parts.analysis_Complete_date)))=0) then 100
when ((count(parts.analysis_Complete_date))=0) then 0
else
((count(parts.analysis_Complete_date)*100)/(count(parts.part_id_num)-count(parts.cancelled_date)))
end as [SC Analysis Completed (%)],NULL as [Requested Completion Date],
NULL as [Committed Date],NULL as Notes,'' as sc_id
from scn_project_details as proj 
left join scn_part_details as parts on proj.project_id=parts.project_id 
left join SCN_BU bu on bu.bu_id=proj.bu_id 
left join dbo.SCN_Product_Name prod on prod.Product_Id=proj.Product_Id 
where proj.status_id<>12 and (proj.analysis_complete_date between @startdate and getdate()
or (proj.status_id >= 4 and proj.status_id < 8)) and parts.sc_id is not null
group by BU.bu_desc,prod.Product_Desc

union

select BU.bu_desc as [BU], prod.Product_Desc as [Product Name], proj.project_id as [SC-ID], 
proj.project_name as [Project Name],usr1.fname+' '+usr1.lname as [Requestor], 
proj.created_date as [Request Date],left(proj.pm_id,2) as [PM],usr2.fname+' '+usr2.lname as     [SCE],
(count(parts.part_id_num)-count(parts.cancelled_date)) as [Total Parts Requested],
null as [No. Of Parts Completed Last Week],
null as [No. Of Parts Completed This Week],
count(parts.analysis_Complete_date) as [Total No.Of Parts Completed], 
case 
when (((count(parts.part_id_num)-count(parts.cancelled_date))-(count(parts.analysis_Complete_date)))=0) then 100
when ((count(parts.analysis_Complete_date))=0) then 0
else
((count(parts.analysis_Complete_date)*100)/(count(parts.part_id_num)-count(parts.cancelled_date)))
end as [SC Analysis Completed (%)],
proj.project_completition_date as [Requested Completion Date],
proj.original_commit_date as [Committed Date],
NULL as Notes,parts.sc_id as sc_id
from scn_project_details as proj 
join scn_part_details as parts on proj.project_id=parts.project_id  and parts.sc_id is not null
left join SCN_users usr1 on proj.created_by=usr1.[user_id] 
left join SCN_users usr2 on parts.sc_id=usr2.[user_id] 
left join SCN_BU bu on bu.bu_id=proj.bu_id 
left join dbo.SCN_Product_Name prod on prod.Product_Id=proj.Product_Id 
where proj.status_id<>12 and (proj.analysis_complete_date between @startdate and getdate()
or (proj.status_id >= 4 and proj.status_id < 8)) and parts.sc_id is not null
group by BU.bu_desc,prod.Product_Desc,proj.project_id,proj.project_name,usr1.fname+'     '+usr1.lname,
proj.created_date,proj.project_completition_date,proj.original_commit_date,
proj.pm_id,usr2.fname+' '+usr2.lname,proj.analysis_complete_date,parts.sc_id ) as t


update a
set a.[No. Of Parts Completed Last Week]=c.cnt
from #a a, (
select count(b.sc_id) as cnt,b.project_id,b.sc_id
from scn_part_details as b
where b.analysis_complete_date between @LastWeekStart and @LastWeekEnd
group by b.project_id,b.sc_id) as c
where a.[SC-ID]=c.project_id
and a.sc_id=c.sc_id

update a
set a.[No. Of Parts Completed This Week]=c.cnt
from #a a, (
select count(b.sc_id) as cnt,b.project_id,b.sc_id
from scn_part_details as b
where b.analysis_complete_date between @ThisWeekStart and @ThisWeekEnd
group by b.project_id,b.sc_id) as c
where a.[SC-ID]=c.project_id
and a.sc_id=c.sc_id

select BU,[Product Name],[SC-ID],[Project Name],Requestor,[Request Date],PM,SCE,
[Total Parts Requested],[No. Of Parts Completed Last Week],[No. Of Parts Completed This Week],
[Total No.Of Parts Completed], [SC Analysis Completed (%)],[Requested Completion Date],
[Committed Date],Notes
from #a

End 

GO