how i can use if else into sum - mysql statement - mysql

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

Related

I need to get specific ids from db if these are in current and last quarter using SQL

[DB Table]
SELECT b.first_name, b.last_name, a.pod_name, a.category, c.user_id,
SUM(IF(QUARTER(CURDATE())-1 OR (QUARTER(CURDATE())-2) AND a.user_id, 1, 0)) AS flag FROM kudos a
INNER JOIN users b ON a.user_id = b.id INNER JOIN users_groups c ON a.user_id = c.user_id
INNER JOIN groups d ON c.group_id = d.id WHERE a.group_name = 'G2' AND d.id IN (7,8,9,11,12,13,14,15,16,17,21,22,23,24,25,26,27,28)
AND QUARTER(CURDATE())-1 = a.quarter ORDER BY a.final_score+0 DESC
I need to get the user_ids of those users which are both in quarter 1 and 2 from table.
Tried above query but failed to get expected results.
Can someone please guide me on this?
if you only need user_id then you can do this :
select user_id
from tablename
where quarter in (1,2)
group by user_id
having count(distinct quarter) = 2
another way is to use window function, assuming you have one user id in each quarter:
select * from (
select * , count(*) over (partition by user_id) cn
from tablename
where quarter in (1,2)
) t where cn = 2

Mysql Group By get latest record with Count

This is my customer table.
I want to group by emp_id alongwith the count. But Group By gets the 'first' record and not the 'newest' one.
I have tried various queries, like this
SELECT id, emp_id, COUNT( * ) AS count, created_at
FROM customer c
WHERE created_at = (
SELECT MAX( created_at )
FROM customer c2
WHERE c2.emp_id = c.emp_id
)
GROUP BY emp_id
ORDER BY created_at DESC
LIMIT 0 , 30
But cannot get the count. Please help.
Edit: this answer doesn't help to obtain count
Try joining to a subquery:
SELECT c1.id, c1.emp_id, c1.created_at, c2.cnt
FROM customer c1
INNER JOIN
(
SELECT emp_id, MAX(created_at) AS max_created_at, COUNT(*) AS cnt
FROM customer
GROUP BY emp_id
) c2
ON c1.emp_id = c2.emp_id AND c1.created_at = c2.max_created_at;
please try this
SELECT cust1.id, cust1.emp_id, cust1.created_at, cust2.cnt
FROM customer cust1
INNER JOIN
(
SELECT emp_id, MAX(created_at) AS max_created_at, COUNT(*) AS count
FROM customer
GROUP BY emp_id
) cust2
ON cust1.emp_id = cust2.emp_id AND cust1.created_at = cust2.max_created_at;

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 - SUM of COUNT from multiple tables

I have three tables:
customers: id, name
contracts_jewels: id, customer_id, paid, transferred, final_date
contracts_objects: id, customer_id, paid, transferred, final_date
As you see, the structure of the last two tables is the same.
The "paid" and the "transferred" fields contain the value 0 or 1.
What I need is to make a query which should return all the clients (no matter if they have contracts or not), and for each client:
id, name, count_contracts_all, count_contracts_active
where:
count_contracts_all would mean the sum of [SELECT COUNT( * ) FROM
contracts_jewels WHERE customer_id=3 (for example)] and [SELECT
COUNT( * ) FROM contracts_objects WHERE customer_id=3 (for example)]
count_contracts_active would mean the sum of [SELECT COUNT( * ) FROM
contracts_jewels WHERE customer_id=3 AND final_date>=Now() AND paid=0
AND transferred=0] and [SELECT COUNT( * ) FROM contracts_objects WHERE
customer_id=3 AND final_date>=Now() AND paid=0 AND transferred=0]
Any idea? Would you please help me? Thank you!
You can count the contracts separately and then just join them up to the customers:
SELECT
c.id,
COALESCE(oc.active_count,0) + COALESCE(jc.active_count,0) as count_contracts_active,
COALESCE(oc.total_count,0) + COALESCE(jc.total_count,0) as count_contracts_all
FROM customers c
LEFT JOIN (
SELECT
customer_id
COUNT(*) as total_count,
COUNT(IF(final_date>=Now() AND paid=0 AND transferred=0,1,NULL)) as active_count
FROM contracts_jewels
GROUP BY customer_id
) as oc ON oc.customer_id = c.id
LEFT JOIN (
SELECT
customer_id
COUNT(*) as total_count,
COUNT(IF(final_date>=Now() AND paid=0 AND transferred=0,1,NULL)) as active_count
FROM contracts_objects
GROUP BY customer_id
) as jc ON jc.customer_id = c.id
One fast solution I can think of right now is:
SELECT COUNT(`temp_table`.*) FROM (
SELECT * FROM contracts_jewels WHERE customer_id=3 UNION ALL
SELECT * FROM contracts_objects WHERE customer_id=3) AS `temp_table`
AND
SELECT COUNT(`temp_table`.*) FROM (
SELECT * FROM contracts_jewels WHERE customer_id=3 AND final_date>=Now() AND paid=0 AND transferred=0 UNION ALL
SELECT * FROM contracts_objects WHERE customer_id=3 AND final_date>=Now() AND paid=0 AND transferred=0) AS `temp_table`
You can join each of those tables twice and add their corresponding COUNTs in your result:
SELECT
c.id,
(COUNT(cj1.id)+COUNT(co1.id)) AS count_contracts_all,
(COUNT(cj2.id)+COUNT(co2.id)) AS count_contracts_active
FROM
customers c
LEFT OUTER JOIN contracts_jewels cj1 ON c.id = cj1.customer_id
LEFT OUTER JOIN contracts_objects co1 ON c.id = co1.customer_id
LEFT OUTER JOIN contracts_jewels cj2 ON
c.id = cj2.id AND
cj2.final_date >= NOW() AND
cj2.paid = 0 AND
cj2.transferred = 0
LEFT OUTER JOIN contracts_object co2 ON
c.id = co2.id AND
co2.final_date >= NOW() AND
co2.paid = 0 AND
co2.transferred = 0
GROUP BY c.id
Note: I haven't run this, but hopefully it sets you in the right direction.
simple solution:
SELECT SUM(c) FROM (
SELECT COUNT(1) as c FROM `tbl1` where ...
UNION
SELECT COUNT(1) as c FROM tbl2 where ...
UNION
SELECT COUNT(1) as c FROM tbl3 where ...
) al

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