0

Example:

select c_id,'VIP Customer' [Customer Level] from customer where ...

union

select c_id,'Regular Customer' [Customer Level] from customer where ...

if c_id is part of first level (VIP) can't be 2nd level(Regular). I am using Union it's not working because second column data is different.

Can someone suggest?

Sergej
  • 1,082
  • 11
  • 27
user5342687
  • 55
  • 1
  • 1
  • 5

5 Answers5

0

I'm not sure exactly which SQL variant you are using but you may find it helpful to look at the 'case' expression rather than a union, see:

How do I perform an IF...THEN in an SQL SELECT?

Community
  • 1
  • 1
0

You could check, if the result-id was included in the first query. So something like this:

SELECT c_id,'VIP Customer' [Customer Level] from customer where [CONDITION]
UNION
(select c_id,'Regular Customer' [Customer Level]
from customer where [SECOND_CONDITION] AND
c_id not in 
(SELECT c_id,'VIP Customer' [Customer Level] from customer where [CONDITION]))
Dencrash
  • 133
  • 1
  • 7
0

So you're getting like ...

1, 'VIP'

1, 'Regular'

?

If so, I'm not sure if the where clause is 100% done yet. If it finds the same customer to be multiple types, that might be a red flag. If the customer is saved as multiple types, then a case statement is probably the way to go in this situation instead of a union.

select c_id, case when... then 'VIP' when... then 'Regular' end
Shockwave
  • 412
  • 2
  • 9
0

this is the error Client query.

select A.RC_id
from (
      select c_id RC_id
      from customer
      where 'Regular Customer' criteria) A
inner join (
      select c_id VIP_id
      from customer
      where 'VIP Customer' criteria) B
on A.RC_id=B.VIP_id

or you can apply both criteria on the Client, i think is the shortest solution

select c_id
from customer
where ('VIP Customer criteria' AND 'Regular Customer criteria')
Mattia Caputo
  • 959
  • 1
  • 8
  • 17
0

CASE seems to be a nice solution here, you can add more Customer Levels if needed also.

select c_id, [Customer Level] = 
    CASE
        WHEN ... THEN  'VIP Customer'
        ELSE 'Regular Customer'
    END

 from customer 

More about CASE on MSDN: https://msdn.microsoft.com/en-us/library/ms181765.aspx

Daniel Stackenland
  • 3,149
  • 1
  • 19
  • 22