I have two tables ... accounts & tax. In table tax I would like to updated records (?) depending on column category available in table accounts like this:
table 'accounts'
id|cat2|cat3
1|A|Active
2|A|Active
3|A|Inactive
4|A|Active
5|B|Inactive
6|B|Active
table 'tax'
id|category|count_total|count_active|count_inactive
1|A|?|?|?
2|B|?|?|?
Desired result:
id|category|count_total|count_active|count_inactive
1|A|4|3|1
2|B|2|1|1
For count_total I tried this:
UPDATE tax t2,
( SELECT count(*)
FROM accounts
) t1
SET t2.count_total = t1.count(*)
WHERE t1.cat2 = t2.category;
You can use join to update your tax table
UPDATE tax t2
JOIN (
SELECT
cat2,
COUNT(*) cnt,
SUM(cat3 = 'Active') count_active,
SUM(cat3 = 'Inactive') count_inactive
FROM
accounts
GROUP BY cat2
) t1
ON t1.cat2 = t2.category
SET t2.count_total = t1.cnt ,
t2.count_active = t1.count_active,
t2.count_inactive = t1.count_inactive
Demo
Another version without JOIN:
UPDATE tax
SET
count_total = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category
),
count_active = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Active'
),
count_inactive = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Inactive'
);
Demo
Related
How do I SUM all users, I have two different users. One is student and the second is instructor.
SELECT
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'student') as student,
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'instructor') as instructor,
(SELECT SUM(user_role) FROM tbl_users) as totaluser
and why it doesn't add result as 1
in totaluser? It's 1 + 0 = 1. There should be at least 1 total user(s).
This is what you need
select student, instructor, student+instructor totaluser FROM (
select
sum( if( user_role = 'student', 1, 0 ) ) AS student,
sum( if( user_role = 'instructor', 1, 0 ) ) AS instructor
from tbl_users ) t_alias
2 options ;
1 - you can use "union all" for sum of the different selects
SELECT name, COUNT(name) AS count
FROM table
GROUP BY name
UNION ALL
SELECT 'SUM' name, COUNT(name)
FROM table
or
2-
SELECT
(select count(*) from table1 where selectedID = '123')
+
(select count(*) from table2 where selectedID = '123')
+
(select count(*) from table3 where selectedID = '123')
SELECT student, instructor, (SUM(student)+SUM(instructor)) AS totaluser
FROM tbl_users
You can write it in this way:
'''select student,instructor,(student+instructor) as sum from(SELECT
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'student') as student,
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'instructor') ) table'''
I can't find solution to correct this big query, I always receive an error from database.
I have tre tables and I need to make changes on some attribute on some condition:
UPDATE o36t_orders as temp1,
mytable as temp2
SET
temp1.bonifico = 1,
temp2.ultimo = 1
WHERE
temp1.id_order IN (
SELECT
id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
AND temp2.id IN (
SELECT
id
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
)
Since the subqueries of the 2 IN clauses are identical (except the retuned column), I think that you can do what you want by a straight inner join of the 2 tables and that subquery (returning both columns):
UPDATE o36t_orders temp1
INNER JOIN (
SELECT
id, id_order
FROM o36t_orders
LEFT JOIN o36t_address ON (o36t_address.id_address = o36t_orders.id_address_delivery)
LEFT JOIN mytable ON (
mytable.Causale = CONCAT(
o36t_address.lastname,
' ',
o36t_address.firstname
)
)
WHERE
o36t_orders.bonifico <> 1
) t ON t.id_order = temp1.id_order
INNER JOIN mytable temp2 ON temp2.id = t.id
SET
temp1.bonifico = 1,
temp2.ultimo = 1
This is my sql in SQL Server. How can I achieve this in MySQL?
SELECT COUNT(*) as total FROM (SELECT personal.*
CROSS APPLY
(
SELECT TOP 1 educ_attain.school
FROM educ_attain
WHERE personal.empno = educ_attain.empno
) educ
WHERE personal.status = 'ACTIVE'
) as num
My goal is to exclude employees who doesn't have a record in educ_attain. I have tried using sub-query but still returning employees with no record.
You can try any of these following-
SELECT *
FROM personal
INNER JOIN educ_attain
ON personal.empno = educ_attain.empno
WHERE personal.status = 'ACTIVE'
OR
SELECT *
FROM personal
WHERE empno IN
(
SELECT DISTINCT empno FROM educ_attain
)
AND status = 'ACTIVE'
To get count - Just use SELECT COUNT(*) for any of the above query.
Not Exists, employees do not have record in eductable then show
select * from
employee e
where not exists
(
select 1 from eductable t
on e.emp_id = t.emp_id
)
Hi I am trying to query a table that conatains multiple duplicates on Code,Amount and Status How will I do this if I only one to get a result group according to the client_group name and get the sum of amount under that group
SELECT `client`.`client_group`
, FORMAT(SUM(`Data_result`.`Data_result_amount` ),2) as sum
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
GROUP BY `client`.`client_group`
Result of said query:
Table
Try to distinct before run the 'sum' check whether this solve your problem
SELECT `client_group` , FORMAT(SUM(`Data_result_amount` ),2) as sum from (
SELECT DISTINCT `client`.`client_group` , `Data_result`.`Data_result_amount`
FROM
`qwer`.`Data_result`
INNER JOIN `qwer`.`Data`
ON (`Data_result`.`Data_result_lead` = `Data`.`Data_id`)
INNER JOIN `qwer`.`Data_status`
ON (`Data_result`.`Data_result_status_id` = `Data_status`.`Data_status_id`)
INNER JOIN `qwer`.`client`
ON (`Data`.`Data_client_id` = `client`.`client_id`)
WHERE `Data_status`.`Data_status_name` IN ('PAID') AND MONTH(`Data_result`.`result_ts`) = MONTH(CURRENT_DATE())
AND YEAR(`Data_result`.`result_ts`) = YEAR(CURRENT_DATE())
) T
GROUP BY `client_group`
you can check the query here http://sqlfiddle.com/#!9/36a3f8/6
I have the following query:
SELECT int_intrebari.id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id
Is it possible to update first table with nr_raspunsuri from the query, without writing a foreach statement?
You can UPDATE with JOIN like so:
UPDATE int_intrebari i1
INNER JOIN
(
SELECT id, COUNT( id_raspuns ) AS nr_raspunsuri
FROM int_intrebari
GROUP BY id
) i2 ON i1.id = i2.id
SET i1.nr_raspunsuri = i2.nr_raspunsuri
You can do it like -
update int_intrebari left join int_raspunsuri on int_intrebari.id =int_raspunsuri.id
set int_intrebari.column_to_update = int_raspunsuri.column_from_update_second_table
UPDATE
(SELECT int_intrebari.id, COUNT( id_raspuns) AS nr_raspunsuri
FROM int_intrebari, int_raspunsuri
WHERE int_intrebari.id = int_raspunsuri.id
GROUP BY id) t1,
int_raspunsuri t2
SET
t2.nr_raspunsuri=t1.nr_raspunsuri
WHERE
t1.id=t2.id