SQL Server SQL Statement - Updating record - sql

I have a data as below:
I need to update Matching_id and Matching_Type by using column id, region, company, dept, subdept and amountsepend. The logic is:
Sum AmountSepend by Region, Company, Dept and SubDept. If the sum amount is 0 then Matching_Type is 'Match' and Matching_id is the combination of the id for the matched record else 'Not Match' and Matching_id is the id. **SUM means the total sum of all records for same criteria regardless the AmountSepend is positive or negative.
Another important criteria is if the transaction is single record, meaning the total count by grouping by Region, Company, Dept and SubDept is 1 then Matching type is Not Match and Matching_UID is id regardless the AmountSepend is 0 or positive/negative value. Example id 8.
Below is the output:
Here the table and data script
CREATE TABLE [dbo].[StackoverflowQuest](
[id] [int] NOT NULL,
[Region] [varchar](50) NULL,
[Company] [varchar](50) NULL,
[Dept] [varchar](50) NULL,
[SubDept] [varchar](50) NULL,
[AmountSepend] [float] NULL,
[Matching_id] [varchar](100) NULL,
[Matching_Type] [varchar](100) NULL
) ON [PRIMARY]
How could I achieved such result ? Any help/hint would be appreciate

CREATE TABLE #Table(Id INT,Region VARCHAR(100),Company INT,Dept INT,SubDept
INT,AmtSpend INT,MatchingId VARCHAR(100),MatchingType VARCHAR(100))
INSERT INTO #Table(Id ,Region ,Company , Dept ,SubDept ,AmtSpend )
SELECT 1,'NAM',12378,1,NULL,900 UNION ALL
SELECT 2,'NAM',12378,1,NULL,-900 UNION ALL
SELECT 3,'NAM',12370,1,23,1000 UNION ALL
SELECT 4,'ASA',1234,9,12,5000 UNION ALL
SELECT 5,'NAM',12370,1,23,-1000 UNION ALL
SELECT 6,'ASA',1234,9,12,800 UNION ALL
SELECT 7,'ASA',1234,9,12,-600 UNION ALL
SELECT 8,'ASA',12311,6,NULL,200
UPDATE #Table SET MatchingId = MatchIds,MatchingType = 'Match'
FROM
(
SELECT T2.Company,STUFF( ( SELECT ',' + CAST(T3.Id AS VARCHAR) FROM #Table
T3 WHERE T2.Company = T3.Company FOR XML PATH('')),1,1,'') MatchIds
FROM #Table T2
JOIN
(
SELECT T1.Company Company,SUM(T1.AmtSpend) Total
FROM #Table T1
GROUP BY T1.Company
HAVING SUM(T1.AmtSpend) = 0
)A ON A.Company = T2.Company
GROUP BY T2.Company
) A
WHERE A.Company = #Table.Company
UPDATE #Table SET MatchingId = CAST(Id AS VARCHAR),MatchingType = 'Not
Match' WHERE ISNULL(MatchingId,'') = ''
SELECT * FROM #Table

Related

Looping through groups of records

