How to multiply and divide in from multiple querys? - mysql

So right now i have 3 queries:
Lets call this query X
SELECT count(*) as totalDrink from
Drink D join History H where H.drinkName = D.name AND type = 'vodka' and userID = 'sai'
query Y
SELECT count(*) as totalDrink from
Drink D join Favorite F where F.drinkName = D.name AND type = 'vodka' and userID = 'sai'
query Z
SELECT SUM(totalDrinks) as total FROM (
SELECT COUNT(*) totalDrinks FROM History
WHERE userID = 'sai'
UNION ALL
SELECT COUNT(*) FROM Favorite
WHERE userID = 'sai'
) as totalDrink
Basically I want to do in mySQL and output that:
(X + Y(0.666))/Z

Most directly:
SELECT (
(
SELECT COUNT(*)
FROM Drink D JOIN History H ON H.drinkName = D.name
WHERE type = 'vodka' AND userID = 'sai'
) + (
SELECT COUNT(*)
FROM Drink D JOIN Favorite F ON F.drinkName = D.name
WHERE type = 'vodka' AND userID = 'sai'
)
) / (
(
SELECT COUNT(*) FROM History WHERE userID = 'sai'
) + (
SELECT COUNT(*) FROM Favorite WHERE userID = 'sai'
)
)

Related

Sum of grouped COUNT IN MySQL

I wrote this query
SELECT
country,
COUNT(DISTINCT tmp_tbl.user_guid) AS number_of_customers
FROM complete_tests c INNER JOIN
( SELECT DISTINCT d.dog_guid,
u.user_guid,
u.country
FROM dogs d INNER JOIN users u ON d.user_guid = u.user_guid
WHERE (u.exclude = 0 OR u.exclude IS NULL)
AND (d.exclude = 0 OR d.exclude IS NULL)
)
AS tmp_tbl ON c.dog_guid = tmp_tbl.dog_guid
GROUP BY country
ORDER BY number_of_customers DESC
And I need to add another variable that calculates the percentage of total
when I add
number_of_customers/SUM(number_of_customers)
or SUM(COUNT(DISTINCT tmp_tbl.user_guid)) / COUNT(DISTINCT tmp_tbl.user_guid)
it gives me error
Analytic functions come in handy here. Assuming you are using MySQL 8+:
SELECT country,
COUNT(DISTINCT tmp_tbl.user_guid) AS number_of_customers,
100.0 * COUNT(DISTINCT tmp_tbl.user_guid) /
SUM(COUNT(DISTINCT tmp_tbl.user_guid)) OVER () AS pct_customers
FROM complete_tests c
INNER JOIN
(
SELECT DISTINCT d.dog_guid, u.user_guid, u.country
FROM dogs d
INNER JOIN users u ON d.user_guid = u.user_guid
WHERE (u.exclude = 0 OR u.exclude IS NULL) AND
(d.exclude = 0 OR d.exclude IS NULL)
) AS tmp_tbl
ON c.dog_guid = tmp_tbl.dog_guid
GROUP BY
country
ORDER BY
number_of_customers DESC;

Limit results to 1 row for each group in subquery

as a result of the execution of this query, situations are possible when two rows have the same minimum price, but i still need to select one. I understand perfectly well that the standard limit cannot be dispensed with here. I do not have enough knowledge to understand from which side to approach the solution of this issue. Thank you in advance for your attension.
UPDATE offers t1
SET t1.deleted_at = NOW()
WHERE t1.id
NOT IN
(
SELECT f.id
FROM (
SELECT name, MIN(net_price) as minprice
FROM offers
WHERE
supplier_id = (SELECT id FROM suppliers WHERE name = 'somename')
group BY name
)
AS x inner join (SELECT * FROM offers) AS f ON f.name = x.name AND f.net_price = x.minprice
)
AND
t1.supplier_id = (SELECT id FROM suppliers WHERE name = 'somename');
I don't know somehowe, but this works for me:
UPDATE offers t1
SET t1.deleted_at = NOW()
WHERE t1.id
NOT IN
(
SELECT f.id
FROM (
SELECT id, name, MIN(net_price) as minprice
FROM offers
WHERE
supplier_id = (SELECT id FROM suppliers WHERE name = 'somename')
group BY name
)
AS x inner join (SELECT * FROM offers) AS f ON f.id = x.id
)
AND
t1.supplier_id = (SELECT id FROM suppliers WHERE name = 'somename');
Just add id to subquery select and put it on inner join f.id = x.id

