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
 General SQL Server Forums
 New to SQL Server Programming
 Issue with subquery

Author  Topic 

jauner
Starting Member

19 Posts

Posted - 2010-01-18 : 09:38:52
I have the following SQL:


select jobtran.trans_date
,job.job
,job.suffix
,job.item
,jobtran.wc
,Sum(jobtran.qty_complete) as Gross_qty
,(Select top 1 qty_complete as Net_Qty from GAI_Previous_Job_Info where GAI_Previous_Job_Info.job = job.job and
GAI_Previous_Job_Info.suffix = job.suffix - 1) as net_qty
,(Select top 1 unit_weight from GAI_Previous_Job_Info where GAI_Previous_Job_Info.job = job.job and
GAI_Previous_Job_Info.suffix = job.suffix - 1) as unit_weight
,Sum(CASE When jobtran.trans_type = 'C' Then a_hrs Else 0 End) as run_hrs
from job,jobtran
where jobtran.wc BETWEEN @WCStart and @WCEND and jobtran.trans_date BETWEEN @PStartingTranDate and @PEndingTranDate
and jobtran.job = job.job and jobtran.suffix = job.suffix
and jobtran.trans_type in('R','C')

group by jobtran.trans_date,job.job,job.suffix,job.item,jobtran.wc
order by jobtran.wc


When I have multiple transactions such as two different transaction dates or the same date at a different time i get net_qty repeated is there anyway to avoid this??

Kristen
Test

22859 Posts

Posted - 2010-01-18 : 09:54:11
So jobtran.trans_date is not unique for all the other values you want to show?

If so use:

SELECT MIN(jobtran.trans_date) AS trans_date

and drop it from the GROUP BY list.

Or you could use AVERAGE(jobtran.trans_date) if you prefer
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-18 : 10:09:40
quote:
Originally posted by Kristen

So jobtran.trans_date is not unique for all the other values you want to show?

If so use:

SELECT MIN(jobtran.trans_date) AS trans_date

and drop it from the GROUP BY list.

Or you could use AVERAGE(jobtran.trans_date) if you prefer


AVERAGE(jobtran.trans_date)?

So you have written any ready-made function?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-18 : 10:18:04
MIN(trans_date) + (CONVERT(int, MAX(trans_date) - MIN(trans_date)) / 2)

??
Go to Top of Page
   

- Advertisement -