| Author |
Topic |
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-02 : 18:16:55
|
| The first person that is able to figure this out for me.. I will send $10 to you via check/mo/paypal/neteller, or I will take you out to lunch/dinner if you live in the vacinity - NO JOKE. I know it's not a lot.. but it's just a token of my appreciation for your help. I'm freakin stumped on this one as it goes way beyond my T-SQL abilities.I have a recordset that looks like the following:ID Score Per1 Per2 Category101 1 22 11 21101 1 58 11 21101 1 20 11 21101 2 12 89 22101 2 12 89 22101 2 76 89 22104 4 17 10 74104 1 25 10 74104 2 25 10 74104 3 33 10 74104 5 33 90 75104 5 33 90 75104 5 34 90 75First I need to do the following calculation in T-SQL somehow on each row... Which I can figure out.. but here is the formula:Score * (Per1 * .01) * (Per2 * .01) <--- for each rowThe part I can't figure out is how to take that score, sum up the total for EACH Category within an ID, then divide the two categories to get the average.So for ID 101 for instance.. I need to do something like:Category 21 = 1*22*.01*11*.01+ 1*58*.01*11*.01 +1*20*.01*11*.01 Category 22 = 2*12*.01*89*.01+2*12*.01*89*.01+2*76*.01*89*.01Then I need the avg of Cat21/22 ... ((Category 21 + Category 22) / 2)Then repeat the same thing for the next ID 104, then simply total the averages. In the recordset there will be 1 to Infinate # of ID's...In this case I just need:((Category 21 + Category 22) / 2) + ((Category 74 + Category 75) / 2)Again.. help I know there are better minds than mine out there who can write a query to do this. Or at least a cost efficient way to do this T-SQL.Thanks! -Jason |
|
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-02 : 18:26:23
|
| The answers you should come up with are:Category 21= 0.1100Category 22= 1.7800-AVG: 0.9450 Category 74= 0.2420Category 75= 4.5000-AVG: 2.37102.3710 + 0.9450 = 3.316 <-- this is the magic # i'm looking for (assuming I did the math right) |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 18:30:11
|
| I'm pretty close on this one, but it's not a easy one!Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-02 : 18:32:04
|
| Also... I cannot use a cursor to do this, so the results have to be returned within a recordset. (Or if you can figure out a way to do it w/out a cursor.. that'd be fine w/ me!) |
 |
|
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-02 : 18:33:52
|
quote: Originally posted by MichaelP I'm pretty close on this one, but it's not a easy one!Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
heh.. ya I know.. I've gotten close on it, but I just can't seem to figure out how to do the subsets of ID's and Categories then do the AVG's for each. My brain overloads at that point [CORE DUMP] :) |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 18:37:12
|
 Create Table #myTable (id int, score int, per1 int, per2 int, cat int)Insert Into #myTableSelect 101, 1, 22, 11, 21 Union AllSelect 101, 1, 58, 11, 21 Union AllSelect 101, 1, 20, 11, 21 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 76, 89, 22 Union AllSelect 104, 4, 17, 10, 74 Union AllSelect 104, 1, 25, 10, 74 Union AllSelect 104, 2, 25, 10, 74 Union AllSelect 104, 3, 33, 10, 74 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 34, 90, 75Select id, cat_21_22 = sum(case when cat in (21,22) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2, cat_74_75 = sum(case when cat in (74,75) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2, ttl = sum(case when cat in (21,22) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2 + sum(case when cat in (74,75) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2From #myTableWhere cat in (21,22,74,75)Group By id--EDIT: For the magic number...Select cat_21_22 = sum(case when cat in (21,22) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2, cat_74_75 = sum(case when cat in (74,75) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2, ttl = sum(case when cat in (21,22,74,75) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01) else 0 end)/2From #myTableWhere cat in (21,22,74,75)Drop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 18:39:16
|
| Corey, that doesn't handle N number of ID and N number of cats per ID though.....Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 18:40:40
|
| If I could figure out how to count the number of distinct categories per ID, I'd have this thing. It should be simple, but I can't get it.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 18:41:04
|
magic number matches though??... maybe the question could be clearer??Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 18:46:20
|
why do you need to count the distinct categories?? look at the math:quote: Category 21 = 1*22*.01*11*.01+ 1*58*.01*11*.01 +1*20*.01*11*.01 Category 22 = 2*12*.01*89*.01+2*12*.01*89*.01+2*76*.01*89*.01Then I need the avg of Cat21/22 ... ((Category 21 + Category 22) / 2)
each row calc is the same. lets call it f(rowNum)he wants to average the total of cat1 and cat2cat1 = f(cat1_1) + f(cat1_2) + ... + f(cat1_n)cat2 = f(cat2_1) + f(cat2_2) + ... + f(cat2_n)so (cat1 + cat2)/2 = f(cat1_1) + f(cat1_2) + ... + f(cat1_n) + f(cat2_1) + f(cat2_2) + ... + f(cat2_n)or (f(1) + ... + f(n))/2(cat21 + cat22)/2 + (cat74 + cat75)/2 = (cat21 + cat22 + cat74 + cat75)/2so sum every row calc and then divide by 2.Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 18:58:43
|
| Corey,If I read it right, the magic number is the sum of the average per ID.So, you need to know how many categories per ID there are, then sum the category totals, then divde that number by the number of categories per ID, then sum the averages per ID.It's hard putting that into words :)Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 19:03:26
|
fine.... does this pass your test  Create Table #myTable (id int, score int, per1 int, per2 int, cat int)Insert Into #myTableSelect 101, 1, 22, 11, 21 Union AllSelect 101, 1, 58, 11, 21 Union AllSelect 101, 1, 20, 11, 21 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 76, 89, 22 Union AllSelect 104, 4, 17, 10, 74 Union AllSelect 104, 1, 25, 10, 74 Union AllSelect 104, 2, 25, 10, 74 Union AllSelect 104, 3, 33, 10, 74 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 34, 90, 75Select ttl = sum(ttl)From (Select ttl = case when cat in (21,22,74,75) then convert(float,Score)*(convert(float,per1)*.01)*(convert(float,per2)*.01)/(Select count(distinct cat) From #myTable Where id=A.id) else 0 end From #myTable A Where cat in (21,22,74,75)) ZDrop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 19:05:00
|
YES!! I got it!!DECLARE @myTable TABLE(ID INT, Score INT, Per1 INT, Per2 INT, Category INT)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 1, 22, 11, 21)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 1, 58, 11, 21)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 1, 20, 11, 21)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 2, 12, 89, 22)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 2, 12, 89, 22)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(101, 2, 76, 89, 22)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 4, 17, 10, 74)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 1, 25, 10, 74)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 2, 25, 10, 74)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 3, 33, 10, 74)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 5, 33, 90, 75)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 5, 33, 90, 75)INSERT INTO @mytable(ID, Score, Per1, Per2, Category) VALUES(104, 5, 34, 90, 75)-- select ID, Category, (Score * (per1 * .01) * (Per2 * .01)) As TheForumlaThing-- FROM @MyTable/*SELECT ID, Count(*) FROM (SELECT ID, Category, COUNT(*) AS TheCount FROM @MyTable GROUP BY ID, category) C GROUP BY ID*/SELECT ID, SUM(SumOfCategory) AS SumPerIDINTO #MyTempTableFROM ( SELECT ID, Category, SUM(TheForumlaThing) AS SumOfCategory FROM ( select ID, Category, (Score * (per1 * .01) * (Per2 * .01)) As TheForumlaThing FROM @MyTable ) A GROUP BY a.ID, a.Category) BGROUP BY b.IDSELECT c.ID, Count(*) AS NumCatsPerID, (mtt.SumPerID / Count(*)) AS SumOfAverages FROM (SELECT ID, Category, COUNT(*) AS TheCount FROM @MyTable GROUP BY ID, category) C INNER JOIN #MyTempTable mtt ON mtt.ID = c.IDGROUP BY c.ID, mtt.SumPerID--The Final Answer!SELECT SUM(SumOfAverages)FROM ( SELECT c.ID, Count(*) AS NumCatsPerID, (mtt.SumPerID / Count(*)) AS SumOfAverages FROM ( SELECT ID, Category, COUNT(*) AS TheCount FROM @MyTable GROUP BY ID, category ) C INNER JOIN #MyTempTable mtt ON mtt.ID = c.ID GROUP BY c.ID, mtt.SumPerID) FDROP TABLE #MyTempTable Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-06-02 : 19:07:27
|
| OK, jaroot, which solution is the right one?Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 19:09:05
|
michael.. did you see my last one?? it works as you interpreted the problem...Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-06-03 : 04:21:22
|
Uh, what's going on ?What's wrong with this ?Why hardcoded values ?Why so much code ?What am I doing here ?Create Table #myTable (id int, score int, per1 int, per2 int, cat int)Insert Into #myTableSelect 101, 1, 22, 11, 21 Union AllSelect 101, 1, 58, 11, 21 Union AllSelect 101, 1, 20, 11, 21 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 12, 89, 22 Union AllSelect 101, 2, 76, 89, 22 Union AllSelect 104, 4, 17, 10, 74 Union AllSelect 104, 1, 25, 10, 74 Union AllSelect 104, 2, 25, 10, 74 Union AllSelect 104, 3, 33, 10, 74 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 33, 90, 75 Union AllSelect 104, 5, 34, 90, 75SELECT SUM(AvgById) AS magic_numberFROM ( SELECT id, AVG(SumByCat) AS AvgById FROM ( SELECT id, cat, .0001*SUM(Score*Per1*Per2) AS SumByCat FROM #myTable GROUP BY id, cat ) AS X GROUP BY id ) AS YDROP TABLE #myTable rockmoose |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-03 : 07:19:22
|
i guess all thats really needed is:Select ttl = sum(ttl)From (Select ttl = (convert(float,Score*per1*per2)*.0001)/(Select count(distinct cat) From #myTable Where id=A.id) From #myTable A) ZDrop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-06-03 : 07:50:51
|
| Yes, more compact !rockmoose |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-03 : 08:11:18
|
I think we managed to exhaust this topic, so, jaroot, does that cover it for you!?! Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-03 : 09:58:12
|
| Heh.. yes indeed. Just read all the solutions.. damn you guys are smart! Now who to award the prize too? I have to look a bit more to see who gave me the first answer that handles 1-n # of categories/ID's. |
 |
|
|
jaroot
Starting Member
46 Posts |
Posted - 2005-06-03 : 10:18:18
|
| Hmm.. Seventhenight gave me the first "elegent" solution, but he did have the hardcoded category values in the solution, which would not account for more category values in the recordset. Michael P gave the first (albiet non-elegent) solution that handles any N# of ID's.Ahh hell.. since you guys both took time out to figure out the answer, I'll pay ya both.Send your paypal requests to jaroot@charter.netI really appreciate you guys helping out this SQL noobie! |
 |
|
|
Next Page
|
|
|