I have create this query:
SELECT table1.id, b.sum
FROM table1
CROSS JOIN (SELECT SUM(id) sum
FROM table1) b
ORDER BY id DESC;
But this produces results like this:
id
sum
3
6
2
6
1
6
Sum value print only one time. Can you help me.
But I want this result :
id
sum
3
6
2
1
This should do it:
select
id,
CASE WHEN id=(max(id) over())
THEN sum(id) over (order by id) END as 'sum'
from cte1
order by id desc;
more info see: Window Function Concepts and Syntax
I have been searching at lot and have found topics like similar topics but none were exactly the solutions that I was looking for. In this case, I have a working code, but something that seems extremely hacky to me and should have a simpler and better way of getting it done.
The "Test" table
id
--
1
1
1
2
2
2
3
4
4
Nothing complex, just a simple table with some repeating id values
What I want is to group these id together and show all the id that are repeating the most i.e
id | count
----------
1 3
2 3
The solution that I have currently come up with
select
#max := max(count) as count
from (
select
id,
count(id) as count
from
test
group by
id
)
as
inner_table;
select
id, count
from (
select
id,
count(id) as count
from
test
group by
id
)
as
inner_table
where count = #max;
One way to do it with group by and having.
select id,count(*) as cnt
from t
group by id
having count(*)=(select count(*) as cnt
from t
group by id
order by cnt desc
limit 1)
I have table that looks like this:
id rank
a 2
a 1
b 4
b 3
c 7
d 1
d 1
e 9
I need to get all the distinct rank values on one column and count of all the unique id's that have reached equal or higher rank than in the first column.
So the result I need would be something like this:
rank count
1 5
2 4
3 3
4 3
7 2
9 1
I've been able to make a table with all the unique id's with their max rank:
SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id
I'm also able to get all the distinct rank values and count how many id's have reached exactly that rank:
SELECT
DISTINCT TopRank AS 'rank',
COUNT(id) AS 'count of id'
FROM
(SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id) tableDerp
GROUP BY TopRank
ORDER BY TopRank ASC
But I don't know how to get count of id's where the rank is equal OR HIGHER than the rank in column 1. Trying SUM(CASE WHEN TopRank > TopRank THEN 1 END) naturally gives me nothing. So how can I get the count of id's where the TopRank is higher or equal to each distinct rank value? Or am I looking in the wrong way and should try something like running totals instead? I tried to look for similar questions but I think I'm completely on a wrong trail here since I couldn't find any and this seems a pretty simple problem that I'm just overthinking somehow. Any help much appreciated.
One approach is to use a correlated subquery. Just get the list of ranks and then use a correlated subquery to get the count you are looking for:
SELECT r.rank,
(SELECT COUNT(DISTINCT t2.id)
FROM myTable t2
WHERE t2.rank >= r.rank
) as cnt
FROM (SELECT DISTINCT rank FROM myTable) r;
I am looking to find the count of rows where the entered answer is the same as the correct answer. Here's an example:
WorkerID Answer Correct
1 A A
1 B C
2 A D
I would then get the following result:
WorkerID AnswerCount # Correct
1 2 1
2 1 0
So far I have (conceptually):
SELECT worker_id, count(*), count(Answer == Correct) FROM answer_table GROUP BY WorkerID
What would be the correct query here?
You don't want count(), you want sum():
SELECT worker_id, count(*) as AnswerCount, sum(Answer = Correct) as NumCorrect
FROM answer_table
GROUP BY WorkerID;
count() counts the number of non-NULL values that the expression takes on. You want to count the number of matches, which is the number of trues.
I think this is what you want :
select count(*)
from yourTable
where answer = correct
group by workerId
Basically, what you need to do is
Select all where answer = correct.
group them by workerId.
count the num of rows (where answer = correct) for each group.
Edit : To answer to your edited question,
select count(*), count(b.workerId)
from yourTable
left join (select *
from yourTable
where answer = correct) b using(workerId)
group by workerId
use this:
select workerid,count(*) as numberOfAnswers,
sum(case
when answer=correct then 1
else 0 end) as correctAnswers
from tbl
group by workerid
DEMO
Lets say I have the table below.
I want to get all the friends, but I want the id 5 to be the first item in the list. I don't care about the order that I receive the rest of the items.
The desired query result will be:
friends
-------
id name
5 nahum
1 moshe
2 haim
3 yusuf
4 gedalia
6 dana
How can I do this?
using Mysql 5.1.x.
Thanks!
select id,name
from friends
order by id=5 desc
(given you don't care about order of the rest, otherwise, e.g. rest by id asc)
select id,name
from friends
order by id=5 desc, id asc
Try this:
select id,name
from friends
order by case when id=5 then -1 else id end
if you have more then one you can do:
select id,name
from friends
order by case when id in (5,15,25) then -1 else id end,id
I can't access a MySQL now to test, so it might be reversed... but you can use the fact that Booleans also sort, and that you can have several sort fields.
SELECT ... ORDER BY id != 5, id
(you might have to write id = 5, I can't remember if TRUEs sort before or after FALSEs.)
EDIT: Oh, I just read that you don't care about the order of the rest, in which case I heartily recommend #Richard's answer.
You can use field() in MySQL.
select id,name from friends order by field(id,5,id)
The 1st parameter in field() means the field you want to sort with, the rest is ordering.
So 5 will be sort first, and the rest from id (without 5). You can do like field(id,5,1,3,id) if you want 5,1,3 to be in front.
5 can be choose to sort at last by field(id,id,5). The 2nd id will exclude 5 from it also.
If you want to do the same for UNION query, for example if you have:
select id,name
from friends
UNION
select id,name
from friends
order by id=5 desc
... you would get an exception in PostgreSQL:
Only result column names can be used, not expressions or functions. HINT: Add the expression/function to every SELECT, or move the UNION into a from clause
To get around this, you would use the following:
select id,name, (id=5) AS is_five
from friends
UNION
select id,name, (id=5) AS is_five
from friends
order by is_five DESC, id DESC
The expression (id=5) would return 't' OR 'f', depending on whether your column value is equal or not to the expected value (5), so the order by would first order the 't' columns, then the rest.
You should use MySQL's ORDER BY FIELD clause to solve this. Although, the answer has been accepted on this, here's a better solution.
select 1 id, 'Zeta' order_col union all
select 2 id, 'Alpha' order_col union all
select 3 id, 'Gamma' order_col union all
select 4 id, 'Phi' order_col union all
select 5 id, 'Delta' order_col union all
select 6 id, 'Delta' order_col union all
select 7 id, 'Alpha' order_col union all
select 8 id, 'Gamma' order_col union all
select 9 id, 'Zeta' order_col union all
select 10 id, 'Phi' order_col
order by field (order_col, 'Alpha', 'Gamma', 'Phi', 'Delta', 'Zeta'), id;
This is better than
id=something, order by id asc
order by case when something then 1 when something_else then 2 end desc
This is a little ugly because it has code duplication, but it does the trick:
select .... where id = 5
union
select .... where not id = 5