简体   繁体   English

如何在单个查询中添加两列作为结果

[英]how to add two columns as result in single query

I have below queries我有以下查询

select a.emp_id,a.AUTH_TYPE,b.NTE_TX as des1
  from table  a inner join NOTES b on b.TBL_ROW_ID=a.emp_id
 where a.emp_id=56 and REF_NTE_TYP_ID=27

Output 1 Output 1

在此处输入图像描述

select a.emp_id,a.AUTH_TYPE,b.NTE_TX as des2
  from table  a inner join NOTES b on b.TBL_ROW_ID=a.emp_id
 where a.emp_id=56 and REF_NTE_TYP_ID=28

Output 2 Output 2

在此处输入图像描述

Expected Output:预期 Output:

在此处输入图像描述

please help any one.请帮助任何人。

Simple Aggregation(MAX) will help you to get your expected output. Simple Aggregation(MAX)将帮助您获得预期的 output。

Try this:尝试这个:

select a.emp_id,a.AUTH_TYPE
    ,MAX(CASE WHEN REF_NTE_TYP_ID=27 THEN b.NTE_TX END) as des1
    ,MAX(CASE WHEN REF_NTE_TYP_ID=28 THEN b.NTE_TX END) as des2
  from table  a inner join NOTES b on b.TBL_ROW_ID=a.emp_id
where a.emp_id=56 and REF_NTE_TYP_ID IN(27,28)
GROUP BY a.emp_id,a.AUTH_TYPE

If TBL_ROW_ID, REF_NTE_TYP_ID is unique in your table NOTES you can also use subqueries:如果TBL_ROW_ID, REF_NTE_TYP_ID在您的表中是唯一的NOTES您还可以使用子查询:

select a.emp_id,a.AUTH_TYPE
     , (SELECT b.NTE_TX 
          FROM NOTES b 
         WHERE b.TBL_ROW_ID=a.emp_id
           AND REF_NTE_TYP_ID=27) as des1
     , (SELECT b.NTE_TX 
          FROM NOTES b 
         WHERE b.TBL_ROW_ID=a.emp_id
           AND REF_NTE_TYP_ID=28) as des2
  from table a
 where a.emp_id=56

If you have multiple text-entries you might want to concatenate these entries to just print one long text (replace the 2 occurences of SELECT b.NTE_TX with the following):如果您有多个文本条目,您可能希望连接这些条目以仅打印一个长文本(将SELECT b.NTE_TX的 2 个出现替换为以下内容):

SELECT RTRIM(XMLAGG(XMLELEMENT(b.NTE_TX, CHR(10)).EXTRACT('//text()') ORDER BY b.NTE_TX).GetClobVal(),CHR(10))

I used CHR(10) as text delimiter, just replace it with any other string you want.我使用CHR(10)作为文本分隔符,只需将其替换为您想要的任何其他字符串。 And you might also want some other ORDER BY condition.而且您可能还需要其他一些ORDER BY条件。

With Q1 As (
    select 56 emp_id, 'diple' auth_type 
),
Q2 As (
    Select 'text2' Des2,27 ref_nte_type_id, 56 row_id 
    Union All
    Select 'text1' Des2,28 ref_nte_type_id, 56 row_id 
)

Select Q1.*,Q2.Des2,Q3.Des2 Des1 
    From Q1 
    Inner Join Q2 
    On Q1.Emp_Id=Q2.Row_Id
    And Q2.Ref_Nte_Type_Id=27
    Inner Join Q2 Q3
    On Q1.Emp_Id=Q3.Row_Id
    And Q3.Ref_Nte_Type_Id=28

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

相关问题 如何在sql中将两个查询结果添加为单个结果? - how to add two query results as a single result in sql? 如何通过单个查询获得两个单独的结果 - How to get two separate result with single query 如何添加两个数据库列将显示结果 - How to add two database columns are show the result 如何将两个 SQL 查询结果添加到一个结果表中,每个查询结果具有不同的列? - How to add two SQL queries results in one resultant table with different columns for each query result? 如何按两列对查询结果进行排序,其中一列最后为空 - How to order a query result by two columns with nulls last in one of the columns 如何选择两列(名称,值)并作为单个结果返回? - How to to select two columns (Name, Value) and return as single result? 如何进行查询以在两列中添加值 - how to make a query to add value in two columns 如何根据另一个查询向现有查询结果添加 2 列? - How to add 2 columns to existing query result based on another query? 将两个不同的不相关的SQL查询(返回单列结果)组合为一个具有两列的查询结果 - Combine two different unrelated SQL queries( returning single column results) into one query result with two columns PostgreSQL:如何将两列相乘并在同一查询中显示结果? - PostgreSQL: How to multiply two columns and display result in same query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM