简体   繁体   English

在THEN之后使用LIKE的SQL Case语句以过滤结果

[英]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. 我所拥有的是一个查询,该查询从两个合并为一个的查询中列出了ID。 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. 我关注的两列是SampleCode和Replicate。 http://tinypic.com/r/2ym5zt1/8 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. 我尝试编写一个case语句,但是由于某种原因,THEN子句的语法似乎不起作用。 Only for the C%R does it work. 仅适用于C%R。

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. 就RL而言,我不能使用确切的THEN语句,因为它可以是RL1%或RL1%-1。 If there's a way to set up the C%-1, C%-2...etc through SQL I'd rather go that way. 如果有办法通过SQL设置C%-1,C%-2 ...等,我宁愿那样做。 However if it isn't possible I'll format it in Crystal Reports. 但是,如果不可能,我将在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. 您可以在WHEN之后使用LIKE ,但不能在THEN之后使用。

Try using a subquery. 尝试使用子查询。 If you can setup a SQLFiddle we can assist you better. 如果您可以设置SQLFiddle,我们可以为您提供更好的帮助。

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. 因此,我在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. 谢谢大家的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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