Solved

SQL query help

Posted on 2014-01-03
13
452 Views
Last Modified: 2014-01-06
I have this SQL query:

Select       
      TR.Serial_Number,      
      MAX(CASE
        WHEN (TR.Test_Result LIKE '%PASS%' ) THEN  'Pass'
        ELSE ''      
      END)  as 'ChamberResults',
      MIN(TR.Date_Time) as 'ChamberDate'
FROM
      dbo.Test_Results TR WITH (NOLOCK)  
WHERE       
      (TR.Date_Time BETWEEN '2013-12-23 00:32:35.757' AND '2013-12-23 23:32:35.757')       
      AND (ISNULL(TR.Serial_Number,'')<>'') AND LEN(TR.Serial_Number) > = 14
      AND (System_ID LIKE '%Chamber%')       
Group By
      TR.Serial_Number



I want to modify the above query to pick only  the first unique serial_number based on the Date_Time ascending and do CASE statement check on the first serial_number...


For e.g: C037197 2089 13 appears 3 times , but I need the first row to evaluate CASE statement..ignore the other 2 rows...



Input Data is attached..

Any idea?

Thanks
Capture.PNG
0
Comment
Question by:mani_sai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39755152
THis can be achieved by using the row_number() function and a CTE

;WITH CTE AS 
(
SELECT ROW_NUMBER() OVER(partition by Serial_Number ORDER BY date_time) RN, * 
FROM dbo.test_results WITH (NOLOCK)
)
Select       
      TR.Serial_Number,      
       CASE WHEN rn = 1 THEN
       CASE  
        WHEN (TR.Test_Result LIKE '%PASS%' ) THEN  'Pass' 
        ELSE ''      
      END 
      ELSE '' END )  as 'ChamberResults',
      TR.Date_Time as 'ChamberDate'
FROM 
      CTE TR   
WHERE       
      (TR.Date_Time BETWEEN '2013-12-23 00:32:35.757' AND '2013-12-23 23:32:35.757')       
      AND (ISNULL(TR.Serial_Number,'')<>'') AND LEN(TR.Serial_Number) > = 14 
      AND (System_ID LIKE '%Chamber%')       

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 39755156
Thanks

how to run this from Query Analyzer?
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39755157
just copy and paste it in the query analyzer as you do it for all select queries....

No special thing required
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:mani_sai
ID: 39755158
I am  using SQL Server 2000   (8.0.2066)
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39755161
Then the above solution wont work, as you have sql 2005 and sql 2008 in your question, I assumed you are SQL 2005 or 2008, let me re-write this for SQL 2000 it will take a while.
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39755163
Sorry for that.. Thanks.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39755164
Try the below one

SELECT TR1.*,TR2.ChamberResults 
FROM dbo.Test_Results TR1 WITH (NOLOCK)  
LEFT JOIN (
Select       
      TR.Serial_Number,      
      MAX(CASE 
        WHEN (TR.Test_Result LIKE '%PASS%' ) THEN  'Pass' 
        ELSE ''      
      END)  as 'ChamberResults',
      MIN(TR.Date_Time) as 'ChamberDate'
FROM 
      dbo.Test_Results TR WITH (NOLOCK)  
WHERE       
      (TR.Date_Time BETWEEN '2013-12-23 00:32:35.757' AND '2013-12-23 23:32:35.757')       
      AND (ISNULL(TR.Serial_Number,'')<>'') AND LEN(TR.Serial_Number) > = 14 
      AND (System_ID LIKE '%Chamber%')       
Group By 
      TR.Serial_Number 
) AS TR2
ON   TR1.Serial_Number = TR2.Serial_Number
AND TR1.date_time = TR2.ChamberDate

Open in new window


Check the above out.
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39755174
It ran for 45 seconds and pulled all the data from dbo.Test_Results  table instead of just the filtered result...

normally the query runs in 2 seconds or less...
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39755180
My apologies, I missed the statement Ignore the other 2 rows in your question above

try the below
SELECT TR1.*,TR2.ChamberResults 
FROM dbo.Test_Results TR1 WITH (NOLOCK)  
JOIN (
Select       
      TR.Serial_Number,      
      MAX(CASE 
        WHEN (TR.Test_Result LIKE '%PASS%' ) THEN  'Pass' 
        ELSE ''      
      END)  as 'ChamberResults',
      MIN(TR.Date_Time) as 'ChamberDate'
FROM 
      dbo.Test_Results TR WITH (NOLOCK)  
WHERE       
      (TR.Date_Time BETWEEN '2013-12-23 00:32:35.757' AND '2013-12-23 23:32:35.757')       
      AND (ISNULL(TR.Serial_Number,'')<>'') AND LEN(TR.Serial_Number) > = 14 
      AND (System_ID LIKE '%Chamber%')       
Group By 
      TR.Serial_Number 
) AS TR2
ON   TR1.Serial_Number = TR2.Serial_Number
AND TR1.date_time = TR2.ChamberDate

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 39755190
Thanks, query is not working.. but not getting the expected results..

For e.g: C037197 2089 13  .....appears 3 times , but I need the first row (by earliest Date_time) to evaluate CASE statement..ignore the other 2 rows...

so my case statement should not pass the coil...because first row is failed...

where as C037197 2139 13...should appeared only once and passed, so it should pass..
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39755208
Got it working...

Thanks for your help...  Instead of MAX..i have to use MIN....


SELECT TR2.Serial_Number, TR2.ChamberResults , TR2.ChamberDate
FROM dbo.Test_Results TR1 WITH (NOLOCK)  
JOIN (
Select      
      TR.Serial_Number,      
      MIN(CASE
        WHEN (TR.Test_Result LIKE '%PASS%' ) THEN  'Pass'
        ELSE ''      
      END)  as 'ChamberResults',
      MIN(TR.Date_Time) as 'ChamberDate'
FROM
      dbo.Test_Results TR WITH (NOLOCK)  
WHERE      
      (TR.Date_Time BETWEEN '2013-12-19 00:32:35.757' AND '2013-12-23 23:32:35.757')      
      AND (ISNULL(TR.Serial_Number,'')<>'') AND LEN(TR.Serial_Number) > = 14
      AND (System_ID LIKE '%Chamber%')   AND     Serial_Number IN( 'C037197 2139 13', 'C037197 2089 13')
Group By
      TR.Serial_Number
) AS TR2
ON   TR1.Serial_Number = TR2.Serial_Number
AND TR1.date_time = TR2.ChamberDate
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39756714
Got it working...
Excellent! Now please close the question.
0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39759061
Thanks for your help.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 34
SQL Server Database Inventory 7 30
Please explain the difference between EXCLUDE, INTERSECT and JOIN 7 52
T-SQL Query 9 36
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question