简体   繁体   中英

SQL Case statement with LIKE after THEN to filter results

I'm not sure if I'm getting the syntax right on this. What I have is a query that is listing out ID's from two queries that are combined into one. I've made links between the tables to slim down the results as much as possible.

This is the query that I have to work with. The two columns I'm focused on are the SampleCode and Replicate. http://tinypic.com/r/2ym5zt1/8

How the output needs to be formatted is much like this example (Note: the % in this case is just a place filler):

C%    C%R
C%-1  C%-2
C%-1  C%-3
C%-2  C%-3
RL%   RLD%
RL%-1 RLD%-1

Essentially it's comparing each and every sample to the others. I've tried putting filters to narrow down the selection but things don't line up properly. I've tried writing a case statement but for some reason the syntax of the THEN clause doesn't seem to work. Only for the C%R does it work.

CASE WHEN SAMPLES_1.SAMPLECODE LIKE 'C%R%' THEN LEFT(SAMPLES.SAMPLECODE,6)
     WHEN SAMPLES_1.SAMPLECODE LIKE 'RLD%' THEN SAMPLES.SAMPLECODE LIKE 'RL1%'
END

As far as the RL is concerned, I can't use an exact THEN statement because it can be either an RL1% or and RL1%-1. If there's a way to set up the C%-1, C%-2...etc through SQL I'd rather go that way. However if it isn't possible I'll format it in Crystal Reports.

Thanks in advanced.

Here is the query

SELECT DISTINCT 
                  TOP (100) PERCENT dbo.Samples.BatchCode, dbo.Samples.SampleCode, Samples_1.SampleCode AS Replicate, dbo.SampleResults.ComponentName AS Activity, 
                  dbo.SampleResults.EnteredValue AS [Activity Ent], dbo.SampleResults.ComponentValue AS [Activity Comp], SampleResults_1.ComponentName AS [Activity Rep], 
                  SampleResults_1.EnteredValue AS [Activity Rep Ent], SampleResults_1.ComponentValue AS [Activity Rep Comp], SampRunTest_1.RunTstCode, 
                  dbo.SampRunTest.RunTstCode AS [Rep test], dbo.SampRunTest.RunCode, SampRunTest_1.RunCode AS [Rep RunCode], 
                  SampleResults_2.EnteredValue AS [ISO 1 Ent], SampleResults_3.EnteredValue AS [ISO 2 Ent], SampleResults_4.ComponentName AS [1s TPU], 
                  SampleResults_4.EnteredValue AS [1s TPU Ent], SampleResults_4.ComponentValue AS [1s TPU Comp], SampleResults_5.ComponentName AS [1s TPU Rep], 
                  SampleResults_5.EnteredValue AS [1s TPU Ent Rep], SampleResults_5.ComponentValue AS [1s TPU Comp Rep], SampleResults_6.EnteredValue AS [Units Ent], 
                  dbo.Samples.SubstanceCode
