Unix Technical Forum

group by 2 fileds

This is a discussion on group by 2 fileds within the MySQL forums, part of the Database Server Software category; --> I need a halp with a query. I have a table like this: ID FLD1 FLD2 1 A 125 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:19 AM
Massimo
 
Posts: n/a
Default group by 2 fileds

I need a halp with a query.
I have a table like this:

ID FLD1 FLD2
1 A 125
2 B 165
3 C 165
4 A 125
5 A 100
6 C 165

I need to select all the record grouped by FLD1 + FLD2 and the count of
every combination.
In this case:
A 125 2
B 165 1
C 165 2
A 100 1

I tried with
SELECT COUNT(FLD1) as c,FLD1,FLD2 from TABLE1 GROUP BY FLD1,FLD2

but it doesn't work.

Thank you in advance

bye,
max



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:19 AM
Luuk
 
Posts: n/a
Default Re: group by 2 fileds


"Massimo" <TOGLIMImassimo@pafin.it> schreef in bericht
news:Puoqi.6548$pd1.5564@tornado.fastwebnet.it...
>I need a halp with a query.
> I have a table like this:
>
> ID FLD1 FLD2
> 1 A 125
> 2 B 165
> 3 C 165
> 4 A 125
> 5 A 100
> 6 C 165
>
> I need to select all the record grouped by FLD1 + FLD2 and the count of
> every combination.
> In this case:
> A 125 2
> B 165 1
> C 165 2
> A 100 1
>
> I tried with
> SELECT COUNT(FLD1) as c,FLD1,FLD2 from TABLE1 GROUP BY FLD1,FLD2
>
> but it doesn't work.
>
> Thank you in advance
>
> bye,
> max
>
>
>


to me this seems to work,
why do you think this does not work?



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:19 AM
Luuk
 
Posts: n/a
Default Re: group by 2 fileds


"Luuk" <luuk@invalid.lan> schreef in bericht
news:46aa3dd0$0$240$e4fe514c@news.xs4all.nl...
>
> "Massimo" <TOGLIMImassimo@pafin.it> schreef in bericht
> news:Puoqi.6548$pd1.5564@tornado.fastwebnet.it...
>>I need a halp with a query.
>> I have a table like this:
>>
>> ID FLD1 FLD2
>> 1 A 125
>> 2 B 165
>> 3 C 165
>> 4 A 125
>> 5 A 100
>> 6 C 165
>>
>> I need to select all the record grouped by FLD1 + FLD2 and the count of
>> every combination.
>> In this case:
>> A 125 2
>> B 165 1
>> C 165 2
>> A 100 1
>>
>> I tried with
>> SELECT COUNT(FLD1) as c,FLD1,FLD2 from TABLE1 GROUP BY FLD1,FLD2
>>
>> but it doesn't work.
>>
>> Thank you in advance
>>
>> bye,
>> max
>>
>>
>>

>
> to me this seems to work,
> why do you think this does not work?
>
>
>


sorry, forgot this to add to my post:

mysql> select * from t27;
+------+------+
| FLD1 | FLD2 |
+------+------+
| A | 125 |
| B | 165 |
| C | 165 |
| A | 125 |
| A | 100 |
| C | 165 |
+------+------+
6 rows in set (0.00 sec)

mysql> select count(FLD1) as c, FLD1, FLD2 from t27 group by FLD1, FLD2;
+---+------+------+
| c | FLD1 | FLD2 |
+---+------+------+
| 1 | A | 100 |
| 2 | A | 125 |
| 1 | B | 165 |
| 2 | C | 165 |
+---+------+------+
4 rows in set (0.00 sec)

mysql>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:19 AM
Paul Lautman
 
Posts: n/a
Default Re: group by 2 fileds

Massimo wrote:
> I need a halp with a query.
> I have a table like this:
>
> ID FLD1 FLD2
> 1 A 125
> 2 B 165
> 3 C 165
> 4 A 125
> 5 A 100
> 6 C 165
>
> I need to select all the record grouped by FLD1 + FLD2 and the count
> of every combination.
> In this case:
> A 125 2
> B 165 1
> C 165 2
> A 100 1
>
> I tried with
> SELECT COUNT(FLD1) as c,FLD1,FLD2 from TABLE1 GROUP BY FLD1,FLD2
>
> but it doesn't work.
>
> Thank you in advance
>
> bye,
> max


"it doesn't work"
Well that sure tells us everything we need to know to help you.
Based on what you've said it could be:

There is no table called TABLE1
Mysql is not running
You wrote the query in Notepad and forgot to submit it to mysql
There are no records in the table
The field names are wrong

or anything else.

So how about thinking before you write and tell us in what way it doesn't
work!


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:20 AM
Massimo
 
Posts: n/a
Default Re: group by 2 fileds


> mysql> select count(FLD1) as c, FLD1, FLD2 from t27 group by FLD1, FLD2;


sorry, now it works, I'don't know why before not worked

bye,
max


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:20 AM
Massimo
 
Posts: n/a
Default Re: group by 2 fileds


> > mysql> select count(FLD1) as c, FLD1, FLD2 from t27 group by FLD1, FLD2;

>
> sorry, now it works, I'don't know why before not worked
>
> bye,
> max
>
>

i need also to select only rows whre c>1:
select count(FLD1) as c, FLD1, FLD2 from t27 where c>1 group by FLD1, FLD2;

but return me an error:
Unknown column 'c' in 'where clause'

bye,
max


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:20 AM
Captain Paralytic
 
Posts: n/a
Default Re: group by 2 fileds

On 31 Jul, 08:49, "Massimo" <TOGLIMImass...@pafin.it> wrote:
> > > mysql> select count(FLD1) as c, FLD1, FLD2 from t27 group by FLD1, FLD2;

>
> > sorry, now it works, I'don't know why before not worked

>
> > bye,
> > max

>
> i need also to select only rows whre c>1:
> select count(FLD1) as c, FLD1, FLD2 from t27 where c>1 group by FLD1, FLD2;
>
> but return me an error:
> Unknown column 'c' in 'where clause'
>
> bye,
> max


That is because 'c' is an alias.

Try:
select
count(FLD1) as c,
FLD1,
FLD2
from t27
group by FLD1, FLD2
having c>1

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 10:20 AM
Massimo
 
Posts: n/a
Default Re: group by 2 fileds


> Try:
> select
> count(FLD1) as c,
> FLD1,
> FLD2
> from t27
> group by FLD1, FLD2
> having c>1
>


ok, works!

thanx

bye,
max


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com