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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 [SOLVED ]Help With Correlated Subquery
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Pace
Constraint Violating Yak Guru

United Kingdom
264 Posts

Posted - 12/12/2006 :  04:25:12  Show Profile  Reply with Quote
Hi All,

Im just messing about really and have made this query which shows me all my customers that have never had a quote from us;

SELECT
		COUNT(*) AS NoQuotes
FROM
		Customer AS C
		WHERE NOT EXISTS
		(
		SELECT
			*
		FROM
			Quotes AS Q
		WHERE
			Q.[Cust Code] = C.Code
		)


What I want to do is in a single query, also obtain a COUNT(*) From my customers, then subtract the value of no quotes but, so I can see everything.... So I would end up with;

Customers CustomersWithoutQuote Difference
-----------------------------------------------
10 8 2

"Impossible is Nothing"

Edited by - Pace on 12/12/2006 06:08:27

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 12/12/2006 :  04:36:45  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT count(*) as [Customers], sum(case when q.[cust code] is null then 1 else 0 end) as [Customers Without Quote]
from (select distinct code from customers) c
left join (select distinct [cust code] from quotes) q on q.[cust code] = c.code


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Pace
Constraint Violating Yak Guru

United Kingdom
264 Posts

Posted - 12/12/2006 :  06:08:02  Show Profile  Reply with Quote
Peso, you are truly an Animal! [:-p]

Thank you sir!

"Impossible is Nothing"
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 12/12/2006 :  06:49:11  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Pace

Peso, you are truly an Animal! [:-p]

Thank you sir!

"Impossible is Nothing"

What did you mean by Animal?

Madhivanan

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

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 12/12/2006 :  06:53:46  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
And for differnec you can use

Select Customers,[Customers Without Quote],Customers-[Customers Without Quote] as Difference
from
(
SELECT count(*) as [Customers], sum(case when q.[cust code] is null then 1 else 0 end) as [Customers Without Quote] from (select distinct code from customers) c
left join (select distinct [cust code] from quotes) q on q.[cust code] = c.code
) T


Madhivanan

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

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 12/12/2006 :  07:52:20  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Shouldn't the simple difference be calculated in front-end application?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 12/12/2006 :  11:22:22  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Peso

Shouldn't the simple difference be calculated in front-end application?


Peter Larsson
Helsingborg, Sweden


Yes

Madhivanan

Failing to plan is Planning to fail
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.08 seconds. Powered By: Snitz Forums 2000