www.webdeveloper.com
Results 1 to 5 of 5

Thread: Is there a better/more concise way to write this sql query?

Hybrid View

  1. #1
    Join Date
    Jun 2011
    Location
    California
    Posts
    383

    Cool Is there a better/more concise way to write this sql query?

    Instead of using 'case' as many times as i do?


    Code:
    select
    case
    when b.patient_id is null then a.dfcode else b.dfcode
    end as dfcode,
    case
    when b.patient_id is null then a.diagcode else b.diagnosis
    end as diasgnosis,
    case
    when b.patient_id is null then a.times_testing else b.times_testing
    end as times_testing,
    case
    when b.patient_id is null then a.on_insulin else b.insulin
    end as insulin
    from event.dbo.patient_dg() a
    left outer join event.dbo.diabetes_rx_tracker b on a.code=b.patient_id and b.image_id='2110180'
    where a.code='ADSMANZS01'

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Looks like IFNULL() could contribute to some conciseness.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2011
    Posts
    59
    if you on SQL server this will work
    Code:
    select ISNULL(a.dfcode, b.dfcode) as dfcode,
    ISNULL(a.diagcode, b.diagcode) as diagcode,
    ISNULL(a.times_testing, b.times_testing) as times_testing,
    ISNULL(a.on_insulin, b.on_insulin) as on_insulin,
    from event.dbo.patient_dg() a
    left outer join event.dbo.diabetes_rx_tracker b on a.code=b.patient_id and b.image_id='2110180'
    where a.code='ADSMANZS01'

  4. #4
    Join Date
    Jun 2011
    Location
    California
    Posts
    383
    that looks excellent...

    i've never used the ISNULL function.. i think it will work a treat!


    =)

  5. #5
    Join Date
    Jun 2011
    Location
    California
    Posts
    383
    I have another question regarding duplicates and inserting...

    if first name last name and phone number do not exist... than insert... if they do exist.. do nothing... here's what i have so far...


    $exists = sql::value("Select 1 from event.aps.trainer where first = $first, last = $last, trainer_phone = $trainer_phone");
    if (!$exists) sql::safe_insert('event', 'aps', 'trainer', $trainer_hex);
    else
    {
    IGNORE ('event', 'aps', 'trainer', $trainer_hex);
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles