如何才能进行以下显示实例> 1

时间:2015-08-20 14:59:22

标签: sql sql-server sql-server-2008 tsql

我在存储过程中有一个生成临时表的查询。

我需要它只显示超过1行的应用程序,并且每个重复条目都需要一行。

这是我到目前为止,我怎样才能使它发挥作用?

    Select  t2.ApplicationName, t3.MainframeDev, t3.MainframeTest, t3.MMX,       
            t3.HMG, t3.WMI, t3.TJXE, 'Changing' as UsedAs, t3.ProjectName, 
            t4.FirstName+'  '+LastName as ProjectLead,t3.Status,  
            CONVERT(varchar(20),t3.ImplementationDate,126) as ImplementationDate
    from    ImpactedApplications t1 
    inner join Applications t2 on t2.AppID = t1.AppID 
    inner join Projects t3 on t3.ProjectID = t1.ProjectID
    inner join Employees t4 on t3.ProjectLead = t4.EmpId
    where t2.ApplicationName <> 'NA' 


    UNION

    Select  t2.ApplicationName, t3.MainframeDev, t3.MainframeTest, t3.MMX, 
            t3.HMG, t3.WMI, t3.TJXE, 'Supporting' as UsedAs, t3.ProjectName, 
            t4.FirstName+' '+LastName as ProjectLead,t3.Status, 
            CONVERT(varchar(20),t3.ImplementationDate,126) as ImplementationDate
    from SupportingApplications t1 
    inner join Applications t2 on t2.AppID = t1.AppID
    inner join Projects t3 on t3.ProjectID = t1.ProjectID
    inner join Employees t4 on t3.ProjectLead = t4.EmpId
    where t2.ApplicationName <> 'NA'
    Order By ApplicationName, MainframeDev, UsedAs ASC 

预期输出将具有来自上面的选择查询的相关信息的应用程序,如下所示:

ApplicationName1 
ApplicationName1 
ApplicationName1 
ApplicationName2 
ApplicationName2 
ApplicationName2 
ApplicationName3
ApplicationName3

2 个答案:

答案 0 :(得分:1)

如果你想获得所有重复的行或所有那些有多个条目的应用程序,那么我认为你只需要Group By Clause和Having Clause来解决这个问题。

将查询的输出放在临时表中说#application,然后

Create table #application(appid int, aapvname varchar(50))
insert into #application values (1,'100'),(1,'101'),(2,'200'),(2,'201'),(3,'300'),(4,'400')

Select * 
From #application
WHERE appid IN (
                 Select appid 
                 From #application
                 GROUP BY appid
                 Having COUNT(*) > 1)

<强> Working SQL FIDDLE Sample

答案 1 :(得分:0)

由于我没有你的基本表,这在黑暗中有点像:

;WITH
    cte1 AS (
        Select  t2.ApplicationName, t3.MainframeDev, t3.MainframeTest, t3.MMX,       
                t3.HMG, t3.WMI, t3.TJXE, 'Changing' as UsedAs, t3.ProjectName, 
                t4.FirstName+'  '+LastName as ProjectLead,t3.Status,  
                CONVERT(varchar(20),t3.ImplementationDate,126) as ImplementationDate
        from    ImpactedApplications t1 
        inner join Applications t2 on t2.AppID = t1.AppID 
        inner join Projects t3 on t3.ProjectID = t1.ProjectID
        inner join Employees t4 on t3.ProjectLead = t4.EmpId
        where t2.ApplicationName <> 'NA' 

        UNION

        Select  t2.ApplicationName, t3.MainframeDev, t3.MainframeTest, t3.MMX, 
                t3.HMG, t3.WMI, t3.TJXE, 'Supporting' as UsedAs, t3.ProjectName, 
                t4.FirstName+' '+LastName as ProjectLead,t3.Status, 
                CONVERT(varchar(20),t3.ImplementationDate,126) as ImplementationDate
        from SupportingApplications t1 
        inner join Applications t2 on t2.AppID = t1.AppID
        inner join Projects t3 on t3.ProjectID = t1.ProjectID
        inner join Employees t4 on t3.ProjectLead = t4.EmpId
        where t2.ApplicationName <> 'NA'
    ), cte2 AS (
        SELECT      *, AppCount = COUNT(ApplicationName) OVER (PARTITION BY ApplicationName)
        FROM        cte1
    )

SELECT  *
FROM    cte2
WHERE   AppAcount > 1