how i can use if else into sum - mysql statement

i could not find the correct query of sql, how i can use if else statement in sum?? i want add sum(if(noresult),0,number) if there is no any result. but it doesnt work.
INNER JOIN ( SELECT
COUNT(*), SUM(cekilen_tutar) AS bal_total
FROM alinanbaliklar
WHERE user_id = 1
GROUP BY user_id )balik
INNER JOIN ( SELECT
COUNT(*), SUM(cekilen_tutar) AS dom_total
FROM alinandomuzlar
WHERE user_id = 1
GROUP BY user_id )domuz
INNER JOIN ( SELECT
COUNT(*),SUM(cekilen_tutar) AS seb_total
FROM alinanmeyvesebzeler
WHERE user_id = 1
GROUP BY user_id )sebze
INNER JOIN ( SELECT
COUNT(*), SUM(cekilen_tutar) AS ag_total
FROM alinanagaclar
WHERE user_id = 1
GROUP BY user_id )agac
You are only selecting one row. So, dispense with the GROUP BY and use CROSS JOIN:
FROM (SELECT COUNT(*), SUM(cekilen_tutar) AS bal_total
FROM alinanbaliklar
WHERE user_id = 1
) balik CROSS JOIN
(SELECT COUNT(*), SUM(cekilen_tutar) AS dom_total
FROM alinandomuzlar
WHERE user_id = 1
) domuz CROSS JOIN
(SELECT COUNT(*), SUM(cekilen_tutar) AS seb_total
FROM alinanmeyvesebzeler
WHERE user_id = 1
) sebze CROSS JOIN
(SELECT COUNT(*), SUM(cekilen_tutar) AS ag_total
FROM alinanagaclar
WHERE user_id = 1
) agac

How to count records from multiple columns Mysql

How to write count mysql query from multiple columns.I have UID which is common in all tables so I framed my query like this It does the job but is there a better way to write multiple count queries
SELECT
(SELECT count(*) from follow WHERE followed_user_uid = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') as following,
(SELECT count(*) from follow WHERE my_user_uid= 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') as followers,
SUM(
(SELECT count(*) from prac_test where UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') +
(select count(*) from multi_test where my_UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') +
(select count(*) from shadow_test where UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f')
) as totalTestCount;
SELECT * FROM
(SELECT
(SELECT count(*) from follow AS flw WHERE flw.followed_user_uid = param.user_id) as following,
(SELECT count(*) from follow AS flw WHERE flw.my_user_uid= param.user_id) as followers,
SUM(
(SELECT count(*) from prac_test AS pt where pt.UID = param.user_id) +
(select count(*) from multi_test AS mt where mt.my_UID = param.user_id) +
(select count(*) from shadow_test AS st where st.UID = param.user_id)
) AS totalTestCount
FROM
(SELECT
'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') AS user_id,
) AS param
)AS tmp;

Mysql query distinct + multiple

I want to do this query:
SELECT *
FROM user k
INNER JOIN (
SELECT id, tagName, b.guid, name, owner, publicKey
FROM noteTags a
INNER JOIN (
SELECT *
FROM note
ORDER BY guid
LIMIT 0 , 12
)b ON a.guid = b.guid ORDER BY b.id DESC
)l ON k.owner = l.owner
But I want it to return DISTINCT b.guids.
Structure of the tables:
note
|
|=id
|=name
|=guid
|=owner
|=publicKey
noteTags
|
|=guid
|=tagName
user
|
|=owner
|=username
|=auth
Basically I want to select ALL data (with the limit on the deeper inner join) and return DISTINCT guids
Thanks!
Here's my initial answer,
SELECT *
FROM note a
INNER JOIN user b
ON a.owner = b.owner
INNER JOIN notetags c
ON a.guid = b.guid
INNER JOIN
(
SELECT guid, MAX(tagName) maxTag
FROM notetags
GROUP BY guid
) d ON c.guid = d.guid AND
c.tagName = d.maxTag
How about:
SELECT *
FROM user k
INNER JOIN (
SELECT id, tagName, b.guid, name, owner, publicKey
FROM noteTags a
INNER JOIN (
select id, name, MIN(guid) as guid, owner, publicKey
FROM note
GROUP BY guid
LIMIT 0 , 12
)b ON a.guid = b.guid ORDER BY b.id DESC
)l ON k.owner = l.owner