简体   繁体   中英

SQL return multiple rows as a single row

In our SQL-DB our four Limits are stored as separate rows.
I want to get the Limits back as a single row, with the four Limits as columns.
The Data looks like:

TABLE A
lsequence   operator1   value1    operator2    value2  
 1            <          10.5      {NULL}       {NULL}  
 2            >=         11.5      <=           12.5  
 3            >=         10.5      <=           13.5   
 4            >          13.5      {NULL}       {NULL}  

 TABLE B
 lsequence   limittypeid   
  1           LowerFail  
  2           Pass     
  3           Warning     
  4           UpperFail   

Wanted Return Value:

LowerFail      Pass                     Warning                 UpperFail  
   < 10.5       >= 11.5 || <= 12.5       >= 10.5 || <= 13.5      > 13.5 

With Case function bases on 'limittypeid' i get the 4 colums... But still 4 rows..
Current Return

COLUMN1      COLUMN2                COLUMN3                 COLUMN4
< 10.5 ||     
             >= 11.5 || <= 12.5        
                                    >= 10.5 || <= 13.5    
                                                             > 13.5 ||

Used Code:

SELECT
CASE
 WHEN a.limittypeid = 'LowerFail'
 THEN concat (b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2)
END,
CASE
  WHEN a.limittypeid = 'Pass'
  THEN concat (b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2)  
END,
CASE
  WHEN a.limittypeid = 'Warning'
  THEN concat (b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2) 
END,
   CASE
    WHEN a.limittypeid = 'UpperFail'
THEN concat (b.operator1 , ' ' , b.value1 , ' || ' , b.operator2 , ' ' ,  b.value2)  
END  
 FROM   
  b   
  INNER JOIN b.lsequence = a.lsequence  
  WHERE ......conditions........

I know it must be possible.

You are just missing the GROUP BY. Use those column(s) that are unique for the expected rows.

More about this pivot technique: http://modern-sql.com/use-case/pivot

Use aggregation:

SELECT MAX(CASE WHEN a.limittypeid = 'LowerFail'
                THEN concat(b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2)
           END),
       MAX(CASE WHEN a.limittypeid = 'Pass'
           THEN concat(b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2)  
           END),
       MAX(CASE WHEN a.limittypeid = 'Warning'
                THEN concat(b.operator1 , ' ' , b.value1 , ' || ' , b.operator2, ' ' , b.value2) 
           END),
       MAX(CASE WHEN a.limittypeid = 'UpperFail'
                THEN concat(b.operator1 , ' ' , b.value1 , ' || ' , b.operator2 , ' ' ,  b.value2)  
           END)  
FROM b INNER JOIN
     a
     ON b.lsequence = a.lsequence  
WHERE ......conditions........;

The use of aggregations functions ( MAX() ) in the SELECT turns this into an aggregation query. With no GROUP BY , the query returns exactly one row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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