Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing one query, to replace several simple queries

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB

I have a table that consists of the following…

dateStart dateEnd entries segment vol1 vol2 vol3 vol4
28/12/09 3/1/10 entrya segment1 2 2 2 3
28/12/09 3/1/10 entryc segment1 1 1 1 0
28/12/09 3/1/10 entryb segment0 7 1 1 0
28/12/09 3/1/10 entryd segment0 1 1 5 5
18/1/10 24/1/10 entrye segment1 1 65 1 0
28/12/09 3/1/10 entryf segment0 1 1 1 4
18/1/10 24/1/10 entryg segment1 1 1 1 0
28/12/09 3/1/10 entryh segment1 1 1 3 0


i can produce a count table like this using the following query…

Code:
SELECT dateEnd, COUNT(entries) 	
FROM table 	
WHERE segment = 'segment1' 	
AND segment  != '' 	
GROUP by dateEnd


Wk end COUNT(entries)
3/1/10 4983
10/1/10 5987
17/1/10 5665
24/1/10 6309
31/1/10 5808
7/2/10 9465
14/2/10 6410
21/2/10 5551


I can then produce a second sum table like this… and produce additonal tables for vol2, vol3, and so forth

Code:
SELECT dateEnd, SUM(vol1) 	
FROM table 	
WHERE segment = 'segment1' 	
AND segment  != '' 	
GROUP by dateEnd


Wk end SUM(vol1)
3/1/10 204
10/1/10 267
17/1/10 217
24/1/10 280
31/1/10 260
7/2/10 401
14/2/10 351
21/2/10 280


what I would like to do is write one query to produce the following table…

Wk end COUNT(entries) SUM(vol1) SUM(vol1) SUM(vol1)
3/1/10 4983 204 1784 556
10/1/10 5987 267 1581 588
17/1/10 5665 217 1600 540
24/1/10 6309 280 1788 541
31/1/10 5808 260 1628 508
7/2/10 9465 401 2400 685
14/2/10 6410 351 1798 543
21/2/10 5551 280 1708 525


and.. I would like to write another query that would produce a table with just a total average of each week ending…

Wk end COUNT(entries) SUM(vol1) SUM(vol1) SUM(vol1)
3/1/10 4983 204 1784 556
10/1/10 5987 267 1581 588
17/1/10 5665 217 1600 540
24/1/10 6309 280 1788 541
31/1/10 5808 260 1628 508
7/2/10 9465 401 2400 685
14/2/10 6410 351 1798 543
21/2/10 5551 280 1708 525
AVG 5485 236 1683 572
 
just realized i screwed up the first part of this question going to rewrite it... but the second part about the averages still stands
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top