How to count records from multiple columns Mysql - 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;

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;

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

Print name of all activities with neither maximum nor minimum number of participants

How can i solve this question of hackerrank. I am quite confused an unable to move forward with this.
here is what i have tried
SELECT DISTINCT ACTIVITY FROM FRIENDS,
SELECT max(ACTIVITY) AS M
WHERE M = (SELECT NAME FROM Activities);
You can use window functions if your mysql version is 8 or above
select activity from (select activity, count(*) as cnt,
max(count(*)) over () as maximum_cnt,
min(count(*)) over () as minimum_cnt
from friends group by activity) mytable
where cnt not in (maximum_cnt, minimum_cnt);
I solved this using mysql
select activity
from
(select activity, count(activity) as cnt
from friends
group by activity
having cnt <> (select count(activity) from friends group by activity order by count(activity) limit 1)
and
cnt <> (select count(activity) from friends group by activity order by count(activity) desc limit 1)
) as x;
I like to solve tasks like this with left join and checking NULL:
SELECT
ACTIVITY,
SUM(1) AS Total
INTO #ActivityCount
FROM FRIENDS
GROUP BY ACTIVITY
SELECT
ActivityCount.ACTIVITY
FROM #ActivityCount
LEFT JOIN (SELECT MAX(ActivityCount.Total) AS Total FROM ActivityCount) AS MaxTotal
ON ActivityCount.Total = MaxTotal.Total
LEFT JOIN (SELECT MIN(ActivityCount.Total) AS Total FROM ActivityCount) AS MinTotal
ON ActivityCount.Total = MinTotal.Total
WHERE
MaxTotal.Total IS NULL
AND MinTotal.Total IS NULL
Here is what i did
Select ACTIVITY from Friends
group by ACTIVITY
having count(ACTIVITY) <> (select count1 from (select count(ACTIVITY)
count1 from Friends
group by (ACTIVITY)
order by count1 asc)
where rownum <= 1) and count(ACTIVITY) <> (select count2 from (select count(ACTIVITY) count2 from Friends group by (ACTIVITY) order by count2 desc) where rownum <= 1);
with temp as
(
select activity, count(id) as act_count from friends
group by activity
)
select activity from temp
where act_count not in (select max(act_count) from temp)
and act_count not in (select min(act_count) from temp);

SQL - Select discint AND count from JOIN query

I have below query:
SELECT u.*
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt
FROM (SELECT DISTINCT user_by
FROM xeon_users_rented
) AS xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50
Which selects some data from my database. The above query works fine. However, I would like to also select count(*) from xeon_users_rented where user_by = u.username This is what I have attempted:
SELECT u.*
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt,
(SELECT DISTINCT count(*)
FROM xeon_users_rented
WHERE xur.user_by = u.username
) AS ttl
FROM (SELECT DISTINCT user_by
FROM xeon_users_rented
) AS xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50
However, that gives me the total number of rows in xeon_users_rented as ttl - not the total distinct rows where username = user_by
I think you can do what you want just by tinkering with your subquery a little. That is, change the select distinct to a group by:
SELECT u.*, xur.cnt,
(SELECT sum(trs.amount)
FROM transactions trs
WHERE u.id = trs.user AND trs.type = 'Recycle' AND
trs.TIME >= UNIX_TIMESTAMP(CURDATE())
) as amt
FROM (SELECT user_by, COUNT(*) as cnt
FROM xeon_users_rented
GROUP BY user_by
) xur JOIN
users u
ON xur.user_by = u.username
LIMIT 50;
Some notes:
SELECT DISTINCT is not really necessary, because you can do the same logic using GROUP BY. So, it is more important to understand GROUP BY.
You are using LIMIT with no ORDER BY. That means that you can get a different set of rows each time you run the query. Bad practice.

How to multiply and divide in from multiple querys?

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'
)
)