如何水平显示行而不是垂直显示行?

时间:2019-07-14 06:30:56

标签: sql sql-server tsql sql-server-2012

我正在获取这样的记录:

Rev-- Status-- Remarks-- RemarksDate -- User
0     prepared  done     12/jul/2019    John
0     approved  done     12/jul/2019    Amy
0     Issued    done     13/jul/2019    Tom
1     REOPENED  done     14/jul/2019    John
1     Prepared  done     14/jul/2019    Ritz

但是我正试图显示这样

Rev   PreparedBy APprovedBy IssuedBy REOPENEDBy  Date
0     John       Amy        Tom      John        onlyissuedate
1     John       Ritz       Marine   Riya            //

我试图在不更改表结构的情况下实现这一点,但是不能。

查询:

        Select * from table

无需以第二种格式包含注释。

为了准备好,我正在使用status# 0,对于approve 1,对于issue 3,对于reopened 5

3 个答案:

答案 0 :(得分:2)

数据透视查询方法应该在这里起作用:

SELECT
    rh.Rev,
    MAX(CASE WHEN rh.Status = 'prepared' THEN rh.RemarksByName END) AS PreparedBy,
    MAX(CASE WHEN rh.Status = 'approved' THEN rh.RemarksByName END) AS ApprovedBy,
    MAX(CASE WHEN rh.Status = 'Issued'   THEN rh.RemarksByName END) AS IssuedBy,
    MAX(CASE WHEN rh.Status = 'REOPENED' THEN rh.RemarksByName END) AS reopenedBy,
    MAX(CASE WHEN rh.Status = 'Issued'   THEN rh.RemarksDate END)   AS Date
FROM RemarksHisotry rh
INNER JOIN inspectionReport ir
    ON ir.InspectionReportID = rh.InspectionReportID
INNER JOIN Status s
    ON s.StatusID = rh.[Status]
WHERE
    ir.VelosiReportNo = @InspectionReportNo
GROUP BY
    rh.Rev
ORDER BY
    rh.Rev;

答案 1 :(得分:1)

这可能有帮助。

DECLARE @TempData TABLE(Rev VARCHAR(10),Status VARCHAR(50),Remarks VARCHAR(50),RemarksDate DATE,UserName VARCHAR(50))
INSERT INTO  @TempData VALUES
('0',     'Prepared',  'done',     '12/jul/2019',    'John'),
('0',     'Approved',  'done',     '12/jul/2019',    'Amy '),
('0',     'Issued  ',  'done',     '13/jul/2019',    'Tom '),
('0',     'Reopened',  'done',     '14/jul/2019',    'John'),
('1',     'Reopened',  'done',     '14/jul/2019',    'Marine'),
('1',     'Prepared',  'done',     '14/jul/2019',    'John'),
('1',     'Approved',  'done',     '12/jul/2019',    'Ritz '),
('1',     'Issued',    'done',     '14/jul/2019',    'John')

SELECT pvtValue.Rev
      ,pvtValue.Prepared PreparedBy
      ,pvtValue.Approved ApprovedBy
      ,pvtValue.Issued IssuedBy
      ,pvtValue.Reopened ReopenedBy
      ,t.RemarksDate IssueDate
FROM( 
     SELECT Rev,Status,UserName
     FROM  @TempData

     ) pvt
     PIVOT (MAX(UserName) FOR STATUS IN (Prepared,Approved,Issued, Reopened )
     ) pvtValue 
     LEFT JOIN @TempData t ON pvtValue.Issued = t.UserName AND pvtValue.Rev = t.Rev AND t.Status = 'Issued'

enter image description here

答案 2 :(得分:0)

您可以简单地实现此目的,而无需JOIN相同表的任何副本。 您所需要的只是一种将每个行值转换为列的方式(为此,我们将使用CASE WHEN),并将其与将忽略MAX的聚合函数NULL结合起来我们在上述情况下指定的值。

select 
    rev,
    max(case when status='prepared' then user        else null end) preparedBy,
    max(case when status='approved' then user        else null end) approvedBy,
    max(case when status='Issued'   then user        else null end) IssuedBy  ,
    max(case when status='REOPENED' then user        else null end) reopenedBy,
    max(case when status='Issued'   then remarksdate else null end) date
from
    table_name
group by
    rev
order by
    rev