How to use aggregate in the set list of an UPDATE statement - sql

Let me describe the whole situation: I have below two tables. The values of MaxTestCasesExecutedBySingleUser and TotalTestCasesExecutedByAllUser in Module table needs to be updated using some Aggregate clause
CREATE TABLE [dbo].[Module](
[ProjectID] [int] NOT NULL,
[ModuleID] [int] NOT NULL,
[ModuleName] [nvarchar](100) NOT NULL,
[MaxTestCasesExecutedBySingleUser] [int] NULL,
[TotalTestCasesExecutedByAllUser] [int] NULL,
PRIMARY KEY ([ProjectID],[ModuleID]))
CREATE TABLE [dbo].[ModuleMember](
[ProjectID] [int] NOT NULL,
[ModuleID] [int] NOT NULL,
[SerialNo] [int] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
PRIMARY KEY ([ProjectID],[ModuleID],[SerialNo]))
Insert Into Module Values (1,1,'Installation_Test',null,null)
Insert Into Module Values (1,2,'Server_Test',null,null)
Insert Into Module Values (1,3,'Client_Test',null,null)
Insert Into Module Values (1,4,'Security_Test',null,null)
Insert Into ModuleMember Values(1,1,0,'Jim')
Insert Into ModuleMember Values(1,1,1,'Bob')
Insert Into ModuleMember Values(1,2,0,'Jack')
Insert Into ModuleMember Values(1,2,1,'Steve')
Insert Into ModuleMember Values(1,2,2,'Roy')
Insert Into ModuleMember Values(1,2,3,'Jerry')
Insert Into ModuleMember Values(1,3,0,'Root')
Insert Into ModuleMember Values(1,3,1,'Tom')
Insert Into ModuleMember Values(1,4,0,'Evil')
There is another Table Valued Function dbo.GetValue which takes Name (from ModuleMember table) as parameter and returns the values of Name and TestCasesExecutedByTheUser in the form of a table. I need to update these two values in Module table.
Definition:
MaxTestCasesExecutedBySingleUser = maximum number if test case executed by a member in a module
TotalTestCasesExecutedByAllUser = total number of test cases executed by all member in a module.
I tried below query but it is throwing me error : An aggregate may not appear in the set list of an UPDATE statement.
UPDATE M
SET
M.MaxTestCasesExecutedBySingleUser = MAX(N.TestCasesExecutedByTheUser),
M.TotalTestCasesExecutedByAllUser = SUM(N.TestCasesExecutedByTheUser)
FROM Module M
JOIN ModuleMember Mem ON (M.ProjectID = Mem.ProjectID AND M.ModuleID = Mem.ModuleID)
CROSS APPLY dbo.GetValue(Mem.Name) N

The workaround is a little more verbose - you have to get the aggregates first (e.g. in a CTE), and then update the target table:
;WITH src AS
(
SELECT
Mem.ProjectID,
Mem.ModuleID,
mtc = MAX(N.TestCasesExecutedByTheUser),
stc = SUM(N.TestCasesExecutedByTheUser)
FROM dbo.ModuleMember AS Mem
CROSS APPLY dbo.GetValue(Mem.Name) AS N
GROUP BY Mem.ProjectID, Mem.ModuleID
)
UPDATE M
SET M.MaxTestCasesExecutedBySingleUser = src.mtc,
M.TotalTestCasesExecutedBySingleUser = src.stc
FROM dbo.Module AS M
INNER JOIN src
ON M.ProjectID = src.ProjectID
AND M.ModuleID = src.ModuleID;

I think it's perfect case to use apply
update Module set
MaxTestCasesExecutedBySingleUser = C.maxtc,
TotalTestCasesExecutedByAllUser = C.sumtc
from Module as M
cross apply (
select
max(N.TestCasesExecutedByTheUser) as maxtc,
sum(N.TestCasesExecutedByTheUser) as sumtc
from ModuleMember as MM
cross apply dbo.GetValue(MM.Name) as N
where MM.ProjectID = M.ProjectID and MM.ModuleID = M.ModuleID
) as C

Related

Stored procedure to Insert data between tables

