Click to See Complete Forum and Search --> : A tricky sql question


lsy
September 21st, 2005, 11:32 PM
if i got 2 table
Table1
-------
WorkWeek Value
28 212
28 242
28 435
28 534
29 254
29 542
30 984

Table2
------
WorkWeek ValueX
28 453
28 22
29 124
29 232
29 12
30 54
30 546
30 324

i want my result as
WorkWeek
Sum(Table1.Value) AS SumVal
COUNT(Table2.ValueX) AS CountValX
Sum(Table1.Value) / COUNT(Table2.ValueX) AS Answer

WorkWeek SumVal CountValX Answer
---------------------------------------------
28 1423 2 7115
29 706 3 265.33
30 984 3 328

so how can my sql statement should look like... join 2 table which get the sum of Value in Table1 and count the row of table 2 which both group by WorkWeek and take both to devided it...
Is it possible make it in one sql statement?

Zeb
September 22nd, 2005, 02:48 AM
SELECT t1.workweek,
t1.val AS SumVal,
t2.val AS CountValX,
t1.val / t2.val AS Answer
FROM
(SELECT workweek, SUM(Table1.Value) FROM Table1 GROUP BY WorkWeek) t1 INNER JOIN
(SELECT workweek, COUNT(Table2.ValueX) FROM Table2 GROUP BY WorkWeek) t2 ON t1.workweek = t2.workweekThat work? Should be close... Don't know how your Answer column example adds up to the definition you gave for it though.

(btw, haven't tested it so may not be 100%, but it gives you the idea)