Specific Column is not showing up, when it should? - sql

I don't know what's going on with my code here. I am trying to return the highest number of calls received by particular phone numbers, and find the top 7 numbers by calls received, but I am only getting the count column in my results. The code is:
SELECT COUNT (call_id) FROM call_test
GROUP BY receiver_id
ORDER BY COUNT(call_id) DESC
LIMIT 7;
But all it is returning is:
COUNT(call_id)
3
2
2
2
2
1
1
I think my code is right, but how do you show the particular numbers that correspond to the respective counts? This is SQLPro for MAC.

Is it as simple as including the number in the select?
SELECT receiver_id, COUNT(call_id)
FROM call_test
GROUP BY receiver_id
ORDER BY COUNT(call_id) DESC
LIMIT 7;

Related

SQL rank function to show lowest number members in a policy not working

I have two tables. One with a policy name and one with member details. I need to identify the policy name with least amount of members and want to use a rank function but it isnt working for me. Getting strange result. Sample of the results can be seen below. The results coming back for the count are not correct. It should be in the thousands and not in the low numbers it is giving.
Strangely when I put rank in desc order it returns the correct count amount. What am I doing wrong here. I want to get it so that I can pick lowest by selecting rank = 1 and show the correct count amount
select count (distinct member_id) as count_members,
policy_name,
rank () over (order by count(distinct member)) as rank
from member_table t1, policy_table t2
where t1.policy_id = t2.policy_id
and month_id = 202210
group by policy_name
count_members
policy_name
rank
3
Policy a
1
5
Policy b
2

SQLite: How to fetch the ORDER index of COUNT

I have an SQLite database that contains a list of user messages in a group.
And I want to get a user's "rank" by counting the number of messages they had sent.
Currently I'm doing this
SELECT user_id, COUNT(*) as count
FROM message
group by user_id
ORDER BY count DESC
It'd return something like this:
-
user_id
count
1
2072040132
61877
2
1609505732
40514
3
1543287045
34735
4
203349203
30570
5
842634673
29651
6
1702633101
29185
7
1978947042
27728
8
1929648593
27025
9
1069841429
17944
10
1437208364
17344
11
...
...
Like user 1609505732 is top 2, and 1702633101 is top 6.
But my database has more than 2 million rows, and this is too slow having to fetch all of the list.
I was wondering if there are any way that I can fetch only the order of it.
Like this:
-
user_id
order
count
1
1702633101
6
61877
And the user with id=1702633101 is top 6. That'd be a lot faster.
Thanks for spending time on my question, I can't seem to find the answer anywhere on the internet.
To improve query speed, I'd consider physicalising the aggregate view, example below:
CREATE Table as tbl_aggregate()
Id INTEGER PRIMARY KEY AUTOINCREMENT
, user_id NVARCHAR
, count INT;
INSERT INTO tbl_aggregate
SELECT user_id, COUNT(*) as count
FROM message
group by user_id
ORDER BY count DESC;
Select * from tbl_aggregate
Where Id = 6
Select top 10 * from tbl_aggregate

How to get top 10 from one column and sort by another column in hive?

I want to find top 10 title with high number of user ids. So I used query like
select title,count(userid) as users from combined_moviedata group by title order by users desc limit 10
But i need to sort them based on title, I tried this query
select title,count(userid) as users from combined_moviedata group by title order by users desc,title asc limit 10
But it doesnot sort them. Merely returned same results. How to do this
The answer from #KaushikNayak is very close to what I'd consider the "right" answer.
At one level, work out what your top 10 records are
At a different level, sort them by a different field
The only thing I'd say is that if the 10th and 11th most common titles are tied for the same count, they should generally also be included in the results. This is a RANK().
WITH
ranked_titles AS
(
SELECT
RANK() OVER (ORDER BY COUNT(*) DESC) frequency_rank,
title
FROM
combined_moviedata
GROUP BY
title
)
SELECT
*
FROM
ranked_titles
WHERE
frequency_rank <= 10
ORDER BY
title
;
http://sqlfiddle.com/#!6/7283c/1
Note that in the example linked, 12 rows are returned. That is because 4 titles are all tied for the 9th most frequent, and it is actually impossible to determine which two should be selected in preference over the others. In this case selecting 10 rows would normally be statistically incorrect.
title frequency frequency_rank
title06 2 9
title07 2 9
title08 2 9
title09 2 9
title10 3 6
title11 3 6
title12 3 6
title13 4 4
title14 4 4
title15 5 2
title16 5 2
title17 6 1
You could make use of a WITH clause
with t AS
(
select title,count(userid) as users from combined_moviedata
group by title
order by users desc limit 10
)
select * FROM t ORDER BY title ;

Postgres limit is showing wrong number of rows

I've created a query in Postgres which is limiting the number of results but for some reason, my limit clause is showing one less result that I ask for. Here is the query:
select articles.title, articles.slug, count.views
from articles,
(select path, count(path) as views
from log
where status = '200 OK'
and path != '/'
group by path
order by views desc limit 3
) count
WHERE articles.slug LIKE LTRIM(count.path, '/article/');
but when I run it, I only get the first 2 rows. If I change the 3 to 4, I get the first 3 rows and so on. Any suggestions?
When I remove the limit, all 6 relevant rows are displayed and if I use limit 3 offset 1 in returns the top 3 rows as requested so I believe the Gordon's answer is correct. – Gerry de Caires

PostgreSQL:How get last rows from a select query

See the below example,
create table data(name varchar, value int);
insert into data values('joe',1);
insert into data values('bob',2);
insert into data values('jane',3);
insert into data values('anne',4);
insert into data values('kate',5);
And if I Execute
select * from data limit 2;
Will Get
name | value
------+-------
joe | 1
bob | 2
(2 rows)
So,How Can I Get the Last 2 Rows in select * from data?
What I'm expecting is....
name | value
------+-------
anne | 4
kate | 5
(2 rows)
You have two options according to your need i.e,
select * from data order by value desc limit 2
Or
LIMIT and OFFSET
if you want the 4th and 5th row just offset the first 3 so that the 4th row becomes the start of our set and you can specify a limit to say that you only want 2 rows from that.
select * from data offset 3 limit 2;
/* The order of LIMIT and OFFSET does not matter. This gives the same result */
select * from data limit 2 offset 3;
I know I'm answering a six year old question and I know the accepted answer says to use an offset, however that's only going to be useful if you know the length of the table. Looking at the wording of the question and the example given, I assumed that like myself, the author wanted the last entries in a table based on an id or other value in the order that they were entered.
The best solution I've found so far that orders the values as expected is using a subquery as follows:
SELECT * FROM ( SELECT * FROM data ORDER BY VALUE DESC LIMIT 2) AS _ ORDER BY VALUE ASC;
You'd substitute VALUE for whichever column you'd want to sort by to get the "last" entries. In my experience, this method is an order of magnitude quicker than using a count to find an offset.
To get the x last rows, example with x=10,
use offset alone, with count:
SELECT *
FROM data
ORDER BY value ASC
OFFSET (SELECT count(*) FROM DATA)-10
I let you check if the offset is not negative...
Make use of Order by Clause
select * from data order by value desc limit 2;
OR
select top 2 * from data order by value desc ;
You can achieve it using Order by clause desc
SELECT *
FROM data
ORDER BY value DESC limit 2;
like does Krishraj Rana you can try this function for get the last row with "limit 1" and order by "x col" desc, complete it "by x_col desc limit 1".