Would really appreciate it if someone could help me with my query.
Below is a sample table in which I want to retrieve those ActionId's which are not repeated twice or more.
I've tried not in, count, and distinct but no luck so far.
Required: Product=AA, OrderType=Market, ActionId=Non Repeated ones
select distinct ActionId,LocationId,PartyName from order where ActionIid in (select distinct ActionId from order where Product = 'AA'and OrderStatus = 'Complete') order by ActionId desc
select ActioId from order where Product ='AA'
and OrderType='Market'
and ActioId not in (
select ntd_id from order where Product ='AA'
and OrderType!='Limit'
and OrderType!='Stop'
group by ActioId having count(*)<2
)
group by ActionId
Where am I making the mistake?
Thanks in advance!
You want all the actionid appears once in the table for which product='AA' and ordertype='Market'. I have edited my answer. Please check:
select ActionId from order
group by actionid
having count(*)=1 and max(Product) ='AA' and max(OrderType)='Market'
I have no time to test this but this is how I would go fro this. It should only give you the ActioId that is only once in the table.
SELECT
`ActioId`
FROM `order `
WHERE
Product = 'AA'
AND
OrderType = 'Market'
GROUP BY
`ActioId`
HAVING
count(ActioId) < 2
ORDER BY
`ActioId` ASC
Related
I want show progress data where year = 2019 and month = 3 but I want to choose highest id to show so my code is like this
SELECT *
FROM projectmonthlyview2
where YEAR(tgl_masuk) =2020
And MONTH(tgl_masuk) =3
and id IN (select MAX(id)
FROM projectmonthlyview2
GROUP
BY project_id)
Order
by project_id asc
But not showing any value
Your question isn't exactly clear but maybe this?
SELECT * FROM `project_monthly`
where YEAR(tgl_masuk) =2020 And MONTH(tgl_masuk) =3
order by id desc
limit 1
I have solved it with the following query:
SELECT * FROM `projectmonthlyview2`
where id IN (select MAX(id) FROM projectmonthlyview2 Where YEAR(tgl_masuk) =2020 And MONTH(tgl_masuk) =3 GROUP BY project_id)
Order by project_id asc
My table has following data:
As you can see above table has user_id and created_at columns. I want to query data as shown below.
I used below query but it only data for 1 user at any date.
e user_id is not null
GROUP BY DATE(created_at);
[![enter image description here][3]][3]
As you can see, it shows data only for one user a time.
Please help.
Include user_id in group by clause to get data for each date and each user
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',
user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id;
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at), user_id;
Try to group with concat
SELECT `date`,`user_id`,COUNT(*) FROM ibiza_production.line_write_revenues GROUP BY CONCAT(`date`,'-',`user_id`) ORDER BY `date`,`user_id` LIMIT 50
Hi Can you try this and let me know if it helped you.
SELECT count(*) AS 'Requests',
DATE(created_at) AS 'Date',user_id
FROM ibiza_production.line_write_revenues where user_id is not null
GROUP BY DATE(created_at),user_id order by DATE(created_at);
I am having trouble writing a query for the following problem. I have tried some existing queries but cannot get the results I need.
I have a results table like this:
userid score timestamp
1 50 5000
1 100 5000
1 400 5000
1 500 5000
2 100 5000
3 1000 4000
The expected output of the query is like this:
userid score
3 1000
1 1000
2 100
I want to select a top list where I have n best scores summed for each user and if there is a draw the user with the lowest timestamp is highest. I really tried to look at all old posts but could not find one that helped me.
Here is what I have tried:
SELECT sum(score) FROM (
SELECT score
FROM results
WHERE userid=1 ORDER BY score DESC LIMIT 3
) as subquery
This gives me the results for one user, but I would like to have one query that fetches all in order.
This is a pretty typical greatest-n-per-group problem. When I see those, I usually use a correlated subquery like this:
SELECT *
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3;
This is not the whole solution, as it only gives you the top three scores for each user in its own row. To get the total, you can use SUM() wrapped around that subquery like this:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3) tmp
GROUP BY userId;
Here is an SQL Fiddle example.
EDIT
Regarding the ordering (which I forgot the first time through), you can just order by totalScore in descending order, and then by MIN(timestamp) in ascending order so that users with the lowest timestamp appears first in the list. Here is the updated query:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score, timeCol
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score) <= 3) tmp
GROUP BY userId
ORDER BY totalScore DESC, MIN(timeCol) ASC;
and here is an updated Fiddle link.
EDIT 2
As JPW pointed out in the comments, this query will not work if the user has the same score for multiple questions. To settle this, you can add an additional condition inside the subquery to order the users three rows by timestamp as well, like this:
SELECT userId, SUM(score) AS totalScore
FROM(
SELECT userId, score, timeCol
FROM myTable m
WHERE(
SELECT COUNT(*)
FROM myTable mT
WHERE mT.userId = m.userId AND mT.score >= m.score
AND mT.timeCol <= m.timeCol) <= 3) tmp
GROUP BY userId
ORDER BY totalScore DESC, MIN(timeCol) ASC;
I am still working on a solution to find out how to handle the scenario where the userid, score, and timestamp are all the same. In that case, you will have to find another tiebreaker. Perhaps you have a primary key column, and you can choose to take a higher/lower primary key?
Query for selecting top three scores from table.
SELECT score FROM result
GROUP BY id
ORDER BY score DESC
LIMIT 3;
Can you please try this?
SELECT score FROM result GROUP BY id ORDER BY score DESC, timestamp ASC LIMIT 3;
if 2 users have same score then it will set order depends on time.
You can use a subquery
SELECT r.userid,
( SELECT sum(r2.score)
FROM results r2
WHERE r2.userid = r.userid
ORDER BY score DESC
LIMIT 3
) as sub
FROM result r
GROUP BY r.userid
ORDER BY sub desc
You should do it like this
SELECT SUM(score) as total, min(timestamp) as first, userid FROM scores
GROUP BY userid
ORDER BY total DESC, first ASC
This is way more efficient than sub queries. If you want to extract more fields than userid, then you need to add them to the group by.
This will of cause not limit the number of scores pr user, which indeed seems to require a subquery to solve.
Is there any other way to write this query ?
I tried doing it in a subquery but it doesn't work because of the multiple columns. It seems like this query only works by itself. Please correct me
Records
PK recordId
dateViewed
CarViewed
I tried this
SELECT R.`dateViewed` FROM Records ,(
SELECT R.CarViewed, COUNT(R.CarViewed) as cnt FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Then I tried this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt FROM Records ,
(
SELECT R.CarViewed FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Along many other queries I tried, I have no idea how to get it working.
In a nutshell for a specific date, I would like to get the most common cars that were viewed.
Like :
dateViewed favouriteCarOfTheDay
2012-09-22 | Nissan
2012-09-23 | BMW
try this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt ,R.CarViewed FROM Records R
GROUP BY R.`dateViewed
ORDER BY COUNT(R.CarViewed) DESC
I think the following should work (disclaimer, not all my own work, adapted from an answer at another question)
SELECT DISTINCT
R.dateViewed,
R.CarViewed
FROM Records R
WHERE
R.dateViewed =
(SELECT R2.dateViewed FROM
(
SELECT R1.dateViewed, COUNT(*) AS numViewed
FROM Records R1
WHERE R1.CarViewed = R.CarViewed
GROUP BY R1.dateViewed
ORDER BY R1.numViewed DESC
) AS R2
LIMIT 1
)
ORDER BY r.dateViewed
Such things are really awful to do in MySQL so it might actually by slower than two correlated subquery but at least it returns both the car and it's viewcount:
SELECT counts.`dateViewed`,counts.`CarViewed` as favourite_car, counts.cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as counts JOIN
(SELECT R.`dateViewed`, MAX(cnt) as cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as q
GROUP BY R.`dateViewed`) as maxes
ON counts.cnt=maxes.cnt
Have been reading for a few days now and can't seem to get hold of my problem here.
Let me give you an insight of my DB:
table1 codes:
id CODE
1 D0G08H13L12
2 D1G12H10L12
3 D0G10H12L11
4 D1G10H10L09
5 D0G08H13L12
6 D1G12H10L12
7 D0G08H13L12
8 D1G10H10L09
9 D0G08H13L12
10 D1G12H10L12
now I'm searching for a query to count which codes occurs the most after D0G08H13L12, so searching in the next record my answer from the query would be:
3 times D1G12H10L12
1 time D1G10H10L09
this is what I have until now, I think I have to use subquery but I'm not quite sure.
SELECT id, code, COUNT(code) FROM table1 WHERE id IN ($idprint +1) GROUP BY code
ORDER BY COUNT(code) DESC
can any of you DB pro's help me in the good direction?
Try this:
SELECT COUNT(id) tot, `code`
FROM your_table
GROUP BY `code`
HAVING tot <=
(SELECT COUNT(id) FROM your_table
WHERE code = 'D0G08H13L12')
AND code <> 'D0G08H13L12'
ORDER BY tot DESC
If you want to have only two records append this to the query:
LIMIT 2
Try this, This will give u the desired result....
Select id, code, count(code)
from table1
where id>(select max(id) from table1 where code =D0G08H13L12) group by code order by count(code) desc;
select min(x.id), x.code
from yourTable x,
(
select * from yourTable
where code = 'D0G08H13L12'
) v
where
x.id > v.id
group by v.id
To get the most next codes for entered one.