Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select only First Valid Summed Row

Author  Topic 

rosshughes
Starting Member

16 Posts

Posted - 2010-09-15 : 17:23:59
Hi All,

I have a stored procedure that accumulates a labor quantity and then selects only the records where the final labor quantity is greater or equal to the production quantity.

It works great except I am getting multiple records that are greater than the production quantity. For example, production quantity is 100, then when the 3rd labor transaction fulfills the 100 it displays. Then, if another labor transaction occurs and brings it to 110 that will display as well.

How can I only select the first record that fulfills this condition?

Any help is appreciated.

Thanks,
Ross

My Select statement is below:

-------------------------------------------
SELECT counter,jcdept,resourcegrpid,jobnum,prodqty,partnum,oprseq,opdescription,duedate,laborqty,payrolldate,assemblyseq,ontime,late,finalqtycmp
FROM
(SELECT t.counter,t.Company,t.plant,t.jcdept,t.resourcegrpid,t.jobnum,t.prodqty,t.partnum,t.oprseq,t.opdescription,t.duedate,
t.laborqty,t.payrolldate,t.labortype,t.employeenum,t.labordtlseq,t.laborentrymethod,t.jobclosed,t.number20,t.assemblyseq,
t.ontime,t.late,t.laborqty+COALESCE(t1.TotalComplete,0) AS FinalQtyCmp
FROM @OperationsTable t
OUTER APPLY (SELECT SUM(laborqty) AS TotalComplete
FROM @OperationsTable
WHERE jobnum=t.jobnum AND oprseq=t.oprseq
AND counter < t.counter
AND counter > t.counter - 100)t1) AS SumBaseTable
WHERE FinalQtyCmp > prodqty

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 17:24:41
TOP 1?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2010-09-15 : 17:37:03
Hi Tara,

Thanks for the response.

TOP 1 only returns the 1st record of the entire summed data set where I am looking at returning the first record that fulfills the FinalQtyComp > prodqty condition for EACH operation.

I just added TOP 1 to my select statement. Maybe I am doing something wrong. Do I need to add grouping for it to work correctly?

Thanks,
Ross
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 17:43:56
Please post sample data of what your query is currently returning and then what you want it to return.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2010-09-15 : 17:54:07
Here is abbreviated data. I only want it to return 19 out of 19, 20 and 21. 20 and 21 do meet the criteria but I only need the first record that acheives the condition (263 is greater than 152). The rest of the dataset is fine.
---------------------------------------

2 110 20X26 006690 152.00 30796 10 106.00 166.00
19 115 SBENCH 006690 152.00 30796 70 116.00 263.00
20 115 SBENCH 006690 152.00 30796 70 2.00 265.00
21 115 SBENCH 006690 152.00 30796 70 15.00 280.00

23 115 SGRIND 006690 152.00 30796 60 93.00 166.00
26 115 SMCOREO 006690 152.00 30796 40 76.00 166.00
29 115 SMSAW 006690 152.00 30796 50 68.00 162.00
32 132 AGEOVEN 006690 152.00 30796 90 71.00 161.00
35 132 FINSP 006690 152.00 30796 110 114.00 161.00
38 132 HEATTRT 006690 152.00 30796 80 71.00 161.00
41 132 PASSTHR 006690 152.00 30796 100 71.00 161.00
47 480 MAKA55 006690 152.00 30796 120 21.00 173.00
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-15 : 17:59:08
You can use the ROW_NUMBER() function for this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rosshughes
Starting Member

16 Posts

Posted - 2010-09-15 : 18:14:34
Thanks. I have never used that function; I will explore that.

Do you have any examples or pointers?

Thanks,
Ross
Go to Top of Page
   

- Advertisement -