0

我正在寻找 Sql Server 中的 Select 语句,我想要这样的东西

SELECT                 
   st.segment_guid as ST_Segment_Guid,
   ST.ST02 as [Transaction_Set_ControlNumber],      
   N1.N102  AS [Payer_Name],                        
   N1.N104  AS [Payer_Identification_Number],
   N1.N102  AS [Payee_Name],
   N1.N104 END AS [Payee_Identification_Number],

FROM X12_ST ST
   INNER JOIN X12_N1 N1 ON st.segment_guid = n1.parent_segment_guid 
where ST.x12_interchange_guid = '3ff7d629-0036-4deb-bb69-31f8c159f71e'

如果N1.LoopID是,'1000A'那么它应该从付款人列中选择。如果N1.LoopID是, '1000B'那么它应该从收款人列中选择。

而对于 ST.x12_interchange_guid = '3ff7d629-0036-4deb-bb69-31f8c159f71e' 应该有一个付款人和一个收款人,我想要打扰付款人,收款人对于 ST.x12_interchange_guid 在单行中选择它时。

4

2 回答 2

0
CASE
  WHEN N1.LoopID = '1000A'
    THEN (Column from Payer Table)
  ELSE SomeOtherColumn
END AS theColumn

文档

于 2013-08-30T00:15:06.240 回答
0

在同一行上获得两件东西通常意味着聚合(或在某些情况下是连接)。您将需要聚合以及 case 语句:

SELECT st.segment_guid as ST_Segment_Guid,
       ST.ST02 as [Transaction_Set_ControlNumber],      
       max(case when N1.LoopID = '1000A' then N1.N102 end) AS [Payer_Name],                        
       max(case when N1.LoopID = '1000A' then N1.N104 end) AS [Payer_Identification_Number],
       max(case when N1.LoopID <> '1000A' then N1.N102 end) AS [Payee_Name],
       max(case when N1.LoopID <> '1000A' then N1.N104 end) AS [Payee_Identification_Number]
FROM X12_ST ST INNER JOIN
     X12_N1 N1
     ON st.segment_guid = n1.parent_segment_guid 
where ST.x12_interchange_guid = '3ff7d629-0036-4deb-bb69-31f8c159f71e'
group by st.segment_guid, ST.ST02;
于 2013-08-30T01:05:00.083 回答