I'm trying to run a query like this:
WITH cte AS
(
SELECT id, category, SUM(amount) AS sum_amount FROM t1 GROUP BY id, category
)
UPDATE table SET amount = cte.sum_amount WHERE id = cte.id;
However, I keep getting the error
Unknown column 'cte.id in WHERE clause'
Does anyone know how I can refer to my common table expression in the UPDATE query, or otherwise, rewrite it?
You can try below
WITH cte AS
(
SELECT id, SUM(amount) AS sum_amount FROM t1 GROUP BY category
)
UPDATE T
SET T.sum_amount= CT.sum_amount
FROM table T
JOIN cte CT
ON T.id = CT.id
Alternate way with Temporary table, you can read about CTE and Temporary Table
Temporary table:
SELECT id, category, SUM(amount) AS sum_amount
INTO #temp
FROM t1 GROUP BY id, category
Update query with temp table:
UPDATE OT
SET OT.sum_amount= TT.sum_amount
FROM table OT
JOIN #temp TT
ON OT.id = TT.id
CTE:
WITH cte AS
(
SELECT id, category, SUM(amount) AS sum_amount FROM t1 GROUP BY id,category
)
UPDATE T
SET T.sum_amount= CT.sum_amount
FROM table T
JOIN cte CT
ON T.id = CT.id
Related
I am trying to join two tables and get the count and grouped by specific field. However, it outputs same count values even if the other table consist only two rows. How should I fix this?
Here's my code:
SELECT tbl1.preferredDay, COUNT(tbl1.preferredDay) as count_1, COUNT(tbl2.preferredDay) as count_2
FROM tblschedule as tbl1
LEFT JOIN tblappointments as tbl2 ON (tbl1.preferredDay = tbl2.preferredDay)
WHERE tbl1.preferredDay = tbl2.preferredDay
GROUP BY preferredDay;
Here is the output but it should be [15, 0][3, 3]
Your query is based on left join it will return the same count().
This is a working query for Mysql 8:
with tbl1 as (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
),
tbl2 as (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
)
select t1.preferredDay, t1.count_1, t2.count_2
from tbl1 t1
inner join tbl2 t2 on t1.preferredDay = t2.preferredDay
There are two WITHs to get separately the count and then an INNER JOIN to join those results
For Mysql 5.7 and lower :
select t1.preferredDay, t1.count_1, t2.count_2
from (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
) as t1
inner join (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
) as t2 on t1.preferredDay = t2.preferredDay
Hi i am new to subqueries and so not sure if i am doing right
I am happy with this
SELECT t2.cons, t1.date, t1.account_no FROM
(
SELECT date, account_no FROM tbl_consignment_x3
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
It gives me the results i want to use
So i was hoping something like this below would do what i want but i can't get the syntax right plus i'm not sure if my technique is completely wrong
UPDATE tbl_margin_all t3
(
SELECT t2.cons, t1.date, t1.account_no FROM
(
SELECT date, account_no FROM tbl_consignment_x3
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
)
SET t3.cons=t2.cons
WHERE t1.date=t3.date AND t1.account_no=t3.account_no
thanks
ADDITION:
First i do a total count of consignments for a particular date taken from consignment table that is then written in volume table with the date and account.. then from consignments table i want to write to each consignment that count that i did.
You need to join with the first subquery -- put JOIN after t3.
Actually, you don't need any of the subqueries at all, you can just join directly with the tables.
UPDATE tbl_margin_all AS t3
JOIN tbl_consignment_x3 AS t1 ON t1.date = t3.date AND t1.account_no=t3.account_no
JOIN tbl_volume_analysis AS t2 ON t2.account_no=t1.account_no AND t2.date=t1.date
SET t3.cons = t2.cons
It's the same in the SELECT query, it should be:
SELECT t2.cons, t1.date, t1.account_no
FROM tbl_consignment_x3 AS t1
JOIN tbl_volume_analysis AS t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
Notice the comma after t3 for syntax
UPDATE tbl_margin_all_4000 t3,
(
SELECT cons, t1.date, t1.account_no FROM
(
SELECT date, account_no, consignment_no FROM tbl_consignment_x3_4000
) t1
INNER JOIN
(
SELECT cons, date, account_no FROM
tbl_volume_analysis_4000
) t2
ON t2.account_no=t1.account_no AND t2.date=t1.date
) as src
SET t3.cons=src.cons
WHERE t3.date=src.date AND t3.account_no=src.account_no
this query took 4 mins. i had tried the double joins as suggested kindly but that took 45 mins.
I have a select statement that returns a set of ids and a maxdate. Now, I want to update the performance_date in the invoices table with the given maxdate where the given invoices ids.
Select invoices.id, max(invoicepositions.performance_date) as maxdate
from invoices
inner join invoicepositions on invoices.id = invoicepositions.invoice_id
where invoices.performance_date IS NULL
group by invoices.id
(How) is this possible with MySQL?
You can use your current SELECT query as a Derived Table and Join it to the invoices table using id, and then update.
UPDATE invoices AS i
JOIN
(
Select invoices.id, max(invoicepositions.performance_date) as maxdate
from invoices
inner join invoicepositions on invoices.id = invoicepositions.invoice_id
where invoices.performance_date IS NULL
group by invoices.id
) AS dt
ON dt.id = i.id
SET i.performance_date = dt.maxdate
select *
from
(
SELECT id, imei1, status
FROM `owarranty_imei` mto
WHERE EXISTS
(
SELECT 1
FROM `owarranty_imei` mti
WHERE mto.imei1=mti.imei1
LIMIT 1, 1
)
) t1
left join `owarranty_warranty_activations` as t2 on t1.id=t2.imei_id
where t2.id is null
limit 100
this is my query. In owarranty_imei has more than 100000 records. i want to get duplicates from imei table which owarranty_imei not in owarranty_warranty_activation table. This query work for few records but when i run it for more than 1000000 records its not working
SELECT
mto.id,
mto.imei1,
mto.status
FROM
`owarranty_imei` mto
INNER JOIN
`owarranty_imei` mti ON mto.imei1=mti.imei1
LEFT JOIN
`owarranty_warranty_activations` as t2 ON mto.id=t2.imei_id
GROUP BY mto.id
HAVING COUNT(t2.id)=0
I am in this dilemma: here is a table with records.
The last column has supervisors and it refers to the first column.
How can I select all employees who are supervisors and display for each, first name and last name?
Here is the table
You can use EXISTS to do this(preferred approach)
SELECT *
FROM yourtable t1
WHERE EXISTS (SELECT 1
FROM yourtable t2
WHERE t1.EMPLOYEEID = t2.EMPSUPERVISOR)
using IN operator
SELECT *
FROM yourtable t1
WHERE t1.EMPLOYEEID IN (SELECT t2.EMPSUPERVISOR
FROM yourtable t2)
using JOIN
SELECT t1.*
FROM yourtable t1
JOIN (SELECT DISTINCT EMPSUPERVISOR
FROM yourtable) t2
ON t1.EMPLOYEEID = t2.EMPSUPERVISOR
I would say:
SELECT EMPLNAME , EMPLFNAME
FROM THETABLE
WHERE EMPLOEEID IN (SELECT DISTINCT EMPSUPERVISOR
FROM THETABLE
)
;