简体   繁体   English

如何在sql中将两行合并为一行?

[英]How to merge two rows into one row in sql?

I have a table as 我有一张桌子

EmployeeID    IndividualPay  FamilyPay   IsActive
    1            200          300        true
    1            100          150        false

But I want the output as follows(I want to use this output to inner join with some other table) 但我希望输出如下(我想使用此输出与其他表的内连接)

EmployeeID  IndPay_IsActive  IndPay_IsNotActive FamilyPay_IsActive   FamilyPay_IsNotActive 
    1            200                 100              300                   150

I have looked into PIVOT , but not sure how to use it in my case. 我调查了PIVOT ,但不知道如何在我的情况下使用它。

This type of transformation is known as a pivot . 这种类型的转换称为枢轴 You did not specify what database you are using but you can use an aggregate function with a CASE expression in any system: 您没有指定要使用的数据库,但可以在任何系统中使用带有CASE表达式的聚合函数:

select employeeid,
  max(case when IsActive = 'true' then IndividualPay end) IndPay_IsActive,
  max(case when IsActive = 'false' then IndividualPay end) IndPay_IsNotActive,
  max(case when IsActive = 'true' then FamilyPay end) FamilyPay_IsActive,
  max(case when IsActive = 'false' then FamilyPay end) FamilyPay_IsNotActive
from yourtable
group by employeeid

See SQL Fiddle with Demo 请参阅SQL Fiddle with Demo

Depending on your database, if you have access to both the PIVOT and UNPIVOT functions, then they can be used to get the result. 根据您的数据库,如果您有权访问PIVOTUNPIVOT函数,则可以使用它们来获取结果。 The UNPIVOT function converts the IndividualPay and FamilyPay columns into rows. UNPIVOT函数将IndividualPayFamilyPay列转换为行。 Once that is done, then you can create the four new columns with the PIVOT function: 完成后,您可以使用PIVOT函数创建四个新列:

select *
from
(
  select employeeid,
    case when isactive = 'true'
      then col+'_IsActive'
      else col+'_IsNotActive' end col, 
    value
  from yourtable
  unpivot
  (
    value
    for col in (IndividualPay, FamilyPay)
  ) unpiv
) src
pivot
(
  max(value)
  for col in (IndividualPay_IsActive, IndividualPay_IsNotActive,
              FamilyPay_IsActive, FamilyPay_IsNotActive)
) piv

See SQL Fiddle with Demo . 请参阅SQL Fiddle with Demo

Both give the same result: 两者都给出相同的结果:

| EMPLOYEEID | INDIVIDUALPAY_ISACTIVE | INDIVIDUALPAY_ISNOTACTIVE | FAMILYPAY_ISACTIVE | FAMILYPAY_ISNOTACTIVE |
----------------------------------------------------------------------------------------------------------------
|          1 |                    200 |                       100 |                300 |                   150 |
Select
    EmployeeID,
    Active.IndividualPay As IndPay_IsActive,
    Active.FamilyPay As FamilyPay_IsActive,
    Inactive.IndividualPay As IndPay_IsNotActive,
    Inactive.FamilyPay As FamilyPay_IsNotActive
From
    PayTable Active
    Join PayTable Inactive On Active.EmployeeID = Inactive.EmployeeId
        And Inactive.IsActive = 'false'
Where
    Active.IsActive = 'true'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM