2

I have a table like this

date        |  text | link_id
------------+-------+----------
 05-10-2014 | lorem |    2
 06-10-2014 | lorem |    4
 05-10-2014 | lorem |    4
 05-10-2014 | lorem |    5
 05-10-2014 | lorem |    5
 05-10-2014 | lorem |    5
 05-10-2014 | lorem |    5
 05-10-2014 | lorem |    5
 05-10-2014 | lorem |    6
 05-10-2014 | lorem |    4

I need to get the count of group from my rows here the result

    date    |  text | link_id  | value
------------+-------+----------+-------
 05-10-2014 | lorem |    2     |   1
 06-10-2014 | lorem |    4     |   3
 05-10-2014 | lorem |    5     |   6

and why if I use function

COUNT(DISTINCT n)

or only

COUNT()

it always forces me to add all SELECT statement to GROUP

and I'm only Distinct 1 row

1 Answer 1

1

Try:

Select link_id, Count (link_id)
From table
Group by link_id;

where you should replace table with the name of your table.

Aggregate functions get applied to all rows in a group. So you first group by link_id and get groups of rows with the same link_id. Count gets applied then to each group to compute the number of rows in each group. You would get then link_id && number of rows with same link_id as result.

Please note that it is actually unclear from your question how do you intend actually to select the data for the two other colums. In the example you given fot link_id 4 you expect to get count 2, however the dates for the two records with link_id 4 differ (one is 5.10.2014 the other is 6.10.2014). Which one should be taken for the result?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.