I want to insert data from a table called temp_menu into another called menu.
They have the same structure, they store the same data, I want to create a stored procedure to check the differences between the tables. If there are any different rows and the rows don't exist in table menu, I want to insert them into menu; if the rows exists, I want to update the rows in menu if the DateReg column is higher that the DateReg column in the temp_menu table.
The tables have this structure:
CREATE TABLE [dbo].[Menu_Temp]
(
[Date] [datetime] NOT NULL,
[Ref] [int] NOT NULL,
[Art] [char](60) NOT NULL,
[Dish] [char](60) NOT NULL,
[DateReg] [datetime] NOT NULL,
[Zone] [char](60) NOT NULL,
);
I have this code to check for differences between the tables:
SELECT *
INTO #diffs
FROM [Regi].dbo.menu
EXCEPT
SELECT * FROM [Regi].dbo.menu_Temp
IF ##ROWCOUNT = 0
RETURN
SELECT * FROM #diffs
Full details are here : https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql
An example for your situation could be...
MERGE
[Regi].dbo.menu
USING
[Regi].dbo.menu_Temp
ON (menu_Temp.[Ref] = menu.[Ref]) -- Assumes [Ref] is the identifying column?
WHEN
MATCHED AND (menu_Temp.[DateReg] > menu.[DateReg])
THEN
UPDATE SET [Art] = menu_Temp.[Art],
[Dish] = menu_Temp.[Dish],
[Zone] = menu_Temp.[Zone],
[Date] = menu_Temp.[Date],
[DateReg] = menu_Temp.[DateReg]
WHEN
NOT MATCHED
THEN
INSERT (
[Date],
[Ref],
[Art],
[Dish],
[DateReg],
[Zone]
)
VALUES (
menu_Temp.[Date],
menu_Temp.[Ref],
menu_Temp.[Art],
menu_Temp.[Dish],
menu_Temp.[DateReg],
menu_Temp.[Zone]
)
http://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=b47d7e879856ffe6210589f6bb64829f

Multi Parent-Child Insertion

