I have the following MySQL line:
SELECT age, count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age and I need to add an additional column to it that shows ONLY the max value of the count(*) for every row. For example:
13 2 7
18 2 7
23 5 7
24 7 7
26 6 7
32 3 7
38 1 7
41 1 7
46 4 7
This would be 3 columns and the 3rd column shows 7 since 7 was the highest number in the second column where the count(*) is made.
Here the solution:
select age,
count(*),
(select max(c) from
(select count(*) as c from pacient where age between 20 and 40 group by age) as x) as t
from pacient
where age between 20 and 40
group by age
order by age;
Have you tried to wrap your query with another query? something like
SELECT A.age, A.total, MAX(A.total) as max_value FROM (
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age) as A
GROUP BY A.age, A.total
select
p.Age,
count(*) CountPerAge,
max(ar.AllRecs) AllRecs
from
pacient p,
( select count(*) AllRecs
from pacient p2
where p2.age between 20 and 40 ) ar
where
p.age between 20 and 40
group by
p.age
By doing a join to the second "subselect" with no join condition, it will give a Cartesian result... Since it is a count with no group by, it will always return a single record and thus be joined to all age rows otherwise. The MAX() of the value is no problem since it is the only record will just be returned as-is.
It is always good to use SQL VIEWS instead of using sub queries. Because VIEW will be having already compiled result.
CREATE VIEW subqueryView
SELECT age, count(*) AS total
FROM pacient
WHERE age BETWEEN 20 AND 40
GROUP BY age ORDER BY age
SELECT A.age, A.total, MAX(A.total) as max_value FROM (SELECT FROM subqueryView) as A
GROUP BY A.age, A.total
Related
Hi I have this table.
id lat lng userId
1 12 23 1
2 45 34 2
3 42 34 3
4 33 34 1
5 36 79 2
6 53 98 2
7 23 90 3
8 23 67 1
Here we have three users. (user ids 1,2,3). I want to get lateset record (id column max value) of each user.
My excepted output is this
userId lat lng
1 23 67
2 53 98
3 23 90
This query will give me group by option
SELECT
*
FROM
covid.locations
GROUP BY userId;
But how do I combine this with MAX(id) function.
One way is to use the following:
SELECT
cl.*
FROM covid.locations cl
INNER JOIN (
SELECT
userid
, MAX( id ) mid
FROM covid.locations
GROUP BY
userId
) g ON cl.userid = g.userid
AND cl.id = cl.mid
Another is to use row_number() over()
SELECT
userId
, lat
, lng
FROM (
SELECT
*
, ROW_NUMBER() OVER (PARTITION BY userid ORDER BY id DESC) rn
FROM covid.locations
GROUP BY
userId
) d
WHERE rn = 1
Both will identify the "most recent" row in the source table based in the id column of that table. Note that the second query requires MySQL version 8+ as this is when row_number() became supported in that database. The first query should run in dbms supporting SQL.
This will do
SELECT
*
FROM
covid.locations
where id in (select max(t.id) from covid.locations t group by t.userId)
order by id desc;
An example of the above query can be found in this SQLFiddle
I am new with mysql and working to change a store application to make it have two stock. I created a table to store stock quantity:
Then I plan to create a view with stock quantity, per store, per SKU. I using the following query:
SELECT
`stockList`.`sku`,
SUM(A.`stockQty`) AS 'store1',
SUM(B.`stockQty`) AS 'store2',
SUM(`stockList`.`stockQty`) AS 'total'
FROM `stockList`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=1
) AS A
ON `stockList`.`sku`=A.`sku`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=2
) AS B
ON `stockList`.`sku`=B.`sku`
GROUP BY `stockList`.`sku`
Per resulting table, calculation is not proper and I could not identify the logic:
SKU 43 should show for store1 = 9 and for store2 = 10, total = 19. This is what they show if I execute the select queries alone. Please, let me know if I misunderstood how this sum logic works.
You might to use SUM on subquery to calculate Totle price by sku
LEFT JOIN may make some fields not match causing NULL so use IFNULL to preset value 0
You can try this.
SELECT
T.sku,
SUM(T.stockQty) as totle,
IFNULL(A.`store1`,0) AS `store1`,
IFNULL(B.`store2`,0) AS `store2`
FROM `stockList` AS T
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store1`
FROM `stockList`
WHERE `idStock`=1
GROUP BY sku
) as A ON A.sku = T.sku
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store2`
FROM `stockList`
WHERE `idStock`=2
GROUP BY sku
) AS B ON T.sku =B.sku
GROUP BY T.sku
sqlfiddle
Your query is much more complicated than it needs to be. You can just do this:
SELECT
sku,
SUM(stockQty) as total,
SUM(IF(idStock=1,stockQty,0)) AS `store1`,
SUM(IF(idStock=2,stockQty,0)) AS `store2`
FROM `stockList`
GROUP BY sku
Output:
sku total store1 store2
36 10 10 0
37 3 3 0
38 4 4 0
39 3 3 0
40 10 10 0
41 12 12 0
42 12 12 0
43 19 9 10
I need some help to solve an issue with my query. I want to join the output of two select statements:
1st
select extract(year from createdDate) as year,
count(extract(year from createdDate)) as count
from table
where to_user_id= 322
group by extract(year from createdDate);
and its output
Year Count
2014 18
2015 117
2016 9
and 2nd query
select count(extract(year from createdDate)) as count
from table
where userId=322
group by extract(year from createdDate);
and its output
Count
18
110
11
I want to add this two tables into one table.
I want that type of output,
Year Count Count
2014 18 18
2015 117 110
2016 9 11
Note that I use to_user_id in query 1 but userId in query 2.
I tried to solved out this thing but I got repeated values in the output.
Anyone know the solution?
Write them as subqueries and join them together.
SELECT a.year, a.count AS t_user_count, b.count AS user_count
FROM (select YEAR(create_date) AS year, COUNT(*) AS count
FROM table
WHERE to_user_id = 322
GROUP BY year) AS a
JOIN (SELECT YEAR(create_date) AS year, COUNT(*) AS count
FROM table
WHERE user_id = 322
GROUP BY year) AS b
ON a.year = b.year
I want to select the total number of different class_id in which at least two students who share the same the birthday.
class_id student_id birthday
1 30 1994-10-01
1 23 1994-01-01
1 19 1994-02-01
1 11 1994-03-01
2 9 1994-02-01
2 43 1994-03-01
3 41 1994-06-01
3 21 1994-05-01
4 9 1992-05-22
4 20 1992-09-05
Write a subquery that finds all the duplicate birthdays in the same class. Then count the number of different classes with SELECT COUNT(DISTINCT class_id) from that subquery.
SELECT COUNT(DISTINCT class_id) FROM (
SELECT class_id, birthday
FROM YourTable
GROUP BY class_id, birthday
HAVING COUNT(*) > 1) AS x
In the inner select group by the class_id and take only those that have different numbers of unique and total birthdays.
Then count those class_ids in the outer select.
select count(*)
from
(
select class_id
from your_table
group by class_id
having count(*) > count(distinct birthday)
) tmp
I have this following query which I want to SUM the count table for any counts that are greater than 1. I'm not sure exactly how to go about doing this:
SELECT count(id) as count,
id,
location_id,
time,
weekofyear(time)
from table where weekofyear(time) = '28'
group by location_id, id
having count > 1
order by count desc
Results:
count id location_id time weekofyear(time)
5 32265 409 7/12/14 3:58 28
5 32266 409 7/12/14 3:59 28
5 27532 399 7/12/14 4:54 28
4 31124 41 7/7/14 4:41 28
So I would like the sum to show 19
A simple subselect from your own query should do it:
SELECT
SUM(t.count)
FROM (
SELECT count(id) as count,
id,
location_id,
time,
weekofyear(time)
from table where weekofyear(time) = '28'
group by location_id, id
having count > 1
) as t