Skip to content
Advertisement

How can I return desired record when using pivot

I am using a pivot table to return records that have been updated in the past 7 days. If account has been updated, only return contype ‘AAA’ and ‘BBB’ for the ID. Each ID can have up to 8 ‘types’. I want to only return records with cont_desc= 01 BUT contypes that are not AAA or BBB, cont_desc does not have to be 01. With that said, I want all cont_desc in pivot table to be 01. It’s a bit difficult to explain so I will give an example:

Table can look something like this:

Notice, ID 1 will get pulled because CCC has last update within last 7 days so it will return the AAA and BBB records of the ID. Also notice, ID 2 will get pulled because contype DDD has been updated within the last 7 days BUT it will pull AAA, id_cont_desc = 93021. I want records to have 01 at all times. I can’t include this in my where clause because records that aren’t AAA or BBB does not have to be 01.

It may return something like:

Record 1 looks great. All contdesc’s are equal to 01. Notice record 2, ‘AAA’_contdesc = 93021. This is because a record that is NOT equal to AAA or BBB (but has been updated in the past 7 days) but returned contype AAA with contdesc not equal to 01. I apologize in advance, this is a bit confusing but I tried my best.

Any suggestions are appreciated! Thanks in advance!

Advertisement

Answer

Is this what you want?

Then your desired results would appear to be:

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