I'm trying to write a stored procedure to pull information from an XML string and use it to create multiple parent-child relationships. I am trying to push this XML into actual database tables. Basically, the local client will send an XML file to the database and store it as a string. I then need to pull the information out of that string and update the appropriate tables. If this was just a Table-A to Table-B, this wouldn't be so difficult. The problem I'm running into is it need to go from Table-A to Table-B to Table-C to Table-D where applicable. Below is a sample XML:
<RunRecordFile>
<Competition>
<Name>Daily</Name>
<StartDate>11/9/2015 12:40:07 AM</StartDate>
<Runs>
<Id>123</Id>
<Name>Daily Run</Name>
<RunDate>11/9/2015 12:40:07 AM</RunDate>
<CompetitionId>1</CompetitionId>
<RunRecords>
<Id>001</Id>
<Number>007</Number>
<ElapsedTime>23.007</ElapsedTime>
<RunId>123</RunId>
</RunRecords>
</Runs>
<Runs>
<Id>456</Id>
<Name>Daily Run</Name>
<RunDate>11/9/2015 12:47:07 AM</RunDate>
<CompetitionId>1</CompetitionId>
<RunRecords>
<Id>002</Id>
<Number>700</Number>
<ElapsedTime>23.707</ElapsedTime>
<RunId>456</RunId>
<RunRecordSpecialty>
<Id>1</Id>
<Handicap>17</Handicap>
<TeamPoints>50000</TeamPoints>
<RunRecordId>002</RunRecordId>
</RunRecordSpecialty>
</RunRecords>
</Runs>
</Competition>
</RunRecordFile>
I've attempted to use a DECLARED table to hold each of the created Primary Keys and to use SQL OUTPUT in order to gather those. When I run my SQL I'm getting (0) Rows Updated. Here's what I've tried in SQL:
CREATE PROC [dbo].[RaceFilePush]
AS
DECLARE #CompetitionIdMapping TABLE ( CompetitionId bigint )
DECLARE #RunIdMapping TABLE ( RunId bigint )
DECLARE #RunRecordIdMapping TABLE ( RunRecordId bigint )
BEGIN
DECLARE #rrXML AS XML
DECLARE #rrfId AS BIGINT
SET #rrfId = (SELECT TOP 1 Id FROM RunRecordFile WHERE Submitted IS NULL)
SET #rrXML = (SELECT TOP 1 RaceFile FROM RunRecordFile WHERE Id = #rrfId)
BEGIN TRAN Competitions
BEGIN TRY
INSERT INTO Competition (
Name
,StartDate
)
OUTPUT INSERTED.Id INTO #CompetitionIdMapping(CompetitionId)
SELECT
xCompetition.value('(Name)[1]', 'varchar(225)') AS Name
,xCompetition.value('(StartDate)[1]', 'datetime') AS StartDate
,#rrfId AS RunRecordFileId
FROM
#rrXML.nodes('/RunRecordFile/Competition') AS E(xCompetition)
INSERT INTO Run (
Name
,RunDate
,CompetitionId
)
OUTPUT INSERTED.Id INTO #RunIdMapping(RunId)
SELECT
xRuns.value('(Name)[1]','varchar(80)') AS Name
,xRuns.value('(RunDate)[1]','datetime') AS RunDate
,(SELECT CompetitionId FROM #CompetitionIdMapping)
FROM
#rrXML.nodes('/RunRecordFile/Competition/Runs') AS E(xRuns)
INSERT INTO RunRecord (
Number
,ElapsedTime
,RunId
)
OUTPUT INSERTED.Id INTO #RunRecordIdMapping(RunRecordId)
SELECT
xRunRecords.value('(Number)[1]','varchar(10)') AS Number
,xRunRecords.value('(ElapsedTime)[1]','numeric(10,5)') AS ElapsedTime
,(SELECT RunId FROM #RunIdMapping)
FROM
#rrXML.nodes('/RunRecordFile/Competition/Runs/RunRecords') AS E(xRunRecords)
INSERT INTO RunRecordSpecialty (
Handicap
,TeamPoints
,RunRecordId
)
SELECT
xRunRecordSpecialty.value('(Handicap)[1]','numeric(10,5)') AS Handicap
,xRunRecordSpecialty.value('(TeamPoints)[1]','numeric(10,5)') AS TeamPoints
,(SELECT RunRecordId FROM #RunRecordIdMapping)
FROM
#rrXML.nodes('/RunRecordFile/Competition/Runs/RunRecordSpecialty') AS E(xRunRecordSpecialty)
UPDATE RunRecordFile SET Submitted = GETDATE() WHERE Id = #rrfId
COMMIT TRAN Competitions
END TRY
BEGIN CATCH
ROLLBACK TRAN Competitions
END CATCH
END
With this SQL you get the whole thing into a flat declared table #tbl:
Remark: I placed the XML from your question into a variable called #xml. Adapt this to your needs...
DECLARE #tbl TABLE (
[Competition_Name] [varchar](max) NULL,
[Competition_StartDate] [datetime] NULL,
[Run_Id] [int] NULL,
[Run_Name] [varchar](max) NULL,
[Run_RunDate] [datetime] NULL,
[Run_CompetitionId] [int] NULL,
[RunRecords_Id] [int] NULL,
[RunRecords_Number] [int] NULL,
[RunRecords_ElapsedTime] [float] NULL,
[RunRecords_RunId] [int] NULL,
[RunRecordSpecialty_Id] [int] NULL,
[RunRecordSpecialty_Handicap] [int] NULL,
[RunRecordSpecialty_TeamPoints] [int] NULL,
[RunRecordSpecialty_RunRecordId] [int] NULL
);
INSERT INTO #tbl
SELECT Competition.value('Name[1]','varchar(max)') AS Competition_Name
,Competition.value('StartDate[1]','datetime') AS Competition_StartDate
,Run.value('Id[1]','int') AS Run_Id
,Run.value('Name[1]','varchar(max)') AS Run_Name
,Run.value('RunDate[1]','datetime') AS Run_RunDate
,Run.value('CompetitionId[1]','int') AS Run_CompetitionId
,RunRecords.value('Id[1]','int') AS RunRecords_Id
,RunRecords.value('Number[1]','int') AS RunRecords_Number
,RunRecords.value('ElapsedTime[1]','float') AS RunRecords_ElapsedTime
,RunRecords.value('RunId[1]','int') AS RunRecords_RunId
,RunRecordSpecialty.value('Id[1]','int') AS RunRecordSpecialty_Id
,RunRecordSpecialty.value('Handicap[1]','int') AS RunRecordSpecialty_Handicap
,RunRecordSpecialty.value('TeamPoints[1]','int') AS RunRecordSpecialty_TeamPoints
,RunRecordSpecialty.value('RunRecordId[1]','int') AS RunRecordSpecialty_RunRecordId
FROM #xml.nodes('/RunRecordFile/Competition') AS A(Competition)
OUTER APPLY Competition.nodes('Runs') AS B(Run)
OUTER APPLY Run.nodes('RunRecords') AS C(RunRecords)
OUTER APPLY RunRecords.nodes('RunRecordSpecialty') AS D(RunRecordSpecialty)
;
SELECT * FROM #tbl
If you need generated IDs, you just add the columns to #tbl and write them there, either "on the flow" or afterwards wiht UPDATE statement.
It should be easy to work through this flat table, select just the needed data level with DISTINCT and insert the rows, then the next level and so on...
Good luck!

Clone records with descendants in sql server

Below is my table.
CREATE TABLE [dbo].[CCMaster](
[CCId] [int] IDENTITY(1,1) NOT NULL,
[GId] [int] NULL,
[BKId] [int] NULL,
[Class] [varchar](100) NULL,
[ParentId] [int] NULL,)
SET IDENTITY_INSERT CCMaster ON
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(1,33,162,'CORPORATE',NULL)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(10,33,162,'Call center related',4)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(11,33,162,'Channel related',2)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(12,33,162,'Advertisement',6)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(13,33,162,'Brand Ambassador',6)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(14,33,162,'OTAF/TVP (New Activation)',11)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(15,33,162,'Service Barred',7)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(16,33,162,'Call center behaviour',10)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(17,33,162,'Store Personnel behavior',8)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(2,33,162,'DTH',NULL)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(3,33,162,'2G',NULL)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(4,33,162,'3GS',NULL)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(5,33,162,'Broadband',NULL)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(6,33,162,'Brand',1)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(7,33,162,'Barring-related',3)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(8,33,162,'Behaviour related',4)
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(9,33,162,'Call center related',3)
SET IDENTITY_INSERT CCMaster OFF
There are thousands of records in this table. I want to clone only the records where GID = 33 and BKID = 162. The parent of the child must match accordingly to the auto generated id of the respective parent. I tried this query but it din't work for me.
I tried using a cursor by first inserting the parents and then trying to query for the child but that too din't work out. Any help would be appreciated.
Use a recursive CTE to get the rows you want to insert.
To figure out what new value is to be used as ParentId you can use merge with output to get a mapping between the old CCId and the new CCId. Capture the output from the merge to table variable and after the merge you can update ParentId from the table variable.
declare #T table
(
OldCCId int,
OldParentId int,
NewCCId int
);
with C as
(
select M.*
from dbo.CCMaster as M
where ParentId is null and
GId = 33 and
BKId = 162
union all
select M.*
from dbo.CCMaster as M
inner join C
on C.CCId = M.ParentId
)
merge into dbo.CCMaster as M
using C
on 1 = 0
when not matched then
insert(GID, BKID, Class)
values (C.GID, C.BKID, C.Class)
output C.CCId,
C.ParentId,
inserted.CCId
into #T;
update M
set ParentId = T2.NewCCId
from CCMaster as M
inner join #T as T1
on M.CCId = T1.NewCCId
inner join #T as T2
on T1.OldParentId = T2.OldCCId;
SQL Fiddle

INSERT or UPDATE table from another table with composite primary key

I am looking for the correct syntax and way to do the following directly from SQL: insert or update (if data already exists inside) TableMain from data contained in TableA with both having same composite primary key.
Both tables are defined as :
CREATE TABLE TableA (
[TID0] [int] NOT NULL,
[TID1] [int] NOT NULL,
[language] [nvarchar](2) NOT NULL,
[TID2] [nvarchar](200) NOT NULL,
[text] [nvarchar](max) NULL,
[updatedOn] [datetime] NOT NULL DEFAULT (getdate())
PRIMARY KEY (
[TID0],
[TID1],
[language],
[TID2],
)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
TableA will be periodically deleted and filled.
TableMain as the same definition but will contain many more rows of data and what I need is to insert never seen values from TableA into TableMain, and update already existing rows.
I used to do this kind of insert but I do not know how to handle update and composite primary keys :
INSERT INTO TableMain
SELECT * FROM TableA
EDIT : i am using SQL Server 9.00.5000
EDIT : another way inspired by MERGE and mimick it
DECLARE #updatedIDs TABLE(
[TID0] [int],
[TID1] [int],
[language] [nvarchar](2),
[TID2] [nvarchar](200),
PRIMARY KEY ([TID0], [TID1], [language], [TID2]) -- as stated by Nikola Markovinović above, thanks
);
-- First update records
update TableMain
set [text] = source.[text],
[updatedOn] = source.[updatedOn]
OUTPUT
inserted.[TID0]
inserted.[TID1]
inserted.[language]
inserted.[TID2]
INTO #updatedIDs
from
TableMain AS main
, TableA AS source
WHERE
TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
-- And then insert
insert into TableMain
select *
from TableA AS source
where not exists
(
select 1
from #updatedIDs AS i
where i.[TID0] = source.[TID0]
and i.[TID1] = source.[TID1]
and i.[language] = source.[language]
and i.[TID2] = source.[TID2]
)
you should use a merge statment
something like this:
merge TableMain AS target
using TableA as source
ON <join tables here>
WHEN MATCHED THEN <update>
WHEN NOT MATCHED BY TARGET <Insert>
WHEN NOT MATCHED BY SOURCE <delete>
Here is a script you might use to upsert your data:
-- On error transaction is automatically rolled back
set xact_abort on
begin transaction
-- First update records
update TableMain
set [text] = source.[text],
[updatedOn] = source.[updatedOn]
from TableMain
inner join TableA source
on TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
-- And then insert
insert into TableMain ([TID0], [TID1], [language], [TID2], [text], [updatedOn])
select [TID0], [TID1], [language], [TID2], [text], [updatedOn]
from TableA source
where not exists
(
select *
from TableMain
where TableMain.[TID0] = source.[TID0]
and TableMain.[TID1] = source.[TID1]
and TableMain.[language] = source.[language]
and TableMain.[TID2] = source.[TID2]
)
commit transaction
You might rewrite not exists() as left join ... where TableMain.TID0 is null if performance is not satisfactory.
you can use the Merge command from SQLServer 2008. It allows you to merge data from another data source into your main data source and define specific behavior when there is a key match (and you'll probably like to update your table) or there is not match and you'll like to insert the new record.
http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/
you can visit this link to get few code samples.

SQL Spatial JOIN By Nearest Neighbor

In the data below I am looking for a query so I can join the results of the 2 tables by nearest neighbor.
Some of the results in dbo.Interests Table will not be in the dbo.Details Table,
This Question finds the k nearest poins for a single point, I need this query to further reconcile data between the 2 tables
How can I extend this SQL query to find the k nearest neighbors?
IF OBJECT_ID('dbo.Interests', 'U') IS NOT NULL DROP TABLE dbo.Interests;
IF OBJECT_ID('dbo.Details', 'U') IS NOT NULL DROP TABLE dbo.Details;
CREATE TABLE [dbo].[Interests](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [nvarchar](255) NULL,
[geo] [geography] NULL,
)
CREATE TABLE [dbo].[Details](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [nvarchar](255) NULL,
[geo] [geography] NULL,
)
/*** Sample Data ***/
/* Interests */
INSERT INTO dbo.Interests (Name,geo) VALUES ('Balto the Sled Dog',geography::STGeomFromText('POINT(-73.97101284538104 40.769975451779729)', 4326));
INSERT INTO dbo.Interests (Name,geo) VALUES ('Albert Bertel Thorvaldsen',geography::STGeomFromText('POINT(-73.955996808113582 40.788611756916609)', 4326));
INSERT INTO dbo.Interests (Name,geo) VALUES ('Alice in Wonderland',geography::STGeomFromText('POINT(-73.966714294355356 40.7748020248959)', 4326));
INSERT INTO dbo.Interests (Name,geo) VALUES ('Hans Christian Andersen',geography::STGeomFromText('POINT(-73.96756141015176 40.774416211045626)', 4326));
/* Details */
INSERT INTO dbo.Details(Name,geo) VALUES ('Alexander Hamilton',geography::STGeomFromText('POINT(-73.9645616688172 40.7810234271951)', 4326));
INSERT INTO dbo.Details(Name,geo) VALUES ('Arthur Brisbane',geography::STGeomFromText('POINT(-73.953249720745731 40.791599412827864)', 4326));
INSERT INTO dbo.Details(Name,geo) VALUES ('Hans Christian Andersen',geography::STGeomFromText('POINT(-73.9675614098224 40.7744162102582)', 4326));
INSERT INTO dbo.Details(Name,geo) VALUES ('Balto',geography::STGeomFromText('POINT(-73.9710128455336 40.7699754516397)', 4326));
A brute force approach will be to compute the distance between all details X interests records:
SELECT *
FROM
(
SELECT *, rank=dense_rank() over (partition by IName, IGeo order by dist asc)
FROM
(
SELECT D.NAME as DName, D.geo as DGeo,
I.NAME as IName, I.geo as IGeo,
I.geo.STDistance(D.geo) AS dist
FROM dbo.Details D CROSS JOIN dbo.Interests I
) X
) Y
WHERE rank <= #k
NotE: #k is the target number of matches you are after