A
A
Alexey Lebedev2014-06-12 20:33:30
SQL
Alexey Lebedev, 2014-06-12 20:33:30

How to count the number of values ​​in top SQL?

There is MS SQL the table.
id, name, rate
We need to take the last 100 entries by id and find out how many people with a rating of 1,2,3,4 have a total of 25 values. And sort everything in descending order.

BEGIN TRANSACTION;

CREATE TABLE test(Id integer PRIMARY KEY, Name text, rate integer);

INSERT INTO test VALUES(1,'Tom',2);
INSERT INTO test VALUES(2,'Lucy',4);
INSERT INTO test VALUES(3,'Frank',5);
INSERT INTO test VALUES(4,'Jane',5);
INSERT INTO test VALUES(5,'Julia', 2);
INSERT INTO test VALUES(6,'Helena', 3);
INSERT INTO test VALUES(7,'Sarah', 2);
COMMIT; 

SELECT * FROM test;

Answer example:
rate, count
2, 3
5, 2
3, 1
4, 1
..
Interested in a simple, but productive option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fadmin, 2014-06-12
@swanrnd

Something like this:

SELECT [Id]
      ,[Name]
      ,[rate] from (SELECT top 100 [Id]
      ,[Name]
      ,[rate]
  FROM [dbo].[test] order by [Id] desc) x order by rate desc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question