Skip to content
Advertisement

How to select data where more than one column is populated

I have a sample table, called [SAMPLES] which I’ve added multiple columns to the end for different types of lab analysis results, however for the majority of samples only one analysis has been completed. I want to be able select only the samples where more than one analysis has been completed on the same sample.

The table structure is something like:

enter image description here

For the above, the query should select only samples ‘ABC122’ and ‘ABC123’.

Is there an easy way to select this data out?

  • SQL 2012 database.

Advertisement

Answer

You can use cross apply and values to easily unpivot your columns back into the rows they should be, then count and filter accordingly

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement