Author |
Topic |
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 09:23:51
|
i have a table if diffrent numbers.each number can appear X>=0 times.i want to count each number maximi of 5 times (or less if there are lest times of that number)thnaks in advancepelgIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-14 : 09:29:42
|
Can you make your reqmnt clear with some sample data and your expected o/p out of that data sample? |
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 09:29:47
|
[code]select number, case when count(number) > 5 then 5 else count(number) end from table group by number[/code]Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 10:37:36
|
RyanRandall - thanks its almost therei will describe more the tablenumber | txt================33 x33 x33 x33 y33 x33 x11 g11 g11 t11 t11 t11 t11 t11 ti want to get the total for each of the letters in the txt column.where - for each "number column" i will count only the 5 first values that i got from him - the rest are not intresting me.how should i fix your querythnakspelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 10:44:33
|
Define what is "first" to you.Do you have some column or other logic defining "order" in your table.Or any 5 records randomly for each txt will do for summation? E 12°55'05.25"N 56°04'39.16" |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 10:45:27
|
>> i will count only the 5 first values that i got from him How do you know the first 5 values - there's nothing in that table to order the entries.Are the values always the same? You want the total but only 5 if there are more than 5?select txt, tot = number * case when num > 5 then 5 else num endfrom(select txt, number = max(number), num = count(*)from tblgroup by txt) a==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 10:49:57
|
[code]DECLARE @Sample TABLE (Number INT, txt CHAR(1))INSERT @SampleSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'y' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't'SELECT DISTINCT s1.txt, (SELECT SUM(Number) FROM (SELECT TOP 5 s2.Number FROM @Sample AS s2 WHERE s2.txt = s1.txt ORDER BY s2.Number DESC) AS d)FROM @Sample AS s1[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 10:51:33
|
ok i wil lexplainthe result should be :33 count(x)=433 count(y)=111 count(g)=211 count(t)=3i hope this more clear?should i add an AUTOINCEMENT column to the table?thnaks in advancepelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 11:01:07
|
>> should i add an AUTOINCEMENT column to the table?Yes - otherwise there's no way of getting the first 5.select t.Number, t.txt, count(*)from tbl tjoin (select Number, seq, cnt = (select count(*) from tbl t2 where t2.Number = t1.Number and t2.seq <= t1.seq) from tbl t1) t3on t.seq = t3.seqwhere t3.cnt <= 5group by t.Number, t.txt==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 11:01:30
|
Peso - how can i make your query to count the data by the order of insert to the db?Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 15:18:04
|
quote: Originally posted by pelegk2 ok i wil lexplainthe result should be :33 count(x)=433 count(y)=111 count(g)=211 count(t)=3i hope this more clear?
No. In your sample data, you have 5 txt "x" and 6 txt "t". E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 15:20:06
|
quote: Originally posted by pelegk2 Peso - how can i make your query to count the data by the order of insert to the db?
SELECT DISTINCT s1.txt, (SELECT SUM(Number) FROM (SELECT TOP 5 s2.Number FROM @Sample AS s2 WHERE s2.txt = s1.txt ORDER BY s2.IdentityCol DESC) AS d)FROM @Sample AS s1 E 12°55'05.25"N 56°04'39.16" |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 15:28:39
|
quote: Originally posted by PesoNo. In your sample data, you have 5 txt "x" and 6 txt "t". E 12°55'05.25"N 56°04'39.16"
It's beacuse the data is ordered.If you take the first 5 entries for 33 you get 4 x's and 1 t.Have a look at my query which assumes an identity column seq.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 15:38:50
|
Oh, I see.So what OP wants is "first found" running streak for each txt? E 12°55'05.25"N 56°04'39.16" |
 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 15:59:08
|
Execly! i want whats first found! an by that i count!Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 16:09:52
|
[code]-- DataDECLARE @Sample TABLE (i int identity(1, 1), Number INT, txt CHAR(1))INSERT @SampleSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'y' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't'-- Calculationselect Number, txt, count(*) as cnt from @Sample awhere (select count(*) from @Sample where Number = a.Number and i < a.i) < 5group by Number, txt/* ResultsNumber txt cnt----------- ---- -----------11 g 211 t 333 x 433 y 1*/[/code]Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 16:16:45
|
I think count for x should be 3, and count for t should be 5. E 12°55'05.25"N 56°04'39.16" |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 16:20:16
|
Nope - the first 5 for 33 arex, x, x, y, x4 x's and 1 y.tryDECLARE @Sample TABLE (Number INT, txt CHAR(1), seq int identity)INSERT @SampleSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'y' UNION ALLSELECT 33, 'x' UNION ALLSELECT 33, 'x' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 'g' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't' UNION ALLSELECT 11, 't'select t.Number, t.txt, count(*)from @Sample tjoin (select Number, seq, cnt = (select count(*) from @Sample t2 where t2.Number = t1.Number and t2.seq <= t1.seq) from @Sample t1) t3on t.seq = t3.seqwhere t3.cnt <= 5group by t.Number, t.txtNumber txt ----------- ---- -----------11 g 211 t 333 x 433 y 1==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 16:37:51
|
I get it now. It is not a "running streak".The result should be the same asDECLARE @Sample TABLE (RowID INT, Number INT, txt CHAR(1))INSERT @SampleSELECT 1, 33, 'x' UNION ALLSELECT 2, 33, 'x' UNION ALLSELECT 3, 33, 'x' UNION ALLSELECT 4, 33, 'y' UNION ALLSELECT 5, 33, 'x' UNION ALLSELECT 6, 33, 'x' UNION ALLSELECT 8, 11, 'g' UNION ALLSELECT 10, 11, 'g' UNION ALLSELECT 12, 11, 't' UNION ALLSELECT 13, 11, 't' UNION ALLSELECT 20, 11, 't' UNION ALLSELECT 21, 11, 't' UNION ALLSELECT 22, 11, 't' UNION ALLSELECT 23, 11, 't'-- CalculationSELECT s.Number, y.txt, COUNT(*) AS OccurenciesFROM ( SELECT Number FROM @Sample GROUP BY Number ) AS sCROSS APPLY ( SELECT TOP 5 t.txt, t.RowID FROM @Sample AS t WHERE t.Number = s.Number ORDER BY t.RowID ) AS yGROUP BY s.Number, y.txt E 12°55'05.25"N 56°04'39.16" |
 |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2008-04-14 : 17:26:24
|
thnaks rn and PESO - the solution i think is not so obvious of itself.can you exlpain more about what you have done?thnakspelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-14 : 17:31:56
|
I get no credit? Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
Next Page
|