×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help me abou DB2 Sql Code

Help me abou DB2 Sql Code

Help me abou DB2 Sql Code

(OP)
Good afternoon,

I need to make a select that makes full union between two records.

I used UNION ALL, but when it generates my SQL result, it lists the duplicate result and what I want is to merge both into one line.

For example:

In the first statement, I have:

Company ID, grade category (sale)

In the second statement, I have:

Company ID, Grade Category (Return)



When I generate it looks like this:

Company - Category

1 - SALE - RETURN

1 - RETURN - SALE



Being that, I wanted to unite like this:

Company - Category - Category

1 - SALE - RETURN

- "Below is the SQL I made so far" ***

SELECT

COMPANY,

CASE WHEN TIPOCATEGORIA = 'A' THEN

'SALE'

ELSE 'RETURN'

END AS TYPOCATEGORY,

CASE WHEN TIPOCATEGORIA = 'F' THEN

'SALE'

ELSE 'RETURN'

END AS TYPOCATEGORY

FROM

(

SELECT

COMPANY,

'A' AS TYPOCATEGORY

FROM

(

SELECT

COMPANY,

TYPOCHATEGORY

FROM

DBA.NOTAS_ENTRADA_IDAIDA

ONDE

NOTES_ENTRADA_SAIDA.TIPOCATEGORIA = 'A' AND

NOTES_ENGTH_OUT_DATE.DTMOVEMENT> '2019-08-05'

GROUP BY

COMPANY,

TYPOCHATEGORY

) TYPE

GROUP BY

TYPE.Company,

TYPE.TIPOCATEGORY



UNION ALL



SELECT

COMPANY,

'F' AS TYPOCATEGORY

FROM

(

SELECT

COMPANY,

TYPOCHATEGORY

FROM

DBA.NOTAS_ENTRADA_IDAIDA

ONDE

NOTES_ENTRADA_SAIDA.TIPOCATEGORIA = 'F' AND

NOTES_ENGTH_OUT_DATE.DTMOVEMENT> '2019-08-05'

GROUP BY

COMPANY,

TYPOCHATEGORY

) AS KINDS

GROUP BY

TYPE.Company,

TYPE.TIPOCATEGORY

) ALL

GROUP BY

COMPANY,

TYPOCHATEGORY

Please, show me the correct form. Thank you so much!

RE: Help me abou DB2 Sql Code

UNION will not return duplicate records.
UNION ALL returns everything, including duplicates, triplicates, etc.
Hope this helps.

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


RE: Help me abou DB2 Sql Code

Try:

CODE -->

SELECT S.COMPANY, S.CATEGORY, R.CATEGORY
  FROM TABLE S
      ,TABLE R
WHERE S.CATEGORY = 'SALE'
  AND S.COMPANY  = R.COMPANY
  AND R.CATEGORY = 'RETURN' 

This would take two lines and combine into 1. The problem with this is that it will only give a line if the Company has both sales and return data. If that is not what is required, then you will need an outer join .

Hope I have understood the problem and that this helps.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close