在过滤结果后,使用LIKE的SQL Case语句

时间:2015-10-12 00:51:48

标签: sql sql-server-2008-r2 crystal-reports

我不确定我的语法是否正确。我所拥有的是一个查询,它将两个查询中的ID列为一个。我已经在表格之间建立了链接,以尽可能减少结果。

这是我必须使用的查询。我关注的两个列是SampleCode和Replicate。 http://tinypic.com/r/2ym5zt1/8

输出需要如何格式化就像这个例子(注意:在这种情况下,%只是一个地方填充物):

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

基本上,它将每个样本与其他样本进行比较。我已经尝试过滤器来缩小选择范围,但事情并没有正确排列。我曾尝试编写一个case语句但由于某种原因,THEN子句的语法似乎不起作用。只有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

就RL而言,我不能使用精确的THEN语句,因为它可以是RL1%或RL1%-1。如果有办法通过SQL设置C%-1,C%-2 ......等,我宁愿这样做。但是,如果不可能,我将在Crystal Reports中对其进行格式化。

先谢谢。

这是查询

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)

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解这个问题,但语法不正确。您可以在LIKE之后使用WHEN,但不能在THEN之后使用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

尝试使用子查询。如果您可以设置SQLFiddle,我们可以为您提供更好的帮助。

document.documentElement.innerHTML

答案 1 :(得分:0)

所以这个有点有趣了。我从原始查询中获取了输出。样本并没有像我报告所需要的那样排成一行。所以我在SQL中做了最小的过滤。我把查询带入水晶报告中并制作了一些公式来过滤其余的。它实际上比我想象的要容易。谢谢大家的建议。