2i've a table of repeated items, i got this table done from a left join.-
Lets say that this table alias is "piked"
ID Product PickedBY
1 Basket Josh
1 Basket Jessica
1 Basket Josh
1 Basket Mike
1 Basket Mike
2 Seat Alan
3 Computer Jessica
4 Mouse Josh
4 Mouse Mike
4 Mouse Jessica
I wish to limit the equal Ids to 2. how do i do it? so, if i list just ids result will look 1,1,2,3,4,4.
Thanks
Your question doesn't make sense to me which makes me wonder what you want to achieve with such a query but you can try to group the results:
select id, count(*)
from piked
group by id
That's not exactly what you asked but you can tell whether an id has duplicates by looking at the second column of the result. The result will be
1,5
2,1
3,1
4,3
This should do the trick:
SELECT ID, Product, PickedBy FROM (
SELECT
IF(#prev != q.ID, #rownum:=1, #rownum:=#rownum+1) as rownumber, #prev:=q.ID, q.*
FROM (
SELECT
ID, Product, PickedBy
FROM piked
, (SELECT #rownum:=0, #prev:=0) r
ORDER BY ID
)q
)asdf
WHERE asdf.rownumber <= 2
Related
One of the test questions came by with following schemas, to look for the best doctor in terms of:
Best scored;
The most times/attempts;
For each medical procedures (in terms of name)
[doctor] table
id
first_name
last_name
age
1
Phillip
Singleton
50
2
Heidi
Elliott
34
3
Beulah
Townsend
35
4
Gary
Pena
36
5
Doug
Lowe
45
[medical_procedure] table
id
doctor_id
name
score
1
3
colonoscopy
44
2
1
colonoscopy
37
3
4
ulcer surgery
98
4
2
angiography
79
5
3
angiography
84
6
3
embolization
87
and list goes on...
Given solution as follow:
WITH cte AS(
SELECT
name,
first_name,
last_name,
COUNT(*) AS procedure_count,
RANK() OVER(
PARTITION BY name
ORDER BY COUNT(*) DESC) AS place
FROM
medical_procedure p JOIN doctor d
ON p.doctor_id = d.id
WHERE
score >= (
SELECT AVG(score)
FROM medical_procedure pp
WHERE pp.name = p.name)
GROUP BY
name,
first_name,
last_name
)
SELECT
name,
first_name,
last_name
FROM cte
WHERE place = 1;
It'll mean a lot to be clarified on/explain on how the WHERE clause worked out under the subquery:
How it worked out in general
Why must we match the two pp.name and p.name for it to reflect the correct rows...
...
WHERE
score >= (
SELECT AVG(score)
FROM medical_procedure pp
WHERE pp.name = p.name)
...
Thanks a heap!
Above is join with doctor and medical procedure and group by procedure name and you need doctor names with most attempt and best scored.
Subquery will join by procedure avg score and those who have better score than avg will be filtered.
Now there can be multiple doctor better than avg so taken rank by procedure count so most attempted will come first and then you taken first to pick top one
I have a table with grades:
name grade
peter 10
paul 8
mary 7
peter 6
mary 10
paul 5
paul 7
paul 6
mary 2
I would like to count how many grades does each of one have above their own mean.
For example, Peter has two grades, so Peter's mean is 8. Mary has 3 grades, but only 2 are above her own mean. Paul, has 4 grades, but only 2 are above his own mean. I would like to get as result something like:
name count
peter 1
mary 2
paul 2
I tried to do this using subqueries or adding a condition inside a count() column, but I usually get an error that tells me that my subquery returns more than 1 result. How can I achieve this?
Here is one of my attempts:
with staging as (
select name, count(*) as total,MAX(grade) as max_g, MIN(grade) as min_g, AVG(grade) as avg, STD(grade) as std
from students
where course = 'stats'
group by name)
select name,count(IF(grade > (select avg from staging),1,0)) as grades_over_mean from students
GROUP BY name
You can use window functions:
select s.*,
sum(grade > avg_grade) as num_above_average
from (select s.*,
avg(grade) over (partition by name) as avg_grade
from students s
) s;
I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;
I'm trying to count the number of purchases and 'up'/'down' votes for all items that match a given search term. Unfortunately, as I've set up my query now, the purchases and vote counts are multiplied by a mysterious factor of 22 that I can not figure out where it comes from.
As an example for one of the items: the purchases, up, and down votes should be 7, 2, and 1 respectively but they are instead 154, 44, and 22.
here's my SQL code:
SELECT *
sum(purchaseyesno) as tots,
sum(rating=1) as yes,
sum(rating=0) as no
from items
join items_purchased
on items_purchased.item_id=items.item_id
join accounts
on items.account_id=accounts.account_id
like subject='%album by joe%' or description='%album by joe%'
group by item_id
order by tots desc, yes desc
Here's some sample data:
subject tots yes no full_name
album by joe 154 44 22 joe smith
album by fred 88 44 0 fred jones
Here's how i'd like the data to look:
subject tots yes no full_name
album by joe 7 2 1 joe smith
album by fred 4 2 0 fred jones
Would someone be able to help me figure out what is going on here? I can't figure out this factor of 22 issue which persists despite changing the group by and other things (meaning, this 22 number is independent of the # of returned rows).
You don't show your schema but it might help to use a subquery:
SELECT subject, tots, yes, no, fullnane
FROM (
SELECT item_id, SUM(purchaseyesno) AS tots, SUM(rating=1) AS yes, SUM(rating=0) AS no
FROM items_purchased
GROUP BY item_id
) i
JOIN items ON i.item_id = items.item_id
JOIN accounts ON items.account_id=accounts.account_id
WHERE subject LIKE '%album by joe%' OR description LIKE '%album by joe%'
ORDER BY tots DESC, yes DESC
Firstly, don't do select * if you're grouping by. Remember you must group by all non-aggregated fields.
Secondly, the high amount of results must be comming from those joins. Remove the group by and the aggregated columns and inspect the results and you'll see why you're getting so many records.
Finally... you're missing a where clause there...
SELECT subject,
sum(purchaseyesno)/22 as tots,
sum(CASE WHEN rating=1 THEN 1 END)/22 as yes,
sum(CASE WHEN rating=0 THEN 1 END)/22 as no,
full_name
from items
join items_purchased
on items_purchased.item_id=items.item_id
join accounts
on items.account_id=accounts.account_id
like subject='%album by joe%' or description='%album by joe%'
group by subject,full_name
order by tots desc, yes desc
In this query,the SUM function basically works on the columns purchaseyesno,rating=1,rating=0.
For eg.
Subject purchaseyesno rating full_name
1 5 1 Mark
1 6 0 Mark
2 7 1 Robert
2 8 0 Robert
The above query will return as below.
Subject tots yes no full_name
1 11 1 1 Mark
2 15 1 1 Robert
Name Score
Jim 1
Jim 2
Jim 4
Lisa 2
Lisa 5
Ted 1
Ted 2
Ted 3
How can i group by name, order by highest score, and only pick that one row? So The query would return 3 rows Jim 4, Lisa 5, and Ted 3.
To find the max score, you can GROUP BY name, and use the MAX function:
SELECT ns.Name, MAX(ns.Score) AS Score
FROM NameScore AS ns
GROUP BY ns.Name
ORDER BY ns.Name ASC
I made up the table name, since you did not provide one, switch that for your real table.
I think the following will work, but I haven't tested it:
SELECT Name, MAX(Score) FROM Table
GROUP BY Name
i think this is trust:
select from Table group by Name having MAX(Score);