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.
| Author |
Topic |
|
wooldawg
Starting Member
12 Posts |
Posted - 2008-03-13 : 09:16:24
|
| The query probably speaks for itself. You can see that I had to use a sub select to get the last column. It seems like there must be a better way, but we could not find it. We are on sql 2000. below is the query is the first few rows of the the result set. This query works, being new to sql I am just curious if the sub select could have been avoided in this case. Thanks!SELECT count(distinct a.store)as [# Stores doing at least 1 Verified Deposit], b.division, b.district, (select count(store_no) from store where district = b.district and fiscal_year = 2008 and current_status = 'a' and owner ='company')as '# of Stores in District' FROM depositvariance a, store bWHERE a.verified_date >'10/25/2007' and a.store = b.store_no and b.fiscal_year = 2008 and b.current_status = 'a' and b.owner ='company'Group BY b.division, b.district# Store that did at least 1 Verified Deposit division district # of Stores in District -------------------------------------------- -------- -------- ----------------------- 42 200 201 4428 200 202 4038 200 203 45 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-03-13 : 09:24:14
|
| try thisSELECT COUNT(DISTINCT a.Store) AS [# Store that did at least 1 Verified Deposit], b.Division, b.District, COUNT(b.Store_No) AS [# of Stores in District]FROM dbo.DepositVariance AS aLEFT JOIN Store AS b ON (b.Store_No = a.Store AND b.Fiscal_Year = 2008 AND b.Current_Status = 'a' AND b.Owner = 'Company')WHERE a.Verified_Date > '10/25/2007'GROUP BY b.Division, b.District |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-13 : 09:25:09
|
I think this will do the same thingSELECT COUNT(DISTINCT CASE WHEN a.Verified_Date > '10/25/2007' THEN a.Store ELSE NULL END) AS [# Store that did at least 1 Verified Deposit], b.Division, b.District, COUNT(DISTINCT a.Store) AS [# of Stores in District]FROM dbo.DepositVariance AS aINNER JOIN Store AS b ON b.Store_No = a.StoreWHERE b.Fiscal_Year = 2008 AND b.Current_Status = 'a' AND b.Owner = 'Company'GROUP BY b.Division, b.District But it is hard to tell without knowing more about your business rules. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
wooldawg
Starting Member
12 Posts |
Posted - 2008-03-13 : 10:05:53
|
| Thanks PeterNeo, I tried this first too, instead of counting the number of stores under each district in the store table, it takes the number of times each store occurs in the depositvarience table and counts that instead. # Store that did at least 1 Verified Deposit Division District # of Stores in District -------------------------------------------- -------- -------- ----------------------- 42 200 201 147628 200 202 84438 200 203 112618 200 204 490[quote]Originally posted by PeterNeo |
 |
|
|
wooldawg
Starting Member
12 Posts |
Posted - 2008-03-13 : 10:17:40
|
Peso, The depoist varience table contains a row for every time a store does the depoist varience function. It only contains the store number and a verified date. The store table contains all store numbers (store) along with district and division. The conditions of and b.fiscal_year = 2008 and b.current_status = 'a' and b.owner ='company'must be used because a store number may appear in the store table in more than one row. I'm not sure if this helps. Your query almost worked. I'm confused as to what it is doing though. district, real # of stores, Peso query201 44 44202 40 40203 45 38204 28 24205 27 25403 51 32404 52 49405 47 27501 37 37502 50 49503 48 48602 56 51603 39 8604 43 4605 25 1 |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|
|
|
|