SQL Server 2014, I have a table with a number of rows for example 15, 5 have a groupid column of 736881 and 10 have a group id column 3084235. What I want to do is process each group of records in turn and load the results in to a table.
I have written the code to do this but I think I am not setting the loopcounter incorrectly set as I keep getting the groupid of records 736881 loaded twice.
I cant't currently post the test data due to containing personal information but if the mistake is not obvious I will try and create some dummy data.
SELECT #LoopCounter = min(rowfilter) , #maxrowfilter = max(rowfilter)
FROM peops6
WHILE ( #LoopCounter IS NOT NULL
AND #LoopCounter <= #maxrowfilter)
begin
declare #customer_dist as Table (
[id] [int] NOT NULL,
[First_Name] [varchar](50) NULL,
[Last_Name] [varchar](50) NULL,
[DoB] [date] NULL,
[post_code] [varchar](50) NULL,
[mobile] [varchar](50) NULL,
[Email] [varchar](100) NULL );
INSERT INTO #customer_dist (id, First_Name, Last_Name, DoB, post_code, mobile, Email)
select id, first_name, last_name, dob, postcode, mobile_phone, email from peops6 where rowfilter = #LoopCounter
insert into results
SELECT result.* ,
[dbo].GetPercentageOfTwoStringMatching(result.DoB, d.DoB) [DOB%match] ,
[dbo].GetPercentageOfTwoStringMatching(result.post_code, d.post_code) [post_code%match] ,
[dbo].GetPercentageOfTwoStringMatching(result.mobile, d.mobile) [mobile%match] ,
[dbo].GetPercentageOfTwoStringMatching(result.Email, d.Email) [email%match]
FROM ( SELECT ( SELECT MIN(id)
FROM #customer_dist AS sq
WHERE sq.First_Name = cd.First_Name
AND sq.Last_Name = cd.Last_Name
AND ( sq.DoB = cd.DoB
OR sq.mobile = cd.mobile
OR sq.Email = cd.Email
OR sq.post_code = cd.post_code )) nid ,
*
FROM #customer_dist AS cd ) AS result
INNER JOIN #customer_dist d ON result.nid = d.id order by 1, 2 asc;
SELECT #LoopCounter = min(rowfilter) FROM peops6
WHERE rowfilter > #LoopCounter
end
You need to truncate your table variable (#customer_dist) at the end of the loop:
....
-- Add this
TRUNCATE TABLE #customer_dist
SELECT #LoopCounter = min(rowfilter) FROM peops6
WHERE rowfilter > #LoopCounter
end
See: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/42ef20dc-7ad8-44f7-b676-a4596fc0d593/declaring-a-table-variable-inside-a-loop-does-not-delete-the-previous-data?forum=transactsql
I am not sure you need a LOOP like using a SQL Cursor to fulfill this task
Please check following SQL statement where I used multiple CTE expressions
with customer_dist as (
select
rowfilter,
id, first_name, last_name, dob, postcode, mobile_phone, email
from peops6
), result as (
SELECT
(
SELECT
MIN(id)
FROM customer_dist AS sq
WHERE
sq.rowfilter = cd.rowfilter
AND sq.First_Name = cd.First_Name
AND sq.Last_Name = cd.Last_Name
AND (sq.DoB = cd.DoB OR sq.mobile_phone = cd.mobile_phone OR sq.Email = cd.Email OR sq.postcode = cd.postcode )
) nid,
*
FROM customer_dist AS cd
)
SELECT
result.* ,
[dbo].edit_distance(result.DoB, d.DoB) [DOB%match] ,
[dbo].edit_distance(result.postcode, d.postcode) [post_code%match] ,
[dbo].edit_distance(result.mobile_phone, d.mobile_phone) [mobile%match] ,
[dbo].edit_distance(result.Email, d.Email) [email%match]
FROM result
INNER JOIN customer_dist d
ON result.nid = d.id
order by 1, 2 asc;
Please note, I used my fuzzy string matching Levenshtein Distance Algorithm in this sample instead of your function
And the outcome is as follows
Only you need to add the INSERT statement just before the last SELECT statement
Hope it is useful

Create Count On Table That References Itself

I have a table that looks like below:
The table lists countries and regions (states, provinces, counties, etc) within those countries. I need to generate a count of all the regions within all countries. As you can see, each region has a ParentID which is the ID of the country in which you can find the region.
As an example, California is in USA, so its parent ID is 1 (which is the ID of USA).
So, the results from the simple table above should be:
USA: 2 and
Canada: 1
I have tried the following:
Select all values into a table which have ID a 1 (for USA)
Select all values into a table which have ID a 3 (for Canada)
Select all values into the USA table with Parent ID as 1
Select all values into the Canada table with Parent ID as 3
Do counts on both tables
The problem with the above approach is that if a new country is added, a count will not be automatically generated.
Any ideas on making this more dynamic?
You have to join the table with itself:
select t1.ID, t1.segment, count(distinct t2.ID)
from yourTable t1
join yourTable t2
on t1.ID = t2.parentID
where t1.parentID is null
group by t1.ID, t1.segment
The where clause ensures you that only "top level" rows will be displayed.
Perhaps it makes sense to re-format the data, incase there are other sorts of queries that you want to make in addition to a count of countries and regions.
CREATE TABLE #CountriesRegions
(
[ID] [int] NOT NULL,
parentid [int] NULL,
segment [nvarchar](50) NULL)
insert into #CountriesRegions values (1,null,'usa'), (2,1, 'california'), (3, null, 'canada'), (4, 3, 'quebec'), (5, 1, 'NY')
select * from #CountriesRegions
Create table #Country
([ID] [int] NOT NULL
,[country_name] [nvarchar](50) NOT NULL)
Insert into #Country select ID, segment AS country_name from #CountriesRegions where parentid IS NULL
select * from #Country
Create table #Region
([ID] [int] NOT NULL
,[country_id] [int] NOT NULL
,[region_name] [nvarchar](50) NOT NULL)
Insert into #Region select ID, parentid AS country_ID, segment AS region_name from #CountriesRegions where parentid IS NOT NULL
select * from #Region
Select COUNT(*) As 'Num of Countries' from #Country
Select COUNT(*) As 'Num of Regions' from #Region
CREATE TABLE CountriesRegions
(
[ID] [int] NOT NULL,
parentid [int] NULL,
segment [nvarchar](50) NULL)
insert into CountriesRegions values (1,null,'usa'), (2,1, 'california'), (3, null, 'canada'), (4, 3, 'quebec'), (5, 1, 'NY')
select a.id, a.segment, count(*) as [Region Count]
from CountriesRegions a
left join CountriesRegions b
on a.id=b.parentid
where b.id is not null
group by a.id, a.segment

Why does my insert for new records only does not work even if I'm using where not exits to a subquery?

This is the script I used to create the table.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[ODSCustomerBase]') AND type in (N'U'))
DROP TABLE [ODSCustomerBase]
Go
Create Table ODSCustomerBase
(CustomerBaseID int NOT NULL identity primary key,
RecordSource nvarchar(4),
RecordType varchar(2),
SiteURN nvarchar(128)NOT NULL,
SiteDesc nvarchar(60)NULL,
CustomerLink nvarchar(120)NOT NULL,
HomeCurrencyCode nvarchar(8)NOT NULL,
CustomerID nvarchar(15)NOT NULL,
CustomerCurrencyCode nvarchar(8)NOT NULL,
CustomerName nvarchar(120)NULL,
CustomerShortName nvarchar(30) NOT NULL,
Address nvarchar(255)NULL,
City nvarchar(25)NULL,
PostalCode nvarchar(10) NULL,
CountryCode nvarchar(3) NOT NULL,
CountryName nvarchar(60) NOT NULL,
StateCode nvarchar(8) NULL,
StateName nvarchar(60) NULL,
Phone nvarchar(30) NOT NULL,
Fax nvarchar(30) NULL,
TaxCode nvarchar(15) NULL,
ProspectID nvarchar(15) NULL,
CreateDate datetime NOT NULL,
LastUpdateDate datetime NULL)
This is the script I used to insert records for the first time
insert into SEC_ODS.dbo.ODSCustomerBase
select
--Identity as CustomerBaseID
'E4SE' as Recordsource, --could be others, eg 'BST', CRM, nvarchar(4)'
'C' as RecordType, --could be 'p'as prospects, add a case statement
ss.siteURN,
ss.sitedesc,
ss.FS_URL + 'frmcustomer.aspx?CustomerID=' + customerid as CustomerLink,
co.HomeCurrencyCode,
c.customerid,
c.currencycode as customercurrencycode,
c.entityname as CustomerName,
c.entityshortname CustomerShortName,
c.address,
c.city,
c.postalcode,
c.countrycode,
cn.countryname,
c.statecode,
s.statename,
c.phone,
c.fax,
c.taxcode,
c.prospectid,
c.createdate,
c.lastupdatedate
from country cn,
SECSite ss,
company co,
customer c
left outer join state s
on c.statecode = s.statecode
where ss.LocalSiteFlag = 1
and cn.countrycode = c.countrycode
So if I check the ODS table using this script, it gives me 4395 results
use sec_ods
go
select count(*) from ODSCustomerBase
While this is the script that I use to insert NEW RECORDS ONLY. This is where I used the where not exist condition and the sub query which doesn't work because it doubles the number of records. The goal is to insert only new records.
insert into SEC_ODS.dbo.ODSCustomerBase
select
--Identity as CustomerBaseID
'E4SE' as Recordsource, --could be others, eg 'BST', CRM, nvarchar(4)'
'C' as RecordType, --could be 'p'as prospects, add a case statement
ss.siteURN,
ss.sitedesc,
ss.FS_URL + 'frmcustomer.aspx?CustomerID=' + customerid as CustomerLink,
co.HomeCurrencyCode,
c.customerid,
c.currencycode,
c.entityname,
c.entityshortname,
c.address,
c.city,
c.postalcode,
c.countrycode,
cn.countryname,
c.statecode,
s.statename,
c.phone,
c.fax,
c.taxcode,
c.prospectid,
c.createdate,
c.lastupdatedate
from country cn,
SECSite ss,
company co,
customer c
left outer join state s
on c.statecode = s.statecode
where ss.LocalSiteFlag = 1
and cn.countrycode = c.countrycode
and not exists(select * from SEC_ODS.dbo.ODSCustomerBase b
where(Recordsource=b.Recordsource and
RecordType=b.RecordType and
ss.siteURN=b.siteURN and
ss.sitedesc=b.sitedesc and
ss.FS_URL=b.CustomerLink and
co.HomeCurrencyCode=b.HomeCurrencyCode and
c.customerid=b.customerid and
c.currencycode=b.CustomerCurrencyCode and
c.entityname=b.CustomerName and
c.entityshortname=b.CustomerShortName and
c.address=b.address and
c.city=b.city and
c.postalcode=b.postalcode and
c.countrycode=b.countrycode and
cn.countryname=b.countryname and
c.statecode=b.statecode and
s.statename=b.statename and
c.phone=b.phone and
c.fax=b.fax and
c.taxcode=b.taxcode and
c.prospectid=b.prospectid and
c.createdate=b.createdate and
c.lastupdatedate=b.lastupdatedate))
So if I check the ODS table using this script, it gives me 8790 results which is wrong.
use sec_ods
go
select count(*) from ODSCustomerBase
Can someone help me with this please?
Thank you.
Use either ISNULL or COALESCE around all the fields that could be NULL.
e.g. ISNULL(c.address,'') = ISNULL(b.address,'')
If you want to restrict the already inserted data better to go Merge Statment it will just update the existing data or insert the new data
MERGE ODSCustomerBase AS T
USING (
SELECT Col1,col2,.... FROM ODSCustomerBase
) AS S
ON (
t.Col1 = s.Col1
AND t.Col2 = s.Col2
AND .....
)
WHEN NOT MATCHED BY TARGET
THEN
INSERT (
Col1
,Col2
,....
,....
)
VALUES (
S.Col1
--,S.NrID
,s.Col2
,s.Col3
)
WHEN MATCHED
THEN
UPDATE
SET T.Col1 = S.Col1
WHEN NOT MATCHED BY SOURCE
AND EXISTS (
SELECT 1
FROM ODSCustomerBase c
WHERE t.Col1 = Col1
)
THEN
DELETE;

how to insert multiple rows with check for duplicate rows in a short way

I am trying to insert multiple records (~250) in a table (say MyTable) and would like to insert a new row only if it does not exist already.
I am using SQL Server 2008 R2 and got help from other threads like SQL conditional insert if row doesn't already exist.
While I am able to achieve that with following stripped script, I would like to know if there is a better (short) way to do this as I
have to repeat this checking for every row inserted. Since we need to execute this script only once during DB deployment, I am not too much
worried about performance.
INSERT INTO MyTable([Description], [CreatedDate], [CreatedBy], [ModifiedDate], [ModifiedBy], [IsActive], [IsDeleted])
SELECT N'ababab', GETDATE(), 1, NULL, NULL, 1, 0
WHERE NOT EXISTS(SELECT * FROM MyTable WITH (ROWLOCK, HOLDLOCK, UPDLOCK)
WHERE
([InstanceId] IS NULL OR [InstanceId] = 1)
AND [ChannelPartnerId] IS NULL
AND [CreatedBy] = 1)
UNION ALL
SELECT N'xyz', 1, GETDATE(), 1, NULL, NULL, 1, 0
WHERE NOT EXISTS(SELECT * FROM [dbo].[TemplateQualifierCategoryMyTest] WITH (ROWLOCK, HOLDLOCK, UPDLOCK)
WHERE
([InstanceId] IS NULL OR [InstanceId] = 1)
AND [ChannelPartnerId] IS NULL
AND [CreatedBy] = 1)
-- More SELECT statements goes here
You could create a temporary table with your descriptions, then insert them all into the MyTable with a select that will check for rows in the temporary table that is not yet present in your destination, (this trick in implemented by the LEFT OUTER JOIN in conjunction with the IS NULL for the MyTable.Description part in the WHERE-Clause):
DECLARE #Descriptions TABLE ([Description] VARCHAR(200) NOT NULL )
INSERT INTO #Descriptions ( Description )VALUES ( 'ababab' )
INSERT INTO #Descriptions ( Description )VALUES ( 'xyz' )
INSERT INTO dbo.MyTable
( Description ,
CreatedDate ,
CreatedBy ,
ModifiedDate ,
ModifiedBy ,
IsActive ,
IsDeleted
)
SELECT d.Description, GETDATE(), 1, NULL, NULL, 1, 0
FROM #Descriptions d
LEFT OUTER JOIN dbo.MyTable mt ON d.Description = mt.Description
WHERE mt.Description IS NULL

Pick each column value on different criteria

I have a SQL Query as below:
SELECT urban_appr, urban_in, rural_appr, rural_in, ground_appr, ground_in
FROM <table>
In the above query, I have to add WHERE clause as follows:
For urban values, WHERE scheme_type = 'U'
For rural values, WHERE scheme_type = 'R'
For ground values, WHERE scheme_type = 'E'
I want all this using single SQL query. I am using SQL Server 2005.
Edited:
Added image below of the actual problem. You can see that it is showing district BR as two records, whereas I want it in one row.
Use JOIN to bring all rows for each district_nm into one row:
select
district_nm,
t1.urban_appr, t1.urban_in,
t2.rural_appr, t2.rural_in,
t3.ground_appr, t3.ground_in
from <table> t1
join <table> t2 on t2.district_nm = t1.district_nm and t2.scheme_type = 'R'
join <table> t3 on t3.district_nm = t1.district_nm and t3.scheme_type = 'E'
where t1.scheme_type = 'U';
You can add more conditions to the where clause as you like, eg and t1.district_nm = 'x'
Consider creating a view from the above query and use that in our app instead:
create view table_view as
<above query>
Are you trying to do something like this - selecting different columns depending on the type of your values??
SELECT urban_appr, urban_in, NULL, NULL, NULL, NULL
FROM dbo.YourTable WHERE scheme_type = 'U'
UNION
SELECT NULL, NULL, rural_appr, rural_in, NULL, NULL
FROM dbo.YourTable WHERE scheme_type = 'R'
UNION
SELECT NULL, NULL, NULL, NULL, ground_appr, ground_in
FROM dbo.YourTable WHERE scheme_type = 'E'
???
Update: so you want to add another column and group by it:
SELECT district_nm, urban_appr, urban_in, NULL, NULL, NULL, NULL
FROM dbo.YourTable WHERE scheme_type = 'U'
UNION
SELECT district_nm, NULL, NULL, rural_appr, rural_in, NULL, NULL
FROM dbo.YourTable WHERE scheme_type = 'R'
UNION
SELECT district_nm, NULL, NULL, NULL, NULL, ground_appr, ground_in
FROM dbo.YourTable WHERE scheme_type = 'E'
GROUP BY district_nm
Is that what you're looking for??
Well, this looks ugly - but try this...
SELECT ISNULL(district_nm,nm) district_nm,
URBAN_APPR,
URBAN_IN,
RURAL_APPR,
RURAL_IN,
ground_appr,
ground_in
FROM
(SELECT ISNULL(a.district_nm, b.district_nm) nm,
urban_appr,
urban_in,
rural_appr,
rural_in
FROM
(SELECT DISTRICT_NM,
URBAN_APPR,
URBAN_IN
FROM TABLE_NAME
WHERE SCHEME_TYPE = 'U'
) a
FULL OUTER JOIN (
(SELECT DISTRICT_NM,
RURAL_APPR,
RURAL_IN
FROM TABLE_NAME
WHERE SCHEME_TYPE = 'R'
)B)
ON a.district_nm = b.district_nm
) temp
FULL OUTER JOIN (
(SELECT DISTRICT_NM,
GROUND_APPR,
GROUND_IN
FROM TABLE_NAME
WHERE SCHEME_TYPE = 'E'
)c)
ON temp.nm = c.district_nm;