Обсуждение: if statement

Поиск
Список
Период
Сортировка

if statement

От
"Devinder K Rajput"
Дата:
Hi,

I am trying to find out how to do an if statement/case statement inside a
select.  I am comparing item records to find out what is different.  Rather
than writing a query for comparing each field separately, I just want to
tell which field is different by printing the name of that field.  Thank
you.

select
  a.number,
  a.description_1,
  a.description_2,
  b.description_1,
  b.description_2,
  a.hub_id,
  b.hub_id,
  a.category,
  b.category,
  a.sub_category,
  b.sub_category,
--what I would like to say--
  if a.description_1 <> b.description_1 "description_1"
  else if a.description_2 <> b.description_2 "description_2"
  else if ...
---back to rest of query---
from items as a, items as b
where  a.number = b.number
     and a.hub_id <> b.hub_id
     and (a.category <> b.category OR a.sub_category <> b.sub_category OR
             a.prime_vendor <> b.prime_vendor OR a.description_1 <>
b.description_1 OR
             a.description_2 <> b.description_2 OR a.prc_unit <> b.prc_unit
OR
             a.alt_unit_1 <> b.alt_unit_1 OR a.alt_unit_2 <> b.alt_unit_2
OR
             a.alt_unit_3 <> b.alt_unit_3 OR a.prime_vendor_cost <>
b.prime_vendor_cost )
     and a.hub_id < b.hub_id




Devinder Rajput
Stores Division Corporate Offices
Chicago, IL
(773) 442-6474



Re: if statement

От
Ludwig Lim
Дата:
--- Devinder K Rajput <Devinder.Rajput@ipaper.com>
wrote:
> Hi,
>
> I am trying to find out how to do an if
> statement/case statement inside a
> select.  I am comparing item records to find out
> what is different.  Rather
> than writing a query for comparing each field
> separately, I just want to
> tell which field is different by printing the name
> of that field.  Thank
> you.

  Use the CASE clause, it simulates the if-then-else
statement in SQL.

Try the following:
  SELECT a.number,
         a.description_1,
         a.description_2,
         ....
         ....
         CASE
           WHEN a.description_1 <> b.description_1
THEN
              'description_1'
           WHEN a.description_2 <> b.description_2
THEN
              'description_2'
           /* Some more WHEN-THEN statements.... */
         END
   FROM .....
   WHERE .....


regards,
ludwig.


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com