SQL Server: Update duplicates with data from first duplicate - sql-server

I get the following result:
**SID** **KL** **ABT** **KLH** **ABTH**
013239 PKB4 GT NULL NULL
013239 TM4A KD NULL NULL
013243 KSB4 GT NULL NULL
013243 TM4A KD NULL NULL
with this query:
SELECT DISTINCT
SID, KL, ABT, KLH, ABTH
FROM
[SGB]
WHERE
SID IN (SELECT SID FROM [SGB] GROUP BY SID HAVING COUNT(*) > 1)
I'd like to update KLH and ABTH with the content of KL and ABT from the first duplicate.
should look like this:
**SID** **KL** **ABT** **KLH** **ABTH**
013239 PKB4 GT PKB4 GT
013239 TM4A KD PKB4 GT
013243 KSB4 GT KSB4 GT
013243 TM4A KD KSB4 GT
Many thanks!

Here is query which update all duplicated SID with first value:
;WITH tempSGB AS (
SELECT SID
, KL
, ABT
, KLH
, ABTH
--Next column set numbers in order for rows
, ROW_NUMBER() OVER (PARTITION BY SID ORDER BY KL) AS ROWNUM
FROM SGB
WHERE SID IN (SELECT SID FROM SGB GROUP BY SID HAVING COUNT(*) > 1)
)
UPDATE s SET
s.KLH = tmp.KL
, s.ABTH = tmp.ABT
FROM tempSGB tmp
INNER JOIN SGB s ON s.SID = tmp.SID
WHERE ROWNUM = 1; --here we choose only first row from duplicated
About ROW_NUMBER()
But you did not mention how query can decide which row is first from all duplicated(in the example i set in order by KL)
Here SQL Fiddle is a sample query with data for testing

UPDATE B
SET KLH=A.KL,
ABTH=A.ABT
FROM [SGB] A JOIN [SGB] B ON A.SID=B.SID

select * into #temptable from [SGB]
update #temptable t
set **KLH** =**KL**
set **ABTH**=**ABT**
from [SGB] s
inner join s.**SID**=t.**SID**

Related

Clean up SQL Server script

SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
INTO pat_primary_provider
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM;
SELECT ppp.*
INTO ppp1
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM pat_primary_provider GROUP BY PAT_KEY) AS x
INNER JOIN pat_primary_provider AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;
I need to get the results of ppp1 only using one query (no INTO statements) in SQL Server. Please help.
Simply put the first query into a CTE (without the INTO clause). Then select from that.
;WITH pat_primary_provider AS
(
-- The first query goes here
)
-- The second query goes here
But something like below might also return the PAT_KEY's with the maximum PROV_NPI:
SELECT TOP 1 WITH TIES
PAT_KEY,
MAX(PROV_NPI) AS [Max_Provider_ID],
CONCAT(LAST_NM,' ',FIRST_NM) AS [Patient_Provider_Full_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP = 1
AND PAT_KEY IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY PAT_KEY, LAST_NM, FIRST_NM
ORDER BY row_number() over (order by MAX(PROV_NPI) desc);
Whats wrong with just inserting the first query as subqueries into the second?
SELECT ppp.*
FROM (SELECT PAT_KEY, MAX(provider_ID) AS maxprov FROM (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) GROUP BY PAT_KEY) AS x
INNER JOIN (SELECT pp.pat_key, MAX(pp.PROV_NPI) [Provider_ID], CONCAT(pp.LAST_NM,' ',pp.FIRST_NM) [Provider_Name]
FROM TRDW.dbo.PATIENT_PROVIDER pp
WHERE IS_PCP=1
AND pat_key IN (SELECT Consumer_ID FROM CareWire0521)
GROUP BY pp.PAT_KEY, pp.last_nm, pp.FIRST_NM) AS ppp ON ppp.PAT_KEY = x.PAT_KEY AND ppp.Provider_ID = x.maxprov;

Update records SQL?

First when I started this project seemed very simple. Two tables, field tbl1_USERMASTERID in Table 1 should be update from field tbl2_USERMASTERID Table 2. After I looked deeply in Table 2, there is no unique ID that I can use as a key to join these two tables. Only way to match the records from Table 1 and Table 2 is based on FIRST_NAME, LAST_NAME AND DOB. So I have to find records in Table 1 where:
tbl1_FIRST_NAME equals tbl2_FIRST_NAME
AND
tbl1_LAST_NAME equals tbl2_LAST_NAME
AND
tbl1_DOB equals tbl2_DOB
and then update USERMASTERID field. I was afraid that this can cause some duplicates and some users will end up with USERMASTERID that does not belong to them. So if I find more than one record based on first,last name and dob those records would not be updated. I would like just to skip and leave them blank. That way I wouldn't populate invalid USERMASTERID. I'm not sure what is the best way to approach this problem, should I use SQL or ColdFusion (my server side language)? Also how to detect more than one matching record?
Here is what I have so far:
UPDATE Table1 AS tbl1
LEFT OUTER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.fname = tbl2.fname
AND tbl1.lname = tbl2.lname
SET tbl1.usermasterid = tbl2.usermasterid
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
Here is query where I tried to detect duplicates:
SELECT DISTINCT
tbl1.FName,
tbl1.LName,
tbl1.dob,
COUNT(*) AS count
FROM Table1 AS tbl1
LEFT OUTER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.FName = tbl2.first
AND tbl1.LName = tbl2.last
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
AND LTRIM(RTRIM(tbl1.first)) <> ''
AND LTRIM(RTRIM(tbl1.last)) <> ''
AND LTRIM(RTRIM(tbl1.dob)) <> ''
GROUP BY tbl1.FName,tbl1.LName,tbl1.dob
Some data after I tested query above:
First Last DOB Count
John Cook 2008-07-11 2
Kate Witt 2013-06-05 1
Deb Ruis 2016-01-22 1
Mike Bennet 2007-01-15 1
Kristy Cruz 1997-10-20 1
Colin Jones 2011-10-13 1
Kevin Smith 2010-02-24 1
Corey Bruce 2008-04-11 1
Shawn Maiers 2016-08-28 1
Alenn Fitchner 1998-05-17 1
If anyone have idea how I can prevent/skip updating duplicate records or how to improve this query please let me know. Thank you.
You could check for and avoid duplicate matches using with common_table_expression (Transact-SQL)
along with row_number()., like so:
with cte as (
select
t.fname
, t.lname
, t.dob
, t.usermasterid
, NewUserMasterId = t2.usermasterid
, rn = row_number() over (partition by t.fname, t.lname, t.dob order by t2.usermasterid)
from table1 as t
inner join table2 as t2 on t.dob = t2.dob
and t.fname = t2.fname
and t.lname = t2.lname
and ltrim(rtrim(t.usermasterid)) = ''
)
--/* confirm these are the rows you want updated
select *
from cte as t
where t.NewUserMasterId != ''
and not exists (
select 1
from cte as i
where t.dob = i.dob
and t.fname = i.fname
and t.lname = i.lname
and i.rn>1
);
--*/
/* update those where only 1 usermasterid matches this record
update t
set t.usermasterid = t.NewUserMasterId
from cte as t
where t.NewUserMasterId != ''
and not exists (
select 1
from cte as i
where t.dob = i.dob
and t.fname = i.fname
and t.lname = i.lname
and i.rn>1
);
--*/
I use the cte to extract out the sub query for readability. Per the documentation, a common table expression (cte):
Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.
Using row_number() to assign a number for each row, starting at 1 for each partition of t.fname, t.lname, t.dob. Having those numbered allows us to check for the existence of duplicates with the not exists() clause with ... and i.rn>1
You could use a CTE to filter out the duplicates from Table1 before joining:
; with CTE as (select *
, count(ID) over (partition by LastName, FirstName, DoB) as IDs
from Table1)
update a
set a.ID = b.ID
from Table2 a
left join CTE b
on a.FirstName = b.FirstName
and a.LastName = b.LastName
and a.Dob = b.Dob
and b.IDs = 1
This will work provided there are no exact duplicates (same demographics and same ID) in table 1. If there are exact duplicates, they will also be excluded from the join, but you can filter them out before the CTE to avoid this.
Please try below SQL:
UPDATE Table1 AS tbl1
INNER JOIN Table2 AS tbl2
ON tbl1.dob = tbl2.dob
AND tbl1.fname = tbl2.fname
AND tbl1.lname = tbl2.lname
LEFT JOIN Table2 AS tbl3
ON tbl3.dob = tbl2.dob
AND tbl3.fname = tbl2.fname
AND tbl3.lname = tbl2.lname
AND tbl3.usermasterid <> tbl2.usermasterid
SET tbl1.usermasterid = tbl2.usermasterid
WHERE LTRIM(RTRIM(tbl1.usermasterid)) = ''
AND tbl3.usermasterid is null

SQL - add one more column as result of subquery? SQL Server 2008

I have a query which gives me perfectly good results:
select
A.ID_acc, A.ID_us, A.st, table3.KFL,
'100' as myattribute,
'101' as my attribute2
from
SOURCE1 as A
left join
(select
table2.ID_us, table2.ID_acc,
CASE WHEN table2.KFL_type = 'KFL' THEN P.index_num ELSE table2.KFL_type END as KFL
from
(select
table1.ID_us, table1.ID_acc,
CASE WHEN sum(table1.count_kfl) > 1 THEN '9999' WHEN sum(table1.count_kfl) = 1 THEN 'KFL' END as KFL_type
from
(SELECT
ID_us, ID_acc, count(*) as count_kfl
FROM
payments
WHERE
index_num IN (200, 201, 203)
AND (date >= XXXX or date2 >= 'XXXXX')
GROUP BY
1, 2) as table1
group by
1, 2) as table2
join
SOURCE2 as P on table2.ID_us = P.ID_us
and table2.ID_acc = P.ID_acc
where
(P.date>= XXXX or P.date2 >= 'XXXXX')
and index_num in (201,201,203)
group by
1, 2
order by
1, 2) as table3 on table3.ID_us = A.ID_us
and table3.ID_acc = A.ID_acc
where
A.not_deleted >= XXXXXX
This query is not my main question, so I only copied it just to short brief, but I wondering how I can now add one more additional column (result of count operation) as the end of my first query? Just to do not making 2 separately and then mixing results. Naturally I don't want to influence on my earlier fields results.
I have second query which looks like this:
select A.ID_us, count(*)/2 as number
from
SOURCE1 as A
left join
SOURCE3 as B
on A.ID_acc = B.ID_acc
where A.date >= XXXX
group by 1
The link between those 2 queries is attribute ID_acc in SOURCE A which appear in first and in second query.
But don't have idea how do it?
select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute, '101' as my attribute2, NEWSOURCE.MYNEW_attribute
from SOURCE1 as A
left join
(
...
)
as table3 on table3.ID_us = A.ID_us and table3.ID_acc = A.ID_acc
where A.not_deleted >= XXXXXX
left join
(
.
.
.
)
as NEWSOURCE
Something like this of course, don't work:///
Have you tried a correlated subquery:
select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute,
'101' as my attribute2,
( select count(*)/2 as number from SOURCE1 as IA left join SOURCE3 as IB on
IA.ID_acc = IB.ID_acc and IA.ID_Acc = A.ID_Acc where IA.date >= XXXX ) as NewColumn
from ...
Note the use of new aliases in the correlated subquery.

Need to extract only the one row using join query

I am trying to extract one record from joining the Table X and Table Y.
Below is the query am using
SELECT (select ACCOUNTSALESFORCEDETAILID
FROM sysdba.ACCOUNTSALESFORCEDETAIL AS a1
WHERE ORGSALESFORCEID = 'QWKHAA000STK'
AND ACCOUNTID = O.ACCOUNTID
and MODIFYDATE = (
SELECT MIN(MODIFYDATE) AS MODIFYDATE
FROM sysdba.ACCOUNTSALESFORCEDETAIL
WHERE ACCOUNTID = a1.ACCOUNTID
AND ORGSALESFORCEID = a1.ORGSALESFORCEID
GROUP BY ACCOUNTID, ORGSALESFORCEID
)
),
a.ORGSALESFORCEID
FROM sysdba.ACCOUNTSALESFORCEDETAIL a
JOIN sysdba.ACCOUNT O on a.ACCOUNTID=O.ACCOUNTID
WHERE O.ACCOUNTID ='A61E6C43EE71'
AND A.ORGSALESFORCEID in ('QWKHAA000STK','QWKHAA01KXNC','QWKHAA01KXNE')
Below s the result am getting now:
ACCOUNTSALESFORCEID ORGSALESFORCEID
QB46AC89800B QWKHAA000STK
QB46AC89800B QWKHAA01KXNC
QB46AC89800B QWKHAA01KXNE
But i need only First row as the output..
Please help.
Have a try like this
SELECT TOP(1) col1,ORGSALESFORCEID
FROM
(SELECT
(
SELECT
ACCOUNTSALESFORCEDETAILID
FROM
sysdba.ACCOUNTSALESFORCEDETAIL AS a1
WHERE (ORGSALESFORCEID = 'QWKHAA000STK')
AND (ACCOUNTID=O.ACCOUNTID)
AND (MODIFYDATE =( SELECT MIN(MODIFYDATE) AS MODIFYDATE
FROM sysdba.ACCOUNTSALESFORCEDETAIL
WHERE (ACCOUNTID = a1.ACCOUNTID) AND (ORGSALESFORCEID=a1.ORGSALESFORCEID)
GROUP BY ACCOUNTID, ORGSALESFORCEID)
)
) AS col1
,a.ORGSALESFORCEID
FROM
sysdba.ACCOUNTSALESFORCEDETAIL a JOIN sysdba.ACCOUNT O on a.ACCOUNTID=O.ACCOUNTID
WHERE O.ACCOUNTID='A61E6C43EE71' and A.ORGSALESFORCEID in ('QWKHAA000STK','QWKHAA01KXNC','QWKHAA01KXNE')
) AS temp

Sub query in Store Procedure

I have a query in sql stored procedure. I want to get record from other query from its id how I do that.
SELECT t.Name ,t.CreatedDate ,t.CreatedBy , t.Amount
,t.Margin ,t.Probability ,t.Id
FROM (SELECT a = 1) a
CROSS JOIN
(SELECT
Name = HirschInternational_MSCRM.dbo.SalesOrderBase.Name
,CreatedDate=HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedOn
,CreatedBy=HirschInternational_MSCRM.dbo.SystemUserBase.FullName
,Amount = totalamount
,Probability=CloseProbability
,Id=SalesOrderId
,Margin=(SELECT ( ISNULL( ((Sum(Price)-Sum(CurrentCost)) / NULLIF( Sum(Price), 0 ))*100, 0 ) )
FROM HirschInternational_MSCRM.dbo.ProductBase
JOIN HirschInternational_MSCRM.dbo.SalesOrderDetailBase
ON HirschInternational_MSCRM.dbo.SalesOrderDetailBase.ProductId = HirschInternational_MSCRM.dbo.ProductBase.ProductId
JOIN HirschInternational_MSCRM.dbo.SalesOrderBase
ON HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId = HirschInternational_MSCRM.dbo.SalesOrderDetailBase.SalesOrderId)
FROM HirschInternational_MSCRM.dbo.SalesOrderBase
JOIN HirschInternational_MSCRM.dbo.OpportunityBase
ON HirschInternational_MSCRM.dbo.SalesOrderBase.Opportunityid = HirschInternational_MSCRM.dbo.OpportunityBase.Opportunityid
JOIN HirschInternational_MSCRM.dbo.SystemUserBase
ON HirschInternational_MSCRM.dbo.SystemUserBase.SystemUserId = HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedBy
WHERE YEAR(HirschInternational_MSCRM.dbo.SalesOrderBase.CreatedOn)=YEAR(GETDATE())
I want Margin from every record I want Output like
It's not entirely clear what you want, but you might be looking for something like
select *
from (your SQL SELECT statement goes here) t1
where id = ?;
I want to get margin of every record how I filter margin query for SalesOrderId
like
Margin=(SELECT ( ISNULL( ((Sum(Price)-Sum(CurrentCost)) / NULLIF( Sum(Price), 0 ))*100, 0 ) )
FROM HirschInternational_MSCRM.dbo.ProductBase
JOIN HirschInternational_MSCRM.dbo.SalesOrderDetailBase
ON HirschInternational_MSCRM.dbo.SalesOrderDetailBase.ProductId = HirschInternational_MSCRM.dbo.ProductBase.ProductId
JOIN HirschInternational_MSCRM.dbo.SalesOrderBase
ON HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId = HirschInternational_MSCRM.dbo.SalesOrderDetailBase.SalesOrderId
Where HirschInternational_MSCRM.dbo.SalesOrderBase.SalesOrderId= //SalesOrderId that I get in main query)
how I pass that SalesOrderId in this query

Resources