I have an update query like below which contains a join statement which is not a defined association. What would be the best way to convert this query to sequelize. I have googled but could not find a proper way to do this. I do not want to choose sequelize.literal or sequlize.query unless there is no alternative, and if there is not, then what would be the correct way to achieve this
UPDATE employees as comm left join (
SELECT
totalamount,
username, paid FROM payments as total where
paid=0 and group by username
) as tot on comm.username=tot.username
SET comm.paid = 1, comm.paidDate = NOW(), comm.paidBy = ?
where comm.username= ?
and comm.paid=0 and tot.totalamount = ?;
Related
I wrote below code but based on my reseacrh, there is no way to use Group by in Update.
My code:
update customer
set customer.cust_balance= sum(invoice.inv_amount)
where customer.cust_num=invoice.cust_num
group by invoice.cust_num
What I think I should do is something similar below but I am not sure if it is entirely true:
update customer
set customer.cust_balance= (select sum(invoice.inv_amount) from invoice
where customer.cust_num=invoice.cust_num
group by invoice.cust_num)
I believe above subquery will not do the correct job. Any advise or help would be appreciated
Your version would work, but it is better to write it as:
update customer
set cust_balance = (select sum(i.inv_amount)
from invoice i
where customer.cust_num = i.cust_num
);
In other words, the correlation clause is sufficient. You don't also need GROUP BY.
I need to write an update statement for a table of mine.
The table contains a technical key and a customer ID.
I need to update a field in this table by raising the field with +1, but only for the fields where the technical key is at the max value for that specific customer ID.
At first I tried to write this with how I usually do it on MSSQL:
UPDATE a
SET a.VERSION_CHECK = (a.VERSION + 1)
FROM customers a
WHERE a.technical_key = (SELECT MAX(b.technical_key)
FROM customers b
WHERE a.customer_id = b.customer_id
)
I've since been trying to work with a WHERE EXISTS clause in the syntax of the SQL used in Oracle, but I can't seem to figure out how to work the MAX technical key in there correctly, so that only those rows are updated in the table. Anyone got any input please?
You can use a use a correlated subquery:
UPDATE customer c
SET VERSION_CHECK = VERSION + 1
WHERE c.technical_key = (SELECT MAX(c2.technical_key)
FROM customers c2
WHERE c2.customer_id = c.customer_id);
I just hit a wall with my SQL query fetching data from my MS SQL Server.
To simplify, say i have one table for sales, and one table for customers. They each have a corresponding userId which i can use to join the tables.
I wish to first SELECT from the sales table where say price is equal to 10, and then join it on the userId, in order to get access to the name and address etc. from the customer table.
In which order should i structure the query? Do i need some sort of subquery or what do i do?
I have tried something like this
SELECT *
FROM Sales
WHERE price = 10
INNER JOIN Customers
ON Sales.userId = Customers.userId;
Needless to say this is very simplified and not my database schema, yet it explains my problem simply.
Any suggestions ? I am at a loss here.
A SELECT has a certain order of its components
In the simple form this is:
What do I select: column list
From where: table name and joined tables
Are there filters: WHERE
How to sort: ORDER BY
So: most likely it was enough to change your statement to
SELECT *
FROM Sales
INNER JOIN Customers ON Sales.userId = Customers.userId
WHERE price = 10;
The WHERE clause must follow the joins:
SELECT * FROM Sales
INNER JOIN Customers
ON Sales.userId = Customers.userId
WHERE price = 10
This is simply the way SQL syntax works. You seem to be trying to put the clauses in the order that you think they should be applied, but SQL is a declarative languages, not a procedural one - you are defining what you want to occur, not how it will be done.
You could also write the same thing like this:
SELECT * FROM (
SELECT * FROM Sales WHERE price = 10
) AS filteredSales
INNER JOIN Customers
ON filteredSales.userId = Customers.userId
This may seem like it indicates a different order for the operations to occur, but it is logically identical to the first query, and in either case, the database engine may determine to do the join and filtering operations in either order, as long as the result is identical.
Sounds fine to me, did you run the query and check?
SELECT s.*, c.*
FROM Sales s
INNER JOIN Customers c
ON s.userId = c.userId;
WHERE s.price = 10
SELECT Title
FROM movie
WHERE Movie_no = (SELECT Movie_no
FROM Customer
INNER JOIN issues ON customer.`Cus_id`=issues.`Cus_id`
WHERE NAME = 'Shyam')
No. You just need to use IN or ANY:
SELECT Title
FROM movie
WHERE Movie_no IN (SELECT Movie_no
FROM Customer INNER JOIN
issues
ON customer.Cus_id = issues.Cus_id
WHERE NAME = 'Shyam'
);
I mean, you might need to normalize your data for other reasons, but a subquery returning more than one value would not be such a reason. And, your data structure seems reasonable, based on this one query.
I don't see anything that really seems the same to me on here, though it might be just me having not done sql in so long.
So I have a master table TestTempGeneralInfo and a dail TestTaskGeneralInfo. I can use this query:
SELECT testtempgeneralinfo.issueid,
Max(testtaskgeneralinfo.effectivetime)
FROM testtaskgeneralinfo
INNER JOIN testtempgeneralinfo
ON testtaskgeneralinfo.testtemplateid =
testtempgeneralinfo.issueid
WHERE testtempgeneralinfo.projectid = 150
GROUP BY testtempgeneralinfo.issueid
To view the MAX of the TestTaskGeneralInfo tables effective time.
But I am struggling to write a query to update the same column TestTempGeneralInfo. Can anyone help? Hope this is not a dumb question. Thanks
There are a few different ways to do it, but the simplest for you may be to just take the query you have above and make it into a common table expression and then make your update statement join to the CTE:
;WITH MaxDate AS
( SELECT testtemplateid, Max(effectivetime) as MaxEffectiveTime
FROM testtaskgeneralinfo
GROUP BY testtemplateid)
UPDATE testtempgeneralinfo
SET effectivetime = MaxEffectiveTime
FROM MaxDate
INNER JOIN testtempgeneralinfo
ON MaxDate.testtemplateid = testtempgeneralinfo.issueid
WHERE testtempgeneralinfo.projectid = 150
EDIT: Sorry, had a bit of a copy/paste error there. But the above is assuming that you're wanting to update testtempgeneralinfo's effective time with the most recent testtaskgeneralinfo's effective time.
I'm not sure I understand your question. I understand it to be:
"Given the above query, how can I update the row that contains the maximum effectivetime with new values?"
Assuming issueid is your primary key, the answer would be something like this:
update testtempgeneralinfo
set
effectivetime = getdate() --columns and values will vary...
where issueid = (
SELECT top 1 testtempgeneralinfo.issueid
FROM
testtaskgeneralinfo
INNER JOIN
testtempgeneralinfo
ON testtaskgeneralinfo.testtemplateid = testtempgeneralinfo.issueid
WHERE testtempgeneralinfo.projectid = 150
order by
testtaskgeneralinfo.effectivetime desc
);