I have this query:
SELECT `facilities`.`name` AS facility, `countries`.`name` AS country, `states`.`name` AS state, `users`.`dosage` AS dosage, COUNT(`users`.`id`) AS registrations
FROM `users`
LEFT JOIN `countries` ON `users`.`country_id` = `countries`.`id`
LEFT JOIN `states` ON `users`.`state_id` = `states`.`id` LEFT JOIN `user_facilities` ON `users`.`id` = `user_facilities`.`user_id`
LEFT JOIN `facilities` ON `user_facilities`.`facility_id` = `facilities`.`id`
WHERE `users`.`dosage` != "Not sure"
GROUP BY `facilities`.`name`, `countries`.`name`, `states`.`name`, `users`.`dosage`
ORDER BY `facilities`.`id` ASC, `countries`.`id` ASC, `states`.`name` ASC
It gives me this result:
Hospital | United States | Arkansas | 10 doses | 3
Hospital | United States | Arkansas | >10 doses | 4
Home care | United States | Arkansas | 10 doses | 1
Home care | United States | Texas | 10 doses | 1
My users table:
id | email | state_id | country_id | dosage | percent_completed
What I need to do is to get the number of users with percent_completed = 100 for each result. I tried subqueries, but I couldn't find the right subquery for this. I'm stuck. Does anyone have some piece of advice?
Thanks.
If I understand correctly, you can use conditional aggregation. In MySQL you can just do:
select . . .,
sum(percent_completed = 100) as num_at_100
After group by please add the " having sum(percent_completed) >=100 " that will will you users count who are completed > =100 percent
SELECT `facilities`.`name` AS facility, `countries`.`name` AS country, `states`.`name` AS state, `users`.`dosage` AS dosage, COUNT(`users`.`id`) AS registrations
FROM `users`
LEFT JOIN `countries` ON `users`.`country_id` = `countries`.`id`
LEFT JOIN `states` ON `users`.`state_id` = `states`.`id` LEFT JOIN `user_facilities` ON `users`.`id` = `user_facilities`.`user_id`
LEFT JOIN `facilities` ON `user_facilities`.`facility_id` = `facilities`.`id`
WHERE `users`.`dosage` != "Not sure"
GROUP BY `facilities`.`name`, `countries`.`name`, `states`.`name`, `users`.`dosage` having sum(users.percentage_completed) >= 100
ORDER BY `facilities`.`id` ASC, `countries`.`id` ASC, `states`.`name` ASC
Related
I'm trying to Join two query results as one with no luck so far.
I tried Union but that just adds the second query result after the first query result.
Tried scipping the second query alltogether and use something like cross join but that always returned the same row data from the table in the second query.
The first query is this:
SELECT
`namelist`.`id`,
`name`.`id` AS `name_id`,
`name_item`.`content`,
`order`.`create_time`
FROM
`namelist`
LEFT JOIN `name` ON `name`.`namelist_id` = `namelist`.`id`
LEFT JOIN `name_item` ON `name_item`.`name_id` = `name`.`id`
LEFT JOIN `order` ON `namelist`.`order_id` = `order`.`id`
LEFT JOIN `items` ON `items`.`id` = `name_item`.`items_id`
WHERE
`namelist`.`order_id`=1380 AND `items`.`key`='Name'
GROUP BY `name_item`.`content`
ORDER BY `name`.`id`
The second query:
SELECT `validity`, `code`, `image` FROM `code` WHERE `code`.`order_id` = 1380 ORDER BY `id`
And as a result I would like to get something like this:
id | name_id | content | create_time | validity | code | image
--------------------------------------------------------------
1 | 1 | nameone | 2022-10-01 | somedate | 123 | 123.png
1 | 2 | nametwo | 2022-10-01 | somedate | 567 | 567.png
The querys return the same number of rows but they have no common identifier, and the reseult of the second query can not be duplicated because they have unique code colum.
SELECT id,
name_id,
content,
create_time,
validity,
code,
image
FROM
(-- first query
SELECT `namelist`.`id`,
`name`.`id` AS `name_id`,
`name_item`.`content`,
`order`.`create_time`,
ROW_NUMBER() OVER (ORDER BY `name`.`id`) AS rn
FROM `namelist`
LEFT JOIN `name` ON `name`.`namelist_id` = `namelist`.`id`
LEFT JOIN `name_item` ON `name_item`.`name_id` = `name`.`id`
LEFT JOIN `order` ON `namelist`.`order_id` = `order`.`id`
LEFT JOIN `items` ON `items`.`id` = `name_item`.`items_id`
WHERE `namelist`.`order_id`=1380
AND `items`.`key`='Name'
GROUP BY `name_item`.`content`
-- ORDER BY `name`.`id`
) AS subquery1
JOIN
(-- second query
SELECT `validity`,
`code`,
`image` ,
ROW_NUMBER() OVER (ORDER BY `id`) rn
FROM `code`
WHERE `code`.`order_id` = 1380
-- ORDER BY `id`
) AS subquery2 USING (rn)
ORDER BY rn
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT SUM( li_2.quantity ) AS counted
FROM inventory li_2
WHERE li_1.product_id = li_2.product_id
) counted_table
GROUP BY li_1.product_id
ORDER BY li_1.id DESC
I'm attempting to use the parent id (product_id) to count the total amount of quantity for each product in the subquery - But I only get the standard mysql error message.
So something like
id | quantity | total
---------------------------------
0001 | 2 | 6
| |
0001 | 4 | 6
What could be wrong?
if you change sub-query, it could be fixed
SELECT li_1.carrier, li_1.product_id, li_1.quantity, products_description.products_name, sites.sites_id, sites.sites_name, counted_table.counted
FROM inventory li_1
INNER JOIN products_description ON li_1.product_id = products_description.products_id
INNER JOIN sites ON products_description.data = sites.data
INNER JOIN (
SELECT product_id,SUM( li_2.quantity ) AS counted
FROM inventory li_2
GROUP BY li_2.product_id
) counted_table ON li_1.product_id = counted_table.product_id
ORDER BY li_1.id DESC
I'm building a little conjugation/radicalization app, and I have stumbled upon a problem. I have this SQL request:
SELECT DISTINCT RA.*
FROM radical RA
left join conjugated CO
on CO.word_id = RA.id
where CO.conjugation IN ('I', 'am', 'a', 'cat')
That returns:
| id | radical |
| 13 | to be |
However, I would like to get a result of the type:
| id | radical | word |
| null | null | I |
| 13 | to be | am |
| null | null | a |
| null | null | cat |
Does anyone know how?
You need a left join, but to start with all the words you want to keep:
select w.word, ra.*
from (select 'I' as word union all
select 'am' union all select 'a' union all select 'cat'
) w left join
conjugated co
on co.conjugation = w.word left join
radical ra
on ra.id = co.word_id;
If these values are in conjugation, you can simply do:
select c.onjugation, ra.*
from conjugated co left join
radical ra
on ra.id = co.word_id
where c.conjugation in ('I', 'am', 'a', 'cat') ;
That is, conjugation should be first, because you want to keep all matching rows in that table.
Seemingly you are using a Left Join, when you actually need a Right join (since it appears you want all rows of the right table matching the predicate to be returned)
So either switch the join:
SELECT DISTINCT RA.*, co.`conjugated` as word
FROM radical RA
right join conjugated CO
on CO.word_id = RA.id
where CO.conjugation IN ('I', 'am', 'a', 'cat');
Or switch the order of the tables in the FROM:
SELECT DISTINCT RA.*, co.`conjugated` as word
FROM conjugated CO
left join radical RA
on CO.word_id = RA.id
where CO.conjugation IN ('I', 'am', 'a', 'cat');
I've got a SELECT with multiple JOINS for a paginated Tableview. In general this is working for unfiltered results.
The query looks like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Now I've got no clue how to accomplish filtering the results with a condition related to one of the join tables.
When I just set a:
LEFT JOIN tablex AS table ON foreign_table.tblx_uid = table.uid AND {condition}
this condition regards only to the 50 results of the nested SELECT.
Is there any way to achieve using WHERE clauses on the JOIN tables in this scenario?
For sample data see http://sqlfiddle.com/#!2/fad4d/2
Expected results:
to get x team records limited to 5 team uids, where Tournament2 is one of the related tournaments for the team.
Best regards
w1ll1
Try not controlling the pagination in that subquery, instead just use a more conventional query with a composite where clause. HOWEVER, because you are using left joins take care adding filters through the where clause that would override the outer join to produce the effect of an inner join.
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM selecttable AS seltable
LEFT JOIN table1 AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN table2 AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN table3 AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN table4 AS tbl4 ON tbl3.tbl4_uid = tbl4.uid
WHERE seltable.value = 99
...
ORDER BY seltable.datetime DESC
LIMIT 50 OFFSET 0
Alternatively use more subqueries, like this:
SELECT seltable.*,
tbl2.name AS tbl2name,
tbl3.name AS tbl3name,
tbl4.name AS tbl4name
FROM
( SELECT * FROM selecttable
WHERE value = 99
ORDER BY datetime DESC
LIMIT 50 OFFSET 0 )
AS seltable
LEFT JOIN ( SELECT uid, name
FROM table1
WHERE 1=1 -- amend to suit
) AS tbl1 ON seltable.tbl1_uid = tbl1.uid
LEFT JOIN ( SELECT uid, name
FROM table2
WHERE 1=1 -- amend to suit
) AS tbl2 ON tbl1.tbl2_uid = tbl2.uid
LEFT JOIN ( SELECT uid, name
FROM table3
WHERE 1=1 -- amend to suit
) AS tbl3 ON tbl2.tbl3_uid = tbl3.uid
LEFT JOIN ( SELECT uid, name
FROM table4
WHERE 1=1 -- amend to suit
) AS tbl4 ON tbl3.tbl4_uid = tbl4.uid;
Here is another attempt, based on your sqlfiddle it appears that INNER JOINS may be used:
SELECT theteam.*,
trnmnt.name AS tournamentname,
cat.name AS categoryname,
sport.name AS sportname
FROM (
SELECT * FROM team
ORDER BY team.name ASC )
AS theteam
INNER JOIN tournament_team AS tntm ON tntm.team_uid = theteam.uid
INNER JOIN tournament AS trnmnt ON tntm.tournament_uid = trnmnt.uid AND trnmnt.name = 'Tournament2'
INNER JOIN category AS cat ON trnmnt.category_uid = cat.uid
INNER JOIN sport ON cat.sport_uid = sport.uid
LIMIT 5 OFFSET 0
;
The result of that query is:
| UID | NAME | TOURNAMENTNAME | CATEGORYNAME | SPORTNAME |
|-----|--------|----------------|--------------|-----------|
| 2 | Team02 | Tournament2 | Germany | Soccer |
| 3 | Team03 | Tournament2 | Germany | Soccer |
| 4 | Team04 | Tournament2 | Germany | Soccer |
| 5 | Team05 | Tournament2 | Germany | Soccer |
| 6 | Team06 | Tournament2 | Germany | Soccer |
i have a little problem with the following Query. It returns multiple values.
Here is the result I'm receiving:
+----+--------------+--------+--------+---------------------+----------+------------------+
| id | company | county | vm | os | products | sn |
+----+--------------+--------+--------+---------------------+----------+------------------+
| 1 | ABC Corp | USA | VMW | Linux, Linux, Linux | 3 | A123, B234, A343 |
| 2 | DEF Corp | USA | CIT | Windows | 1 | I223 |
+----+--------------+--------+--------+---------------------+----------+------------------+
As you can see, the first line shows 3 times Linux, but this should be only listed once. I've seen that this problem only occurs if the customer has more than 1 product. I think i have to Group my Query or something like this, but i don't know how.
Here is my Query:
SELECT
customer.id,
customer.company,
countries.en AS country,
vmenv.name AS vm,
GROUP_CONCAT(operatingsystems.name SEPARATOR ', ') AS os,
COUNT(device2customer.sn) AS products,
GROUP_CONCAT(device2customer.sn SEPARATOR ', ') AS sn
FROM
customer
LEFT JOIN
countries
ON
customer.country = countries.id
LEFT JOIN
vmenv2kunden
ON
vmenv2kunden.customerid = customer.id
LEFT JOIN
vmenv
ON
vmenv2kunden.vmenvnr = vmenv.id
LEFT JOIN
operatingsystems2customer
ON
operatingsystems2customer.customerid = customer.id
LEFT JOIN
operatingsystems
ON
operatingsystems2customer.osnr = operatingsystems.id
LEFT JOIN
device2customer
ON
device2customer.kundenid = customer.id
GROUP BY
customer.id
In your query change the group_concat statement to
GROUP_CONCAT(DISTINCT operatingsystems.name SEPARATOR ', ') AS os,
COUNT(DISTINCT device2customer.sn) AS products,
GROUP_CONCAT(DISTINCT device2customer.sn SEPARATOR ', ') AS sn
By adding the DISTINCT key word it should work for you
you just need yo use distinct in Group_concat.
Please try the below updated Query.
SELECT
customer.id,
customer.company,
countries.en AS country,
vmenv.name AS vm,
GROUP_CONCAT(distinct operatingsystems.name SEPARATOR ', ') AS os,
COUNT(device2customer.sn) AS products,
GROUP_CONCAT(device2customer.sn SEPARATOR ', ') AS sn
FROM
customer
LEFT JOIN
countries
ON
customer.country = countries.id
LEFT JOIN
vmenv2kunden
ON
vmenv2kunden.customerid = customer.id
LEFT JOIN
vmenv
ON
vmenv2kunden.vmenvnr = vmenv.id
LEFT JOIN
operatingsystems2customer
ON
operatingsystems2customer.customerid = customer.id
LEFT JOIN
operatingsystems
ON
operatingsystems2customer.osnr = operatingsystems.id
LEFT JOIN
device2customer
ON
device2customer.kundenid = customer.id
GROUP BY
customer.id