-1

So I've been trying to get this working and it seems pretty straightforward and simple to me when I read it but for some reason it doesn't want to work on a simple query. I have this same CASE statement working on a different report that has multiple tables but this one doesn't want to work with only 3 tables. Can someone with a fresh set of eyes see where I went wrong? It's almost as if it's filtering the results rather than comparing the column.

SELECT dbo.Samples.BatchCode, dbo.SampRunTest.RunTstCode, 
       dbo.SampRunTest.RunCode, SampRunTest_1.RunTstCode AS ISO
FROM   dbo.SampRunTest 
       INNER JOIN dbo.Samples 
            ON dbo.SampRunTest.RunSmpCode = dbo.Samples.SampleCode 
       INNER JOIN dbo.SampRunTest AS SampRunTest_1 
            ON dbo.SampRunTest.RunCode = SampRunTest_1.RunCode 
               AND dbo.SampRunTest.RunAuditFlag = SampRunTest_1.RunAuditFlag
WHERE     (dbo.Samples.AuditFlag = 0) 
          AND (
              dbo.SampRunTest.RunCode = CASE 
                                           WHEN (dbo.sampruntest.runtstcode) LIKE 'U%' 
                                        THEN 'U' 
                                        ELSE (sampruntest.runtstcode) 
                                        END
               )

EDIT---------------- Let me try this again. So what I'm trying to do is use a CASE statement to filter my column based off of another column in my query. For example, I have 3 tables I'm querying.... SamprunTest...Samples...and Sampleresults. Sampleresults is the main table I'm using to return data but need to filter the results on SamprunTest. Below is an example of the data that I'm trying to receive out of the table based on my query. Right now the case statement listed above returns nothing.

Batchcode RunCode SampleCode RunTstCode ID Confidence 2014-236 14-13-15 C8787 AmTest NA 2014-236 14-13-15 C8787 PuTest NA 2014-236 14-13-15 C8787 GammaTest 0.9872

I know this is a crude representation but I'm horrible at explaining things. Basically what I'm trying to do is use a CASE statement (or some other method) to filter the ID Confidence column name on the RunTstCode column. It might help to mention that the "ID CONFIDENCE" column can contain multiple names and values. This table is set up very weird. In this instance, I'm trying to filter the "ID Confidence" table to filter out only the components that are named "ID Confidence" when the testcode is like gamma.

In plain text:

If runtstcode like 'Gamma%' then ID Confidence = 'ISO 1 ID Confidence' ELSE "NA"

0

1 Answer 1

2

Without knowing your schema or data, here is a generic way to debug a query.

First, run the query first without the WHERE clause. See what the data is coming back. Try this:

SELECT     dbo.Samples.BatchCode, dbo.SampRunTest.RunTstCode, dbo.SampRunTest.RunCode, SampRunTest_1.RunTstCode AS ISO
FROM         dbo.SampRunTest INNER JOIN
                  dbo.Samples ON dbo.SampRunTest.RunSmpCode = dbo.Samples.SampleCode INNER JOIN
                  dbo.SampRunTest AS SampRunTest_1 ON dbo.SampRunTest.RunCode = SampRunTest_1.RunCode AND 
                  dbo.SampRunTest.RunAuditFlag = SampRunTest_1.RunAuditFlag

If you don't get any data, then the problem is in your join somewhere. Start removing joins and change it to a SELECT * to see what you get.

If you do get data, run it again with the CASE section removed:

SELECT     dbo.Samples.BatchCode, dbo.SampRunTest.RunTstCode, dbo.SampRunTest.RunCode, SampRunTest_1.RunTstCode AS ISO
FROM         dbo.SampRunTest INNER JOIN
                  dbo.Samples ON dbo.SampRunTest.RunSmpCode = dbo.Samples.SampleCode INNER JOIN
                  dbo.SampRunTest AS SampRunTest_1 ON dbo.SampRunTest.RunCode = SampRunTest_1.RunCode AND 
                  dbo.SampRunTest.RunAuditFlag = SampRunTest_1.RunAuditFlag
WHERE     (dbo.Samples.AuditFlag = 0)

If that works, then the problem is in the CASE. Seeing the data returned should help understand the problem.

-- EDIT --

If the CASE is the problem, then try putting the CASE statement in your query like this:

SELECT     dbo.Samples.BatchCode, dbo.SampRunTest.RunTstCode, dbo.SampRunTest.RunCode, SampRunTest_1.RunTstCode AS ISO,
    CASE WHEN (dbo.sampruntest.runtstcode) LIKE 'U%'
         THEN 'U' 
         ELSE (sampruntest.runtstcode) 
    END AS [RunCodeShouldEqualThis],
    CASE WHEN dbo.SampRunTest.RunCode =
        CASE WHEN (dbo.sampruntest.runtstcode) LIKE 'U%' 
             THEN 'U' 
             ELSE (sampruntest.runtstcode) 
        END
        THEN 'Equals'
        ELSE 'Not Equals'
    END AS [WhereClauseCaseStatementResult]
FROM         dbo.SampRunTest INNER JOIN
                  dbo.Samples ON dbo.SampRunTest.RunSmpCode = dbo.Samples.SampleCode INNER JOIN
                  dbo.SampRunTest AS SampRunTest_1 ON dbo.SampRunTest.RunCode = SampRunTest_1.RunCode AND 
                  dbo.SampRunTest.RunAuditFlag = SampRunTest_1.RunAuditFlag
6
  • I'm sorry I should of been more specific!! Yes I do get data back with just the select statement but when I apply the case statement to it, it returns no data.
    – Braden
    Feb 26, 2015 at 23:16
  • Is there anymore information I can give that will be more helpful?
    – Braden
    Feb 26, 2015 at 23:49
  • You can try selecting out the results of the CASE statement. I updated the answer with a description of how to do that. Feb 27, 2015 at 21:16
  • I appreciate your response! I ran this query and everything in the WhereClauseCaseStatementResult column says "Not Equals". Here is a section of the output.2014-222 UTest 14-13-02 UTest U Not Equals
    – Braden
    Feb 28, 2015 at 22:04
  • Based on the order of the output you listed, the SampRunTest.RunCode (the 3rd column) is '14-13-02'. That does not equal 'U'. Mar 2, 2015 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.