FROM         dbo.Samples INNER JOIN
                  dbo.SampRunTest AS SampRunTest_1 ON dbo.Samples.SampleCode = SampRunTest_1.RunSmpCode AND 
                  dbo.Samples.AuditFlag = SampRunTest_1.RunAuditFlag INNER JOIN
                  dbo.SampleResults ON SampRunTest_1.RunSmpCode = dbo.SampleResults.SampleCode AND 
                  SampRunTest_1.RunSmpTstPos = dbo.SampleResults.TestPosition AND SampRunTest_1.RunAuditFlag = dbo.SampleResults.AuditFlag INNER JOIN
                  dbo.SampRunTest INNER JOIN
                  dbo.Samples AS Samples_1 ON dbo.SampRunTest.RunAuditFlag = Samples_1.AuditFlag AND dbo.SampRunTest.RunSmpCode = Samples_1.SampleCode INNER JOIN
                  dbo.SampleResults AS SampleResults_1 ON dbo.SampRunTest.RunAuditFlag = SampleResults_1.AuditFlag AND 
                  dbo.SampRunTest.RunSmpCode = SampleResults_1.SampleCode AND dbo.SampRunTest.RunSmpTstPos = SampleResults_1.TestPosition ON 
                  dbo.Samples.BatchCode = Samples_1.BatchCode AND SampRunTest_1.RunTstCode = dbo.SampRunTest.RunTstCode AND 
                  SampRunTest_1.RunCode = dbo.SampRunTest.RunCode AND dbo.SampleResults.ComponentName = SampleResults_1.ComponentName INNER JOIN
                  dbo.SampleResults AS SampleResults_2 ON dbo.SampleResults.TestPosition = SampleResults_2.TestPosition AND 
                  dbo.SampleResults.SampleCode = SampleResults_2.SampleCode AND dbo.SampleResults.AuditFlag = SampleResults_2.AuditFlag AND 
                  LEFT(dbo.SampleResults.ComponentName, 5) = SampleResults_2.ComponentName INNER JOIN
                  dbo.SampleResults AS SampleResults_3 ON SampleResults_1.TestPosition = SampleResults_3.TestPosition AND 
                  SampleResults_1.SampleCode = SampleResults_3.SampleCode AND SampleResults_1.AuditFlag = SampleResults_3.AuditFlag AND 
                  LEFT(SampleResults_1.ComponentName, 5) = SampleResults_3.ComponentName INNER JOIN
                  dbo.SampleResults AS SampleResults_4 ON SampleResults_2.TestPosition = SampleResults_4.TestPosition AND 
                  SampleResults_2.SampleCode = SampleResults_4.SampleCode AND SampleResults_2.AuditFlag = SampleResults_4.AuditFlag AND 
                  LEFT(dbo.SampleResults.ComponentName, 6) + '1s TPU' = SampleResults_4.ComponentName INNER JOIN
                  dbo.SampleResults AS SampleResults_5 ON SampleResults_3.TestPosition = SampleResults_5.TestPosition AND 
                  SampleResults_3.SampleCode = SampleResults_5.SampleCode AND SampleResults_3.AuditFlag = SampleResults_5.AuditFlag AND 
                  LEFT(SampleResults_1.ComponentName, 6) + '1s TPU' = SampleResults_5.ComponentName INNER JOIN
                  dbo.SampleResults AS SampleResults_6 ON SampleResults_4.TestPosition = SampleResults_6.TestPosition AND 
                  SampleResults_4.SampleCode = SampleResults_6.SampleCode AND SampleResults_4.AuditFlag = SampleResults_6.AuditFlag
WHERE     (dbo.Samples.AuditFlag = 0) AND (dbo.SampleResults.ComponentName LIKE '%activity') AND (SampleResults_1.ComponentName LIKE '%activity') AND 
                  (NOT (SampRunTest_1.RunTstCode LIKE '%gamma%')) AND (NOT (dbo.SampRunTest.RunTstCode LIKE '%gamma%')) AND 
                  (dbo.SampleResults.EnteredValue IS NOT NULL) AND (NOT (SampleResults_1.EnteredValue LIKE '%value%')) AND (SampleResults_6.ComponentName = 'Test Code') 
                  AND (NOT (dbo.SampleResults.EnteredValue LIKE '%value%')) AND (Samples_1.SampleCode LIKE 'C%R' OR
                  Samples_1.SampleCode LIKE 'RLD%' OR
                  Samples_1.SampleCode LIKE '%-2' OR
                  Samples_1.SampleCode LIKE '%-4' AND NOT (Samples_1.SampleCode LIKE 'C%-1') AND NOT (Samples_1.SampleCode LIKE 'C%-3')) AND 
                  (NOT (dbo.Samples.SampleCode LIKE 'RB%') AND NOT (dbo.Samples.SampleCode LIKE 'RLD%') AND NOT (dbo.Samples.SampleCode LIKE '%-2') AND 
                  NOT (dbo.Samples.SampleCode LIKE '%-4') AND dbo.Samples.SampleCode <> Samples_1.SampleCode)

I'm not sure I completely understand the question, but the syntax is incorrect. You can use LIKE following WHEN , but not after THEN the way you have it.

Try using a subquery. If you can setup a SQLFiddle we can assist you better.

CASE WHEN SAMPLES_1.SAMPLECODE LIKE 'C%R%'
        THEN LEFT(SAMPLES.SAMPLECODE, 6)
    WHEN SAMPLES_1.[REPLICATE] LIKE 'RLD%'  -- Changed to the Replicate column
        THEN (
            SELECT TOP 1 SAMPLECODE
            FROM dbo.Samples
            WHERE SAMPLECODE LIKE 'RL1%'
            ORDER BY SAMPLECODE )
    END

So this one was a little interesting to figure out. I took the output that I was getting from my original query. The samples weren't lined up at all like I needed for my report. So I did minimal filtering in SQL. I took the query into crystal reports and made some formulas to filter the rest. It was actually easier than I thought. Thanks for everyone's suggestion though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM