如何在sql中从此查询中获取所需的输出

时间:2017-09-14 12:32:05

标签: sql sql-server sql-server-2014

我有一个下面列出的表结构,

SL R_ID    Name  AMT1   AMT2 AMT3  Pos
1     5     ggg   10     60   22   Left
2     5     hhh   44     88   47   Right

我想要输出像这样

LSL LID  LName LAMT1 LAMT2 LAMT3 LPos   SL   RID  RName RAMT1 RAMT2 RAMT3 RPos
1     5     ggg   10     60   22   Left  2     5     hhh   44   88  47  Right

我试过这个

SELECT SL, [LSL], [LSL_HEAD], [LAMT], [LCURR], [LPRE], [RSL], [RSL_HEAD], [RAMT], [RCURR], [RPRE]
FROM
(select SL, R_ID, SL_HEAD, AMT, CURR, PRE, L_SLIDE from FinalAccountDetails f) as st

PIVOT
(
max(R_ID)
FOR
[L_SLIDE]
IN ([LSL], [LSL_HEAD], [LAMT], [LCURR], [LPRE], [RSL], [RSL_HEAD], [RAMT], [RCURR], [RPRE])
) AS pvt

2 个答案:

答案 0 :(得分:0)

如果您的数据如图所示,则不需要任何汇总或pivot(),您可以通过加入来执行此操作。

使用select .. from FinalAccountDetails

中显示的列名

测试设置:

create table finalaccountdetails ([sl] int, [R_id] int, [sl_head] varchar(3), [amt] int, [curr] int, [pre] int, [l_slide] varchar(5)) ; 
insert into finalaccountdetails values 
(1, 5, 'ggg', 10, 60, 22, 'Left'),(2, 5, 'hhh', 44, 88, 47, 'Right');

查询:

select 
    lsl   = l.SL
  , lid   = l.R_ID
  , lname = l.SL_HEAD
  , lamt1 = l.AMT
  , lamt2 = l.CURR
  , lamt3 = l.PRE
  , lpos  = l.L_SLIDE
  , rsl   = r.SL
  , rid   = r.R_ID
  , rname = r.SL_HEAD
  , ramt1 = r.AMT
  , ramt2 = r.CURR
  , ramt3 = r.PRE
  , rpos  = r.L_SLIDE
from finalaccountdetails l
  inner join finalaccountdetails r
    on l.r_id = r.r_id
   and l.L_SLIDE = 'left'
   and r.L_SLIDE = 'right'

rextester演示:http://rextester.com/HXCI97819

返回:

+-----+-----+-------+-------+-------+-------+------+-----+-----+-------+-------+-------+-------+-------+
| lsl | lid | lname | lamt1 | lamt2 | lamt3 | lpos | rsl | rid | rname | ramt1 | ramt2 | ramt3 | rpos  |
+-----+-----+-------+-------+-------+-------+------+-----+-----+-------+-------+-------+-------+-------+
|   1 |   5 | ggg   |    10 |    60 |    22 | Left |   2 |   5 | hhh   |    44 |    88 |    47 | Right |
+-----+-----+-------+-------+-------+-------+------+-----+-----+-------+-------+-------+-------+-------+

答案 1 :(得分:0)

类似的东西:

;WITH LeftData  AS
(
    SELECT  FAD.*
    FROM    FinalAccountDetails AS FAD
    WHERE   FAD.Pos             =   'Left'
)
,RightData      AS
(
    SELECT  FAD.*
    FROM    FinalAccountDetails AS FAD
    WHERE   FAD.Pos             =   'Right'
)
SELECT      LD.SL               AS  LSL         ,
            LD.R_ID             AS  LID         ,   
            LD.Name             AS  LName       ,
            LD.AMT1             AS  LATM1       ,
            LD.ATM2             AS  LATM2       ,
            LD.ATM3             AS  LATM3       ,
            LD.Pos              AS  LPos        ,
            RD.SL               AS  RSL         ,
            RD.R_ID             AS  RID         ,   
            RD.Name             AS  RName       ,
            RD.AMT1             AS  RATM1       ,
            RD.ATM2             AS  RATM2       ,
            RD.ATM3             AS  RATM3       ,
            RD.Pos              AS  RPos                
FROM        LeftData            AS  LD
INNER JOIN  RightData           AS  RD  ON  LD.R_ID =   RD.R_ID;

假定R_ID是数据之间的公共ID?