Consulting

Results 1 to 2 of 2

Thread: getting total use of values across multiple columns

  1. #1
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399

    getting total use of values across multiple columns

    Our system has a table called ledger

    services can have up to 12 additional descriptor codes these are stored as 12 fields in the same table marked dx1, dx2, dx3 etc

    I have worked up a script with the basic essence of what I am trying to get for a single column

    SELECT dx1, count(dx1) FROM Ledger group by dx1
    I tried using UNION but that lead to issues getting the count

    however I need to include the values from the other columns so I get a single list with total time each code is used across all 12 columns

    (I will also be adding a date range after this on the procedure date column but just trying to get the basics in first).
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  2. #2
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    Ok never mind looks like I found the function I needed

    UNPIVOT

    the final script was

    SELECT ICD9, Count(*) as TotalUses FROM Ledger 
    UNPIVOT( ICD9 FOR DX in ([dx1], [dx2], [dx3], [dx4], [dx5], [dx6], [dx7], [dx8], [dx9], [dx10], [dx11], [dx12])) as p 
    WHERE ProcedureDate >= '01/01/2001' AND ProcedureDate <= '01/01/2016' 
    group by ICD9
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

Posting Permissions

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