SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Issue with subquery
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jauner
Starting Member

USA
19 Posts

Posted - 01/18/2010 :  09:38:52  Show Profile  Reply with Quote
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

United Kingdom
22403 Posts

Posted - 01/18/2010 :  09:54:11  Show Profile  Reply with Quote
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

India
22713 Posts

Posted - 01/18/2010 :  10:09:40  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

United Kingdom
22403 Posts

Posted - 01/18/2010 :  10:18:04  Show Profile  Reply with Quote
MIN(trans_date) + (CONVERT(int, MAX(trans_date) - MIN(trans_date)) / 2)

??
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000