Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:44+00:00 2026-06-13T22:57:44+00:00

I am in quite the pickle! I am checking a table that contains information

  • 0

I am in quite the pickle! I am checking a table that contains information related to patient vaccines. For each patient, there are three possible vaccines stored in four rows. These vaccines have a related yes/no value that depicts whether or not the vaccine was in effect. These vaccine rows each had a ‘static’ de_name column that held the name of the vaccine that was potentially being administered. If the vaccine had a yes value, there is also a related row that holds a date value of when the vaccine was administered. Luckily, this second row containing dates also contained the name of the field, which could be used to deduce which vaccine was administered. Due to the row nature of this table, there was no obvious way (to me) to search through each patient other than to turn each possibility into it’s own column and coalesce the results. This allowed me to create two pretty columns for both the administered vaccines and the date the vaccines were administered. This worked because I did not need to reference the rows that pertained to the yes/no value because the name of the vaccine and the fact that the date existed allowed me to use just the date field itself.

Everything was fine and dandy until I was asked to add a fourth vaccine, which instead of holding a yes/no value, contained free-text. I was asked to place the free-text in the same column as the other vaccines ‘static’ text. This is an issue because unlike the rest of the vaccines, there was no column in the fourth vaccines date row that displayed the non-date row vaccine free-text entry. I had to reference the fourth row’s free-text while also referencing the fourth row’s related vaccine date row’s date value. The issue comes in when, I have no idea how to join to rows (by a permanent visit number) within a Case statement within a coalesce statement! Any help would be wonderful!

Please let me know if there is any other data you need me to include. I am including apprehensively some dummy data and what my query would currently display.

Data_Points Table:

 De_Num       De_Name                        De_Value     Perm_Visit_Num    PatientID

 6224       Flu vaccine                        yes             10111           12
 6225    Flu vaccine date administered       11/00/2012        10111           12
 6226     Pneumonia vaccine                    yes             10112           12
 6227  Pneumonia vaccine date administered   11/01/2012        10112           12
 6228      Tetanus vaccine                     yes             10113           12
 6229   Tetanus vaccine date administered    11/02/2012        10113           12
 6230      Other vaccine                 arbitrary free-text   10114           12
 6231  Other vaccine date administered       11/03/2012        10114           12
 6224       Flu vaccine                        yes             10115           10
 6225    Flu vaccine date administered       11/00/2012        10115           10
 6226     Pneumonia vaccine                    yes             10116           10
 6227  Pneumonia vaccine date administered   11/01/2012        10116           10
 6228      Tetanus vaccine                     yes             10117           10
 6229   Tetanus vaccine date administered    11/02/2012        10117           10
 6230      Other vaccine              more arbitrary free-text 10118           10
 6231  Other vaccine date administered       11/03/2012        10118           10    

My Query

select * 
from
(select
        COALESCE(
            CASE
                WHEN de_num = '6225' THEN DE_Name
            END,
            CASE
                WHEN de_num = '6227' THEN DE_Name
            END,
            CASE
                WHEN de_num = '6229' and perm_visit_num in (select perm_visit_num from data_points where de_num = '6228' and de_value = 'Yes') THEN de_name
            END,
            CASE
                WHEN de_num = '6230' THEN DE_Value
            END
        ) VaccineAdministered,
        COALESCE(
            CASE
                WHEN de_num = '6225' THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
            END,
            CASE
                WHEN de_num = '6227' THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
            END,
            CASE
                WHEN de_num = '6229' and perm_visit_num in (select perm_visit_num from data_points where de_num = '6228' and de_value = 'Yes') THEN CASE DE_Value WHEN null THEN null ELSE DE_Value END
            END,
            CASE
                WHEN de_num = '6230' THEN 'foo'
            END
        )
from data_points
WHERE DE_Num in ('6227','6225','6229','6230')
)
where vaccineadministered != ''

The result set with that dummy data and the current sql query above:

Vaccine Administered                   DateAdministered

Flu vaccine date administered             11/00/2012
Pneumonia vaccine date administered       11/01/2012
Tetanus vaccine date administered         11/02/2012
arbitrary free-text                          foo
Flu vaccine date administered             11/00/2012
Pneumonia vaccine date administered       11/01/2012
Tetanus vaccine date administered         11/02/2012
more arbitrary free-text                     foo

My issue is that I cannot figure out a way to pull both the free-text AND the associated date. I can get either or, and in this query I get the free-text.

Please understand this is a very hard thing to explain and I am really in need of help. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:57:44+00:00Added an answer on June 13, 2026 at 10:57 pm

    After further reflection, this is what you need. I initially thought de_num was a primary key but I assume it is some kind of entry type. This isn’t such a bad structure then:

    Select
      v.vaccine As "Vaccine Administered",
      d.de_value As DateAdministered
    From (
      Select
        Perm_Visit_Num,
        Case When de_num = 6230 Then de_value else de_name end As vaccine
      From
        data_points
      Where
        de_num in (6224, 6226, 6228, 6230) -- change this to (6225, 6227, 6229, 6230) to display the exact results you had in mind
      ) v
      Inner Join (
      Select
        Perm_Visit_Num,
        de_value
      From
        data_points
      Where
        de_num in (6225, 6227, 6229, 6231)
      ) d
        On v.Perm_Visit_Num = d.Perm_Visit_Num
    

    This joins the vaccine record, with the administered date record in all cases. You can twiddle with the de_nums in the first part if you want to display the other values.

    This assumes that each perm_visit_num only has two entries. If you can get two ‘other vaccines’ in one visit, then this structure just plain can’t cope.

    http://sqlfiddle.com/#!3/4a8dd/16/0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is quite the pickle... I have a regular HTML form with a couple
Quite a few apps support plugins. Are there any downsides to having a large
Quite simply: is there a place such as DeviantArt, but purely for the purpose
quite some time ago i noticed that in Visual C++ 10 ADL fails when
Working on a project that requires that I am able to pickle the container
Quite by accident today, I discovered that in IE6, IE7 and IE8 it is
Quite a simple question. In SQL 2008 if I have a stored procedure (see
Quite simply I'd like to print out all variables that are in scope in
Quite a few years ago, I remember that I struggled with a multiple site
Quite new to this but i have created a SQL script that groups by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.