Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Consider the table below

Name partno.  sch_date    WO#   owed panels
 aa   1234     08/22/2017  121   22    26
 aa   1234     08/22/2017  222   22    27
 aa   1234     08/22/2017  242   22    27
 aa   1234     08/29/2017  152   20    24
 aa   1234     08/29/2017  167   20    24
 aa   1234     08/29/2017  202   20    26

Is it possible to display the data in such way that when i read through the data if the first partno. in the table has the number of panels  greater than owed, then i don't won't to dispaly the other partno. schedule on the same date(sch_date).

Expected Result

Name partno.  sch_date    WO#   owed panels
 aa   1234     08/22/2017  121   22    26
 aa   1234     08/29/2017  152   20    24


What I have tried:

I am not sure how to imply the case function here.
Posted
Updated 29-Aug-17 0:16am
Comments
RedDk 10-Aug-17 13:34pm    
Look up DISTINCT in the BOL.

Try following ..

SQL
select TOP 1  Name,partno,sch_date,WO#,owed,panels from TableName
ORDER BY
    ROW_NUMBER() OVER(PARTITION BY sch_date ORDER BY WO# ASC)
 
Share this answer
 
Hello,
Check the below solution based on ROW_NUMBER().

SELECT ROW_NUMBER() OVER (PARTITION BY [Name],[sch_date] ORDER BY [Name],[sch_date]) AS RN,*
INTO #Temp
FROM #YourTable WHERE panels>owed

SELECT * FROM #Temp

RN Name partno# sch_date WO# owed panels
1 aa 1234 2017-08-22 00:00:00.000 121 22 26
2 aa 1234 2017-08-22 00:00:00.000 222 22 27
3 aa 1234 2017-08-22 00:00:00.000 242 22 27
1 aa 1234 2017-08-29 00:00:00.000 152 20 24
2 aa 1234 2017-08-29 00:00:00.000 167 20 24
3 aa 1234 2017-08-29 00:00:00.000 202 20 26

SELECT * FROM #Temp WHERE RN=1

Result:

RN Name partno# sch_date WO# owed panels
1 aa 1234 2017-08-22 00:00:00.000 121 22 26
1 aa 1234 2017-08-29 00:00:00.000 152 20 24


Thanks&Regards,
Pradeep Magati.
 
Share this answer
 
;WITH TempTable (Name,partno,sch_date,WO#,owed,panels, duplicateRecCount)
AS
(SELECT Name,partno,sch_date,WO#,owed,panels,ROW_NUMBER() OVER(PARTITION by Name,partno,sch_date,WO#,owed,panels ORDER BY Name)
AS duplicateRecCount FROM [Table Name]	
Select Name,partno,sch_date,WO#,owed,panels from TempTable where duplicateRecCount=1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900