Sql查询是单个标题但有多个结果

时间:2013-07-09 06:17:50

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

我面临着创建查询的问题:

REF_CORP_ID | DEPARTMENT_NAME | IS_APPROVED | APPROVED_BY | APPROVED_DATETIME        |APPROVAL_FROM | CREATED_BY
2           | Sales           | 1           | 1           | 2013-07-05 18:19:31.917  | 2            | 1
            |                 | 1           | 1           | 2013-07-05 18:19:31.917  | 2            | 1
            |                 | 1           | 1           | 2013-07-05 18:19:31.917  | 2            | 1
1           | IT              | 1           | 1           | 2013-07-05 18:05:21.170  | 2            | 1
            |                 | 1           | 1           | 2013-07-05 18:05:21.170  | 2            | 1
            |                 | 1           | 1           | 2013-07-05 18:05:21.170  | 2            | 1
3           | Testing         | 0           | 1           | 2013-07-05 18:32:02.207  | 1            | 1
            |                 | 0           | 1           | 2013-07-05 18:32:31.980  | 1            | 1
4           | HR              | 1           | 1           | 2013-07-08 11:14:42.817  | 2            | 1
            |                 | 1           | 1           | 2013-07-08 11:14:42.817  | 2            | 1

4 个答案:

答案 0 :(得分:4)

试试这个

;with cte as
( 
   select   ROW_NUMBER() OVER(Partition by [DEPARTMENT NAME] order by REF_CORP_ID ) as rno,

       [REF_CORP_ID] ,
       [DEPARTMENT] ,
       [IS_APPROVED],
       [APPROVED_BY],
       [APPROVED_DATETIME],
       [APPROVAL_FROM],
       [CREATED_BY]
FROM Table
)

SELECT     [REF_CORP_ID] ,
           CASE rno WHEN 1 THEN [DEPARTMENT]  ELSE '' END AS 'DEPARTMENT NAME',
           [IS_APPROVED],
           [APPROVED_BY],
           [APPROVED_DATETIME],
           [APPROVAL_FROM],
           [CREATED_BY]
FROM cte

答案 1 :(得分:1)

SELECT [REF_CORP_ID] + ' ' + [DEPARTMENT] + ' ' + [NAME] + ' ' + [IS_APPROVED] + ' ' + [APPROVED_BY] FROM Table...一样?

答案 2 :(得分:0)

尝试此查询,

select
MAX(ISNULL(REF_CORP_ID,'')),
MAX(ISNULL(DEPARTMENT_NAME,'')),
IS_APPROVED,
APPROVED_BY,
APPROVED_DATETIME,
APPROVAL_FROM,
CREATED_BY
FROM TABLE1
GROUP BY IS_APPROVED,APPROVED_BY,APPROVED_DATETIME, APPROVAL_FROM,CREATED_BY

<强> SQL FIDDLE

请参阅此图片

enter image description here

答案 3 :(得分:0)

试试这个

create table #temp 
                    (
                        REF_CORP_ID   varchar(20),
                        DEPARTMENT  varchar(20), 
                        IS_APPROVED bit, 
                        APPROVED_BY smallint,
                        APPROVED_DATETIME datetime,
                        APPROVAL_FROM smallint,
                        CREATED_BY smallint
                    )
create table #temp2 
                    (   ROW             int,
                        REF_CORP_ID   varchar(20),
                        DEPARTMENT  varchar(20), 
                        IS_APPROVED bit, 
                        APPROVED_BY smallint,
                        APPROVED_DATETIME datetime,
                        APPROVAL_FROM smallint,
                        CREATED_BY smallint
                    )

insert into #temp (REF_CORP_ID, DEPARTMENT, IS_APPROVED, APPROVED_BY, APPROVED_DATETIME, APPROVAL_FROM, CREATED_BY)
select '2',   'Sales',   1,   1 ,  '2013-07-05 18:19:31.917', 2 ,  1                

insert into #temp
select '2',   'Sales',   1,   1 ,  '2013-07-05 18:19:31.917', 2 ,  1

insert into #temp
select '2',   'Sales',   1,   1 ,  '2013-07-05 18:19:31.917', 2 ,  1

insert into #temp
select '1',   'IT',   1,   1 ,  '2013-07-05 18:05:21.170', 2 ,  1                   

insert into #temp
select '1',   'IT',   1,   1 ,  '2013-07-05 18:05:21.170', 2 ,  1                   

insert into #temp
select '1',   'IT',   1,   1 ,  '2013-07-05 18:05:21.170', 2 ,  1                   

insert into #temp
select '3',   'Testing',   0,   1 ,  '2013-07-05 18:32:02.207', 2 ,  1      

insert into #temp
select '3',   'Testing',   0,   1 ,  '2013-07-05 18:32:02.207', 2 ,  1                          

insert into #temp
select '4',   'HR',   1,   1 ,  '2013-07-08 11:14:42.817', 2 ,  1                           

insert into #temp
select '4',   'HR',   1,   1 ,  '2013-07-08 11:14:42.817', 2 ,  1                           





insert into #temp2 
select  ROW_NUMBER() OVER(PARTITION BY REF_CORP_ID ORDER BY REF_CORP_ID DESC) AS Row,
        REF_CORP_ID,
        DEPARTMENT 
        ,IS_APPROVED
        ,APPROVED_BY
        ,APPROVED_DATETIME
        ,APPROVAL_FROM
        ,CREATED_BY 
from #temp 



select 
        (CASE  WHEN row >1 THEN '' ELSE REF_CORP_ID END)REF_CORP_ID,
        (CASE  WHEN row >1 THEN '' ELSE DEPARTMENT END)DEPARTMENT,
        IS_APPROVED
        ,APPROVED_BY
        ,APPROVED_DATETIME
        ,APPROVAL_FROM
        ,CREATED_BY 

from #temp2 



drop table #temp 
drop table #temp2