I have Table Job in which i have a column name MainJob_Id name
So i just want to Update Container_TypeId Column Value as 1,2,3...go on...
when MainJob_id is same...
else MainJob_id is new then start again as 1
Execpted Result
Try using an updatable CTE:
WITH cte AS (
SELECT ContainerTypeId,
ROW_NUMBER() OVER (PARTITION BY MainJob_Id ORDER BY LengthId DESC) rn
FROM yourTable
)
UPDATE cte
SET ContainerTypeId = rn;
However, you might want to not do this update and instead just select the sequence you want at the time you query.
Related
(select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,ROW_NUMBER() OVER(PARTITION BY EXTERNAL_TRANSACTION_ID ORDER BY ID ) AS SEQNUM
from AC_POS_TRANSACTION_TRK aptt WHERE [RESULT] ='Success'
GROUP BY ID, EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE )
Hello,
On above query, I want to get rows of transaction id's which has seqnum=1 and seqnum=2
But if that transaction id has no second row (seqnum=2), I dont want to get any row for that transaction id.
Thanks!!
Something like this
Not 100% sure if this is correct without you table definition, but my understanding is that you want to EXCLUDE records if that record has an entry with seqnum=2 -- you can't use a where clause alone because that would still return seqnum = 1.
You can use an exists /not exists or in/not in clause like this
(select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,ROW_NUMBER() OVER(PARTITION BY EXTERNAL_TRANSACTION_ID ORDER BY ID ) AS SEQNUM
from AC_POS_TRANSACTION_TRK aptt WHERE [RESULT] ='Success'
and not exists ( select 1 from AC_POS_TRANSACTION_TRK a where a.id = aptt.id
and a.seqnum = 2)
GROUP BY ID, EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE )
basically what this does is it excludes records if a record exists as specified in the NOT EXISTS query.
One option you can try is to add a count of rows per group using the same partioning critera and then filter accordingly. Not entirely sure about your query without seeing it in context and with sample data - there's no aggregation so why use group by?
However can you try something along these lines
select * from (
select ID,EXTERNAL_TRANSACTION_ID,EXTERNAL_TRANSACTION_TYPE,
Row_Number() over(partition by EXTERNAL_TRANSACTION_ID order by ID) as SEQNUM,
Count(*) over(partition by EXTERNAL_TRANSACTION_ID) Qty
from AC_POS_TRANSACTION_TRK
where [RESULT] ='Success'
)x
where SEQNUM in (1,2) and Qty>1
This should do the job.
With Qry As (
-- Your original query goes here
),
Select Qry.*
From Qry
Where Exists (
Select *
From Qry Qry1
Where Qry1.EXTERNAL_TRANSACTION_ID = Qry.EXTERNAL_TRANSACTION_ID
And Qry1.SEQNUM = 1
)
And Exists (
Select *
From Qry Qry2
Where Qry2.EXTERNAL_TRANSACTION_ID = Qry.EXTERNAL_TRANSACTION_ID
And Qry2.SEQNUM = 2
)
BTW, your original query looks problematic to me, specifically I think that instead of a GROUP BY columns those columns should be in the PARTITION BY clause of the OVER statement, but without knowing more about the table structures and what you're trying to achieve, I could not say for sure.
I need to update the staging table based on the type if ZMD2 is present then update the records else update PNTP records.
UPDATE ITEMS_STAGING SET TYPE=b.TYPE,VALUE=b.VALUE
FROM ITEMS_STAGING a,ITEMS b
WHERE a.PARENT=b.PARENT
In the above statement I need to pick only ZMD2 records for the same parent if exists if not PNTP records. I tried to do UNION for the ITEMS it dint help.
Staging table Output:
Kindly help.
Thanks
You need to use analytical function row_number which will group the rows by parent column to give them numbers and then we will take only one record from each group to update staging table using merge statement as following:
MERGE INTO ITEM_STAGING M
USING (
SELECT T.*,
ROW_NUMBER() OVER(PARTITION BY T.PARENT ORDER BY T.TYPE DESC) RN
FROM ITEMS T
)
ON (M.PARENT = T.PARENT AND T.RN = 1)
WHEN MATCHED THEN
UPDATE SET M.TYPE = T.TYPE AND M.VALUE = T.VALUE;
Cheers!!
You may try below query -
SELECT *
FROM (SELECT IS.*, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY TYPE DESC) RN
FROM ITEMS_STAGING)
WHERE RN = 1;
I am not sure what you want to update in this table.
SQL Server 2008 R2
Table CIRTB
CIRLogID PrimaryKey
StaffName
StaffID
Table StaffTB
StaffID - PrimaryKey
Fullname
DOB
CIN
Program
I have StaffID's in the CIRTB table that are Null, so I need to update it from the Staff table.
I know I need something like the following
update CIRTB
set CIRTB>staffid = (select staffid
from stafftb
where stafftb.fullname = cirtb.Staffname)
where CIRTB.staffid is null
One problem that I am stuck on: there are some duplicates in the StaffTB for fullname, dob, program
One way to do the UPDATE is using a CTE:
;WITH ToUpdate AS (
SELECT t1.StaffID AS t1_StaffID, t2.StaffID AS t2_StaffID,
ROW_NUMBER() OVER (PARTITION BY t2.Fullname
ORDER BY t2.DOB DESC) AS rn
FROM CIRTB AS t1
JOIN StaffTB AS t2 ON t1.StaffName = t2.Fullname
WHERE t1.StaffID IS NULL
)
UPDATE ToUpdate
SET t1_StaffID = t2_StaffID
WHERE rn = 1
The CTE uses ROW_NUMBER in order to pick one record per Fullname. The ORDER BY clause of the window function determines which record is picked in case of duplicates: since DOB DESC is used, the record having the most recent DOB is selected.
Use TOP 1 in the sub-select to make sure it only returns one row even if there are duplicates.
I've table in Redshift with duplicated row that i want to delete,
for that I created filed Id and i want to update him to delete duplicated rows
I'm trying to run this query but it doesn't work
update mr_usage
set id=row_number () over (partition by uid,date(ts),title order by ts)
I received the following error:
ERROR: cannot use window function in UPDATE
I'm looking for a way to update that field
The other possible solution (without CTE) is using UPDATE .. FROM syntax with subquery directly
UPDATE mr_usage outer
SET id = sub.new_id
FROM (
SELECT
id, ROW_NUMBER() OVER (PARTITION BY uid, date(ts), title ORDER BY ts) AS new_id
FROM
mr_usage
) sub
WHERE outer.id = sub.id
But it is also available since PostgreSQL 8.4.
You can try a CTE to achieve this, although such an UPDATE won't remove any duplicate rows.
WITH n AS (
SELECT
id AS current_id,
ROW_NUMBER() OVER (PARTITION BY uid, date(ts), title ORDER BY ts) AS new_id
FROM
mr_usage
)
UPDATE
mr_usage
SET
id = n.new_id
FROM
n
WHERE
mr_usage.id = n.current_id;
I'm currently working with my database in SQL Server. I have a table with 23 fields and it has single and duplicate rows. How can I select both of them without having any duplicate data.
I have try this query:
SELECT
Code, Stuff, and other fields....
FROM
(
SELECT
*,ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Code) AS RN
FROM
my_table
)t
WHERE RN = 1
The above code just return the data from the duplicate rows. But, I want the "single rows" also returned.
This is the illustration.
Thank you for the help.
Could it be as simple as:
SELECT DISTINCT Code, Stuff FROM MyTable
Or, just add stuff to the partition by clause:
PARTITION BY Code,Stuff ORDER BY Code
Try This
You may need to add Stuff and more fields in Partition BY
SELECT
Code, Stuff
FROM
(
SELECT
*,ROW_NUMBER() OVER (PARTITION BY Code,Stuff ORDER BY Code) AS RN
FROM
my_table
)t
WHERE RN = 1