0

how to DISTINCT to single row

here is my code:

SELECT 
wagon.name w_name,
'' as su_name,
'' as m_name,
'' as sk_name,
'' as p_name,
'' as b_name,
'' as o_name,
total w_total,
0 as su_total,
0 as m_total,
0 as sk_total,
0 as p_total,
0 as b_total,
0 as o_total

FROM wagoning 
LEFT JOIN wagon ON wagoning.wagon_id = wagon.id 

UNION ALL

SELECT 

'' w_name,
name su_name,
'' m_name,
'' sk_name,
'' p_name,
'' b_name,
'' o_name,
0 w_total,
total su_total,
0 m_total,
0 sk_total,
0 p_total,
0 b_total,
0 o_total

FROM drying 
LEFT JOIN drier ON drying.drier_id = drier.id 

UNION ALL

SELECT 

'' w_name,
'' su_name,
quantity::varchar as m_name,
'' sk_name,
'' p_name,
'' b_name,
'' o_name,
0 w_total,
0 su_total,
total m_total,
0 sk_total,
0 p_total,
0 b_total,
0 o_total

FROM
bagging

UNION ALL

SELECT 

'' w_name,
'' su_name,
'' m_name,
quantity::varchar as sk_name,
'' p_name,
'' b_name,
'' o_name,
0 w_total,
0 su_total,
0 m_total,
total sk_total,
0 p_total,
0 b_total,
0 o_total


FROM
storing

UNION ALL

SELECT 

'' w_name,
'' su_name,
'' m_name,
'' sk_name,
quantity::varchar as p_name,
'' b_name,
'' o_name,
0 w_total,
0 su_total,
0 m_total,
0 sk_total,
total p_total,
0 b_total,
0 o_total

FROM
processing

UNION ALL

SELECT 

'' w_name,
'' su_name,
'' m_name,
'' sk_name,
'' p_name,
quantity::varchar as b_name,
'' o_name,
0 w_total,
0 su_total,
0 m_total,
0 sk_total,
0 p_total,
quantity * net b_total,
0 o_total

FROM
boxing

UNION ALL

SELECT 

'' w_name,
'' su_name,
'' m_name,
'' sk_name,
'' p_name,
'' b_name,
'' o_name,
0 w_total,
0 su_total,
0 m_total,
0 sk_total,
0 p_total,
0 b_total,
part o_total

FROM
boxing

Please check the screenshot below: top result (above the red) how it is working now. What I want is below the red line as in single row

screenshot

3
  • Please don't add images. Please add your ouput and expected output to the question as text. Commented Dec 12, 2018 at 14:45
  • Please attach your schema of DB Commented Dec 12, 2018 at 15:55
  • StackOverflow thanks you for your contribution. Commented Dec 12, 2018 at 18:14

1 Answer 1

1

I suppose you want MAX over the query result of all UNION ALL

select MAX(w_name) as w_name,
       MAX(su_name)as su_name,
      -- ..  other columns

FROM
(
SELECT 
wagon.name w_name,
'' as su_name,
'' as m_name,
'' as sk_name,
'' as p_name,
'' as b_name,
'' as o_name,
total w_total,
0 as su_total,
0 as m_total,
0 as sk_total,
0 as p_total,
0 as b_total,
0 as o_total

FROM wagoning 
LEFT JOIN wagon ON wagoning.wagon_id = wagon.id 

UNION ALL

SELECT 
..
Sign up to request clarification or add additional context in comments.

4 Comments

Can't use max. It should be repeater in Ireport
@MaratRaimberdievich : But, I don't see that in your query, it has always only one non-null column per query
I tried but it didnt work on repeater. it shows only one row always. I need to repeat those tables as one row, and repeat for other records. Well a client wants those shitty reports :(
Well jasper iReport does the repeating part. I just give a row it will repeat for other records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.