I am trying to modify a table of my taking the id of another table being that this other table is with another column, taking only if the data of that column is 2, take the ID of that column and use in the one that I want to change, example:
UPDATE QuestData SET RepeatFinish = 100000
WHERE QuestID =
(
SELECT * FROM Quest WHERE QuestID = 2
);
But QuestData have so much data and table Quest too, how can i make this?
UPDATE QuestData SET RepeatFinish = 100000
WHERE QuestID in
(
SELECT id FROM Quest WHERE QuestID = 2
);
Change the id in select by yours. when using the in with an other request the select must return a one field that will be used in IN, you are using * so we don't know with what we should compare the QuestID
Related
I have this situation: deceduti(sigla,denominazione_provincia) , province(sigla_provincia,denominazione_provincia)
where sigla is a NUMBER and sigla_provincia is a CHAR(2).
I want to set deceduti.sigla=province.sigla_provincia where deceduti.denominazione_provincia=province.denominazione_provincia.
I have tried different ways but no one worked. I'd appreciate to understand how can i do this with join operator. There are my tries:
MERGE INTO DECEDUTI -- error with the datatypes
USING PROVINCE
ON ( DECEDUTI.DENOMINAZIONE_PROVINCIA = PROVINCE.DENOMINAZIONE_PROVINCIA )
WHEN MATCHED THEN
UPDATE SET DECEDUTI.Sigla = PROVINCE.sigla_provincia;
/
UPDATE -- oracle doesnt have the on update+policy react
(SELECT DECEDUTI.Sigla as OLD, to_number(PROVINCE.Sigla_provincia) as NEW
FROM DECEDUTI
INNER JOIN PROVINCE
ON DECEDUTI.DENOMINAZIONE_PROVINCIA = PROVINCE. DENOMINAZIONE_PROVINCIA
) t
SET t.OLD = t.NEW
/
UPDATE deceduti T
SET T.sigla =
(SELECT distinct sigla_provincia
FROM PROVINCE A
WHERE A.denominazione_provincia = T.denominazione_provincia);
This is what you have:
create table deceduti
(sigla number,
denominazione_provincia unknown_datatype
);
create table province
(sigla_provincia char(2),
denominazione_provincia unknown_datatype
);
Data stored within looks like this (according to datatypes you mentioned):
DECEDUTI:
sigla denominazione_provincia
----- -----------------------
25 1000
null 2000
PROVINCE:
sigla_provincia denominazione_provincia
--------------- -----------------------
AB 1000
X1 2000
As tables are joined on denominazione_provincia, you'd like to - for its value = 1000 - put AB into the deceduti.sigla column. How do you plan to put AB into a NUMBER datatype column? That won't work.
The simplest option is to
alter table deceduti modify sigla (char(2));
but only if sigla column is empty; shouldn't be problem to set it to NULL as it is supposed to accept a new value anyway. If you want to keep some "old" values, then it gets more complicated (creating a new column, store current sigla value in there, empty sigla, change its datatype, move data back, drop new column).
Other options are more complicated, e.g.
make sure that all sigla_provincia values are numeric so that to_number(sigla_provincia) returns a number
update deceduti.sigla values only for denominazione_provincias whose pair in province contains number in sigla_provincia
I have a 2 databases that I want to merge with some similiar tables. The source tables have id as bigint and my destination table has int as ID. There aren't that many records in my source table (< 20k) so I want to assign new ids to all records so the ids can fit in an int. How can I do this with sql?
First Option
You can Use Sequence object as follow:
First Create a Sequence object and assign it's Start With value to max Id value in destination table plus 1. For example if max Id in destination table is 100, you need to assign 101 as Start With. You can also obtain the max Id value from destination table using a Max(Id) aggregate function and store it in a variable:
CREATE SEQUENCE SeqId
START WITH [Max value of Id in destination table]
INCREMENT BY 1 ;
GO
Then insert to destination table using following query:
Insert Into tblXXX (Id, ...) Values (NEXT VALUE FOR SeqId, ...)
Read more about Sequence Object
Second Option
You can make the destination table's Id column as Identity column with seed equal to destination table's Id column max value and Increment equal to 1.
Here is detailed example also Here
You did not provide much details so I can only provide a general guideline:
Note: Example assumes that you want to merge tables A and B into C and you want to generate new IDs. I also assume that these IDs are not referenced by other tables (foreign keys).
First you get record counts from tables A and B:
DECLARE #countA INT
DECLARE #countB INT
SET #countA = ( SELECT COUNT(*) FROM A )
SET #countB = ( SELECT COUNT(*) FROM B )
Next you use a window function to generate new IDs and insert records into table C.
INSERT INTO C
SELECT #countA + ROW_NUMBER() OVER( ORDER BY ID ) AS ID, ....
FROM A
INSERT INTO C
SELECT #countA + #countB + ROW_NUMBER() OVER( ORDER BY ID ) AS ID, ....
FROM B
It is a little bit tricky for me so I need your help :)
I want to update the column Relevant to 0 WHERE Contract_Status_Code is 10 OR the Date_Contract_start YEAR is the same AND the Ranking_Value is lower than the other one ON all records that have the same VIN.
So I want to compare all records which have the same VIN.
Few examples to illustrate it:
I have there two records with the VIN = 123456. One of them (ID = 6847) has a higher Ranking_Value (7) than the other one (3). The YEAR is the same as well so I want to update the column relevant to 0 where the ID is 8105.
Two records with the VIN = 654321. Both of them have the same Ranking_Value but the record with the id = 11012 has the value 10 for the column Contract_Status_Code so I want to update the relevant column to 0 where the ID = 11012.
The last two records... They have the VIN = 171819. The first one (ID = 11578) has the higher Ranking_Value. But they have a different year where the contract has started. So I don't want to update both.
It is also possible that there are three or four records with the same VIN.
I hope you understand my problem. I'm from Germany so sorry for my English :)
By considering your ID column as unique or Identity column, I can suggest you the below query for your solution:
With cte
As
(Select a.Id, a.VIN From Table a
Join (Select max(Ranking_Value) ranks,VIN From Table Group By VIN, Year(Date_Contract_start)) b
on a.VIN=b.VIN And a.Ranking_Value = b.ranks)
update table
set Relevant = 0
where (Contract_Status_Code = 10) Or
ID Not In (Select id from cte)
update table1
set Relevant = 0
where Contract_Status_Code = 10
or (VIN,Year,Ranking_value) not in(
select VIN,Year,max(Ranking_Value)
from table1
group by VIN,Year
)
2 records in above image are from Db, in above table Constraint are (SID and LINE_ITEM_ID),
SID and LINE_ITEM_ID both column are used to find a unique record.
My issues :
I am looking for a query it should fetch the recored from DB depending on conditions
if i search for PART_NUMBER = 'PAU43-IMB-P6'
1. it should fetch one record from DB if search for PART_NUMBER = 'PAU43-IMB-P6', no mater to which SID that item belong to if there is only one recored either under SID =1 or SID = 2.
2. it should fetch one record which is under SID = 2 only, from DB on search for PART_NUMBER = 'PAU43-IMB-P6', if there are 2 items one in SID=1 and other in SID=2.
i am looking for a query which will search for a given part_number depending on Both SID 1 and 2, and it should return value under SID =2 and it can return value under SID=1 only if the there are no records under SID=2 (query has to withstand a load of Million record search).
Thank you
Select *
from Table
where SID||LINE_ITEM_ID = (
select Max(SID)||Max(LINE_ITEM_ID)
from table
where PART_NUMBER = 'PAU43-IMB-P6'
);
If I understand correctly, for each considered LINE_ITEM_ID you want to return only the one with the largest value for SID. This is a common requirement and, as with most things in SQL, can be written in many different ways; the best performing will depend on many factors, not least of which is the SQL product you are using.
Here's one possible approach:
SELECT DISTINCT * -- use a column list
FROM YourTable AS T1
INNER JOIN (
SELECT T2.LINE_ITEM_ID,
MAX(T2.SID) AS max_SID
FROM YourTable AS T2
GROUP
BY T2.LINE_ITEM_ID
) AS DT1 (LINE_ITEM_ID, max_SID)
ON T1.LINE_ITEM_ID = DT1.LINE_ITEM_ID
AND T1.SID = DT1.max_SID;
That said, I don't recall seeing one that relies on the UNION relational operator. You could easily rewrite the above using the INTERSECT relational operator but it would be more verbose.
Well in my case it worked something like this:
select LINE_ITEM_ID,SID,price_1,part_number from (
(select LINE_ITEM_ID,SID,price_1,part_number from Table where SID = 2)
UNION
(select LINE_ITEM_ID,SID,price_1,part_number from Table SID = 1 and line_item_id NOT IN (select LINE_ITEM_ID,SID,price_1,part_number from Table SID = 2)))
This query solved my issue..........
My requirement like this:
I have to fetch a row from table "A". Now I have to loop through all the rows and get the values from the ID column. If I get ID = 5 then I want to execute some stored procedure through which I get the previous id row from some table "B". Like
select * from table b where ID = 4
Buy my question is: if someone deleted that row from the database (e.g. some user deleted the rows with id 3 and 4) - then how can I get the row with id = 2 from the database?
Please give me some sql stored procedure to get previous id record from table.
Hard to understand exactly what it is your looking for - something like this maybe??
CREATE PROCEDURE dbo.FetchPreviousRow #ID INT
AS BEGIN
SELECT TOP 1 ID, (other columns.....)
FROM dbo.TableB
WHERE ID < #ID
ORDER BY ID DESC
END
This will select the row with the biggest ID less than the #ID you pass in, so:
EXEC dbo.FetchPreviousRow #ID = 5
will return the row with ID = 4 - if it exists. Otherwise, it will return the row with ID = 3 - if it exists. Otherwise the row with ID = 2 - and so on....
The following query should get it for you:
select max(ID) from YourTable where ID < #YourID
#YourID is a variable containing your recently inserted ID
How about selecting all ID's less than 5 and sorting them by ID. The first result then would be the one you want.