在一个查询中同时需要这两行

时间:2018-07-19 21:13:26

标签: sql sql-server sql-server-2008 tsql ssms

my as语句应为“ statementmessage”,即标题行,并且通过串联将消息1和消息2都放在一行中。 如果有任何疑问可以帮助我。

           WITH with_stmt_bills as (
                   SELECT 

                       BarStatement_Accounts_AcctMsgText.TextID,
                       BarStatement_Accounts_AcctMsgText.TextSeqID,
                         CASE 
                           WHEN BarStatement_Accounts_AcctMsgText.TextSeqID=0 
                           THEN BarStatement_Accounts_AcctMsgText.TextLine
                           END AS Message1,
                         CASE
                           WHEN BarStatement_Accounts_AcctMsgText.TextSeqID=1
                           THEN BarStatement_Accounts_AcctMsgText.TextLine
                           ELSE ''
                           END AS Message2,
         CONCAT(ISNULL('Message1',''),' ',ISNULL('Message2','')) as StatementMessage,

-----------------------带有语句2 --------------------- -------

                     full_stmt_bills as (

                           (SELECT top 1 Message1 FROM with_stmt_bills
                                    WHERE VisitID = internal_table.VisitID
                                    AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
                                    ORDER by TextID desc) as Message1,

                            (SELECT top 1 Message2 FROM with_stmt_bills
                                    WHERE VisitID = internal_table.VisitID
                                    AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
                                    ORDER by TextID desc) as Message2,

在此处使用此concat将输出显示为列中的message1消息2,实际上我需要输出存储在message 1和message 2中的数据

          (select CONCAT(ISNULL('Message1',''),' ',ISNULL('Message2',''))) as StatementMessage,

2 个答案:

答案 0 :(得分:0)

使用+进行串联:

(SELECT top 1 Message1 + Message2 FROM with_stmt_bills
 WHERE VisitID = internal_table.VisitID
   AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
 ORDER by TextID desc) as Message1and2

在修改OP后添加的内容:

在编辑帖子后,第一个代码块(只是CTE的一个片段)是无用的,因为您似乎试图连接两个字符串(Message1和Message2),其中最多两个字符串不会为空(因为TextSeqID不能同时为01。但是,ISNULL('Message1','')将始终返回字符串'Message1'。当然,您的意思是ISNULL(Message1,''),但这是行不通的,因为您不能直接在其他计算列中引用计算列。

在第二个查询片段中,根本不清楚full_stmt_bills是什么。是另一个CTE吗?否,因为定义仅包含2个值,但没有VALUES子句。您似乎定义了2个完全不同的Message1和Message2列。那么,威士忌Tango Foxtrott您是否要串联以形成串联字符串?我为您的第二个代码片段提供了分析服务,对于第一个(新的)代码片段,它应该是Message1或Message2,所以:

WITH with_stmt_bills as (
  SELECT 

    BarStatement_Accounts_AcctMsgText.TextID,
    BarStatement_Accounts_AcctMsgText.TextSeqID,
    CASE 
      WHEN BarStatement_Accounts_AcctMsgText.TextSeqID=0 
      THEN BarStatement_Accounts_AcctMsgText.TextLine
    END AS Message1,
    CASE
      WHEN BarStatement_Accounts_AcctMsgText.TextSeqID=1
      THEN BarStatement_Accounts_AcctMsgText.TextLine
      ELSE ''
    END AS Message2,
    CASE 
      WHEN BarStatement_Accounts_AcctMsgText.TextSeqID IN (0, 1) 
      THEN BarStatement_Accounts_AcctMsgText.TextLine
      ELSE ''
    END AS StatementMessage,

答案 1 :(得分:0)

您在这里!

SELECT
    (SELECT top 1 Message1 FROM with_stmt_bills
     WHERE VisitID = internal_table.VisitID AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
     ORDER by TextID desc)
     + ' ' + 
    (SELECT top 1 Message2 FROM with_stmt_bills
     WHERE VisitID = internal_table.VisitID AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
     ORDER by TextID desc
    ) AS statementmessage

当然,如果您实际上是从同一列的同一行中提取信息,则可以简化此操作。

SELECT Message1 + ' ' + Message2 AS statementmessage
FROM with_stmt_bills
WHERE VisitID = internal_table.VisitID AND with_stmt_bills.LedBillDateID = internal_table.LedBillDateID
ORDER by TextID desc