tags:

views:

87

answers:

1

Hi

I have this table:

Reason|Area_Code|Id
x         dig     1
x         dig     2
y         dig     3
h         util    4
x         dig     5

I'm trying for a SQL that returns:

Reason|Amount of distinct Reason|Area_code
x              3                  dig
y              1                  dig
h              1                  util

I will use this result to plot a chart. I don´t have any idea on how this SQL can be done. Could you help me?

+3  A: 

Try This:

  Select Reason, Count(*) AmountOfReason, Area_Code
  From Table
  Group By Reason, Area_Code

... but this assumes that Area_Code is always determined by Reason, i.e., they are always paired, that you do not have 2 rows in yr table like

x   util   5
x    dig   6

cause if that was the case this won't work.

Charles Bretana
Hi Charles, your commented is right. I mean, in this table never will have 2 rows as your example. But I've tried your sugestion and it dind't work. I got his error: "not a single-group group funciton"
Luciana Borela
Then your sql is not the same as what is above. Are you trying to just use this idea in another sql query? Anytime you use an aggregate function, (like Count(*)), you must have a Group By clause with specific things in it, and there are rules you must follow governing what other expressions are allowed in the select clause.
Charles Bretana
Charles your are right again, I haven't use Group By. It really works. Tank you very very very much!
Luciana Borela