How to print the result in different line using SQL query?

£可爱£侵袭症+ 提交于 2019-12-08 09:23:32

问题


I am executing the below SQL query which result is displaying as in one line but instead of printing it it one line i want to split into two different line and print the value.

SQL Query:

select
(case when max(PK_MTF1000) = min(PK_MTF1000) and count(PK_MTF1000) = count(*) or max(PK_MTF1000) is null then 'same' else 'diff'end)as PK_MTF1000,max(PK_MTF1000),min(PK_MTF1000) + CHAR(13) ,
(case when max(MID) = min(MID) and count(MID) = count(*) or max(MID) is null then 'same' else 'diff'end) as MID,max(MID),min(MID)
from MTF1000 where ORG=' BULGER CAPITAL LLC';

OUTPUT:

same    NULL  NULL diff 1962008204906400    1962008204823K00

Expecting Output split into two different line and print the output as below

same    NULL  NULL

diff    1962008204906400    1962008204823K00

Tried with CHAR(13)

select
(case when max(PK_MTF1000) = min(PK_MTF1000) and count(PK_MTF1000) = count(*) or max(PK_MTF1000) is null then 'same' else 'diff'end)as PK_MTF1000,max(PK_MTF1000),min(PK_MTF1000) + CHAR(13) +  --<--,
(case when max(MID) = min(MID) and count(MID) = count(*) or max(MID) is null then 'same' else 'diff'end) as MID,max(MID),min(MID)
from MTF1000 where ORG=' BULGER CAPITAL LLC';

Getting error as :

-ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 2 Column: 189

Is there any way to split into two line?


回答1:


I think you probably want a UNION, so each set of data has its own row.

select
    (case when max(PK_MTF1000) = min(PK_MTF1000) and count(PK_MTF1000) = count(*) or max(PK_MTF1000) is null then 'same' else 'diff'end)as PK_MTF1000, 
    max(PK_MTF1000), 
    min(PK_MTF1000)
from MTF1000 where ORG=' BULGER CAPITAL LLC'
union all
select
    (case when max(MID) = min(MID) and count(MID) = count(*) or max(MID) is null then 'same' else 'diff'end) as MID, 
    max(MID), 
    min(MID)
from MTF1000 where ORG=' BULGER CAPITAL LLC';



回答2:


Concatenation operator is double pipe ||, not plus +.

Also, try with CHR(10) instead of CHR(13).

Which means that line we're talking about might look like

MIN (PK_MTF1000) || CHR (10),



回答3:


You are trying to print it like a columns. So you will not get the desired result even after using this CHR function.

You need to contact all the columns into one to get the desired result.

-- table structure -- see the "," between column names
SELECT
    '1'
    || CHR(10) as col1,
    '2' as col2
FROM
    DUAL;

output -- not as expected
------
CO C
-- -
1  2


-- Printable structure -- No "," -- single column output 
SELECT
    '1'
    || CHR(10) 
    || '2' as col1
FROM
    DUAL;

output -- As expected
------
COL
---
1
2


-- Your query should be written something like this
SELECT
    PK_MTF1000
    || '  '
    || CASE WHEN MAX1 IS NULL THEN 'NULL' ELSE MAX1 END -- CHANGED THIS LINE
    || '  '
    || CASE WHEN MIN1 IS NULL THEN 'NULL' ELSE MIN1 END -- CHANGED THIS LINE
    || CHR(10)
    || MID
    || '  '
    || MID1
    || '  '
    || MID2
FROM
    (
        SELECT
            ( CASE
                WHEN MAX(PK_MTF1000) = MIN(PK_MTF1000)
                     AND COUNT(PK_MTF1000) = COUNT(*)
                     OR MAX(PK_MTF1000) IS NULL THEN 'same'
                ELSE 'diff'
            END ) AS PK_MTF1000,
            MAX(PK_MTF1000) AS MAX1,
            MIN(PK_MTF1000) AS MIN1,
            ( CASE
                WHEN MAX(MID) = MIN(MID)
                     AND COUNT(MID) = COUNT(*)
                     OR MAX(MID) IS NULL THEN 'same'
                ELSE 'diff'
            END ) AS MID,
            MAX(MID) AS MID1,
            MIN(MID) AS MID2
        FROM
            MTF1000
        WHERE
            ORG = ' BULGER CAPITAL LLC'
    );

Please adjust the format yourself according to the live data.

Cheers!!



来源:https://stackoverflow.com/questions/57076011/how-to-print-the-result-in-different-line-using-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!