Banking Transaction in SQL - sql

I have a table named BankTransaction:
Create table BankTransaction
(
TransactionID int IDENTITY(1,1),
AccountNumber varchar(25) Not Null,
TransactionDate datetime not null Default getdate(),
TransactionType varchar(25) Not Null,
TransactionAmount money Default '0',
BalanceAsOf money Default '0' ,
Primary Key(TransactionID)
);
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Credit','22535.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','215.9');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','5000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','235.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','1000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('7865HNH','Credit','982000');
what I want is that The account having same AccountNumber if the TransactionType is Debit then subtract from BalanceAsOf and if the TransactionType is Credit then adding to BalanceAsOF:
This is what I tried but doesn't work:
update bt
Set BalanceAsOF = case
when AccountNumber=AccountNumber and TransactionType = 'Credit'
then ( BalanceAsOf + TransactionAmount )
when AccountNumber=AccountNumber AND TransactionType='Debit'
then BalanceAsOf - TransactionAmount
End
from
dbo.BankTransaction as bt
Select * From dbo.BankTransaction as p

This is exactly what you should do
set nocount on;
Create table BankTransaction
(
TransactionID int IDENTITY(1,1),
AccountNumber varchar(25) Not Null,
TransactionDate datetime not null Default getdate(),
TransactionType varchar(25) Not Null,
TransactionAmount money Default '0',
BalanceAsOf money Default '0' ,
Primary Key(TransactionID)
);
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Credit','22535.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','215.9');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','5000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Debit','100');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('123AABDF','Debit','235.215');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2256DF','Credit','1000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('2351AV','Credit','500');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('7865HNH','Credit','982000');
Insert into dbo.BankTransaction(AccountNumber,TransactionType,TransactionAmount)
Values ('876YYT','Credit','27363647');
Create Table #AccountNomber (
AccountNumber varchar(25) Not Null,);
insert into #AccountNomber ( AccountNumber)
Select Distinct BT.AccountNumber
From dbo.BankTransaction as BT
--select * from #AccountNomber
Declare #PreviousBalance money
Set #PreviousBalance = 0
declare #AcctNumber varchar(25)
declare #TransactionId int
--update b
--set b.BalanceAsOf=0
--from BankTransaction b
while exists(select 1 from #AccountNomber)
begin
select #AcctNumber = a.AccountNumber from #AccountNomber a
--select 'test',* from BankTransaction b
--where b.AccountNumber=#AcctNumber
--and b.BalanceAsOf=0
--order by b.TransactionID
--/*
while exists (select 1 from BankTransaction b where b.AccountNumber=#AcctNumber
and b.BalanceAsOf=0
)
begin
select #TransactionId = (select top 1 b.TransactionID from BankTransaction b
where b.AccountNumber=#AcctNumber
and b.BalanceAsOf=0
order by b.TransactionID)
select #PreviousBalance =#PreviousBalance + case when b.TransactionType='Credit' then b.TransactionAmount else -1*b.TransactionAmount end
from BankTransaction b
Where b.AccountNumber=#AcctNumber
and b.TransactionID=#TransactionId
update b
set b.BalanceAsOf=#PreviousBalance
from BankTransaction b
Where b.AccountNumber=#AcctNumber
and b.TransactionID=#TransactionId
end
select #PreviousBalance =0
delete a from #AccountNomber a
where a.AccountNumber=#AcctNumber
End
select * from BankTransaction b
order by b.TransactionID
Drop table BankTransaction
drop table #AccountNomber

Is there a reason to store the current Balance for each transaction? If you just want to display the balance to user you can have a column in Accounts table for example and for every transaction (insert into BankTransaction) you update this column via trigger on BankTransaction or through your application.
UPDATE
Use a trigger to calculate the balance before inserting the new transaction
CREATE TRIGGER trig_UpdBalance
ON [BankTransaction]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO BankTransaction
AccountNumber,
TransactionType,
TransactionAmount,
BalanceAsOf
SELECT
i.AccountNumber,
i.TransactionType,
i.TransactionAmount,
CASE
WHEN i.TransactionType = 'Credit'
THEN ( bt.BalanceAsOf + i.TransactionAmount )
WHEN i.TransactionType='Debit'
THEN bt.BalanceAsOf - i.TransactionAmount
END
FROM Inserted i
INNER JOIN BankTransaction bt
ON BT.AccountNumber = i.AccountNumber
AND TransactionID = (SELECT MAX(TransactionID)
FROM BankTransaction
WHERE AccountNumber = i.AccountNumber)
END

Related

Compare two tables and find not matched price SQL Server

create table #tmp1
(
name varchar(100),
price decimal(10, 2)
)
insert into #tmp1 values('Test', 200.00)
insert into #tmp1 values('Test', 100.00)
insert into #tmp1 values('Test', 300.00)
insert into #tmp1 values('Test1', 500.00)
insert into #tmp1 values('Test2', 300.00)
create table #tmp
(
name varchar(100),
price decimal(10,2)
)
insert into #tmp values('Test', 200.00)
insert into #tmp values('Test', 100.00)
insert into #tmp values('Test', 400.00)
insert into #tmp values('Test1', 600.00)
insert into #tmp values('Test3', 300.00)
I have two tables with comparing parameter Name and find not matched price.
Name should match and price will be differ.
Expected output:
Name Price Price
------------------------------
Test1 600.00 500.00
Test 300.00 400.00
Try this
SELECT *
FROM #tmp a
INNER JOIN #tmp1 b ON a.name=b.name AND a.PRICE <> b.PRICE
UPDATED : AS per your below comment you wants to consider order of insertion as well,
;WITH tmp AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME) RN
FROM #tmp
),tmp1 AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME) RN
FROM #tmp1
)
SELECT a.name,a.price,b.price
FROM tmp a
INNER JOIN tmp1 b ON a.name=b.name AND a.RN=b.RN AND a.PRICE <> b.PRICE

SQL Trigger that should update other table

I am trying to make a trigger that will update a table only if something is inserted in an other table
CREATE TABLE Purchases
(
PurchaseID INT IDENTITY NOT NULL PRIMARY KEY,
DateBought SMALLDATETIME,
Price MONEY CHECK (Price>0),
RealEstateID INT UNIQUE NOT NULL,
DealMadeByEmployeeID INT NOT NULL
);
INSERT INTO Purchases(DateBought, Price, RealEstateID, DealMadeByEmployeeID)
VALUES ('20161015 15:10:03','120000',3, 3); --Апартамент 170 квадратни метра
INSERT INTO Purchases(DateBought, Price, RealEstateID, DealMadeByEmployeeID)
VALUES ('20161219 13:13:30','200000',4, 3); --Къща 500 квадратни метра
INSERT INTO Purchases (Price,RealEstateID,DealMadeByEmployeeID)
VALUES ('130000',6,3); --непродаден апартамент
CREATE TABLE EmployeesSalary
(
EmployeeID INT NOT NULL PRIMARY KEY,
CurrentSalary MONEY DEFAULT 0,-- на процент
MonthlySalesMade INT DEFAULT 0,
MonthlyRentsMade INT DEFAULT 0
);
INSERT INTO EmployeesSalary (EmployeeID, CurrentSalary)
VALUES (1, '400');
INSERT INTO EmployeesSalary (EmployeeID, CurrentSalary)
VALUES (2, '400');
INSERT INTO EmployeesSalary (EmployeeID, CurrentSalary)
VALUES (3, '400');
CREATE TRIGGER tr_EmployeesSalaryPurchasesUpdate --при INSERT в Purchases таблицата
ON Purchases
AFTER INSERT
AS
BEGIN
UPDATE EmployeesSalary
SET EmployeesSalary.MonthlySalesMade=EmployeesSalary.MonthlySalesMade+1
WHERE EmployeesSalary.EmployeeID IN (SELECT inserted.DealMadeByEmployeeID FROM inserted)
END
but based on the IDs.
My code doesn't get any errors but the trigger doesn't seem to work. What is my mistake?
https://postimg.org/image/rot0xcriv/
You should use an INNER JOIN there:
CREATE TRIGGER tr_EmployeesSalaryPurchasesUpdate --при INSERT в Purchases таблицата
ON Purchases
AFTER INSERT
AS
BEGIN
UPDATE ES
SET ES.MonthlySalesMade = ES.MonthlySalesMade+1
FROM EmployeesSalary ES
INNER JOIN INSERTED I
ON ES.EmployeeID = I.DealMadeByEmployeeID;
END
It won't work if there are multiple updates unless NOCOUNT is SET to ON:
CREATE TRIGGER tr_EmployeesSalaryPurchasesUpdate
ON Purchases
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON -- new line of code
UPDATE EmployeesSalary
SET EmployeesSalary.MonthlySalesMade=EmployeesSalary.MonthlySalesMade+1
WHERE EmployeesSalary.EmployeeID IN (SELECT inserted.DealMadeByEmployeeID FROM inserted)
END
You can also use the EXISTS operator.
Also beaware of the possible NULLS in MonthlySalesMade column, +1 update will not work if there is a null values in that column.
CREATE TRIGGER tr_EmployeesSalaryPurchasesUpdate
ON Purchases
AFTER INSERT
AS
BEGIN
UPDATE EmployeesSalary
SET MonthlySalesMade = ISNULL(MonthlySalesMade, 0)+1
WHERE EXISTS (SELECT 1
FROM inserted
WHERE EmployeesSalary.EmployeeID = inserted.DealMadeByEmployeeID )
END

SQL stored procedure results solution

I have a stored procedure that I inherited. The goal is for the data in the temp table: #multi_nominees_uf to be joined with the data in #temp_reviewers_UF and assign #temp_reviewers_UF.uf_rev_id to #multi_nominees_uf.application_number where the corresponding uf_rev_id's short_plan does not match the major associated with the appNumber's major .
It is not as simple as doing a JOIN.
The following has to be in place:
short_plan can not match the major(associated with the uf_rev_id)
count of each uf_rev_id can only be in the table a certain number of times (#RevsPerRevieweruf). Also, the uf_rev_id will be in the #temp_reviewers_uf table more than once with a different short_plan, it should only be looking at DISTINCT uf_rev_id when calculating the #RevPerRevieweruf.
The way it is written now, the counts are not consistent. One uf_rev_ID may have 122 records and another may have 50 - each distinct uf_rev_id should have the same count (or very close). I have researched and tried NTILE but I couldn't figure it out.
Any ideas of the best way to accomplish this?? Any input is appreciated.
-----Sample Data -----
CREATE TABLE #mult_nominees_uf(
appnum VARCHAR(8)
,major VARCHAR(8)
,compid INT
);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78954321','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('90002342','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11156726','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('88855593','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000001','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('20000393','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('11119999','DED',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('78927626','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('67589393','RRE',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('12453647','AAR',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('00012345','ACT',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002343','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10002777','BBC',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('10000023','DED`',2);
INSERT INTO #mult_nominees_uf(appnum,major,compid) VALUES ('23457829','AAR',2);
--with this sample data the #RevsPerReviewerUF count would be 4 since A5 is listed twice and we only want distinct values used
CREATE TABLE #Temp_Reviewers_uf(
uf_rev_id VARCHAR(8)
,short_plan VARCHAR(8)
,fac_emplid INTEGER
);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A1','ACT',00000012);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A2','BBC',00000145);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A3','DED',10002934);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','RRE',90001223);
INSERT INTO #Temp_Reviewers_uf(uf_rev_id,short_plan,fac_emplid) VALUES ('A5','ACT',90001223);
----Stored procedure -
DECLARE #Index INT
DECLARE #Num_nomineesUF INT
DECLARE #Num_reviewersUF INT
DECLARE #Num_reviewersUFDISTINCT INT
DECLARE #Num_reviews INT
DECLARE #Rev_ID nvarchar(25), #Nom_ID varchar(8), #Short_Plan varchar(8), #Major varchar(8)
DECLARE #RevsPerReviewerUF INT
SET #Num_reviews = 4
DECLARE #actualCompID int
DECLARE #UF_Flag INT
DECLARE #InsertNum int
SET #InsertNum = 1
create table #mult_nominees_UF (appNumber varchar(8), Major varchar(8), comp_id INT)
create table #TempNomineeTable (uf_rev_id varchar(8), fac_emplid varchar(9), appNumber varchar(8), Major varchar(8), short_plan varchar(8), comp_id int)
create table #Temp_Reviewers_UF (uf_rev_id varchar(8), short_plan varchar(8), fac_emplid varchar(9)) -- temp table used to hold Nom_IDs already assigned to Rev_IDs
set #actualCompID = 21
-- * * SELECT APPLICATION NUMBER & MAJOR FROM FS_RESULTS TABLE * * * * * --
select appNumber, LEFT(Major, CHARINDEX('-', Major)-1) as Major, comp_id into #Delete_nomineesUF
from FS_Results
where UF='Y'
and comp_id = #actualCompID
and nominated=1;
SET #Num_nomineesUF = ##rowcount; --GET RECORD COUNT
IF (#Num_nomineesUF > 0)
BEGIN
SET #UF_Flag = 1;
END
SET #Index = 1 ; -- reinit variable
WHILE #Index <= 4 BEGIN
if (#UF_Flag > 0)
BEGIN
INSERT into #mult_nominees_uf
select * from #Delete_nomineesUF
END
-- Create temp table for UF Reviewers
select uf_rev_id, short_plan, fac_emplid into #temp_reviewers_UF
from ReviewersID_ShortPlan
where uf_rev_id like 'UF%'
and competition_id = #actualCompID
SET #Num_reviewersUF = ##rowcount
SELECT DISTINCT UF_REV_ID FROM ReviewersID_ShortPlan WHERE UF_REV_ID like 'UF%' AND competition_id = #actualCompID
SET #Num_reviewersUFDistinct = ##rowcount
SET #RevsPerReviewerUF = (#Num_nomineesUF * #Num_reviews) / nullif(#Num_reviewersUFDistinct,0)
WITH Match_NomineesWithReviewers AS(
SELECT DISTINCT
appNumber,
RTRIM(Major) AS Major,
COUNT(1) as rowcnt,
comp_id
FROM #mult_nominees_uf
GROUP BY appNumber,
RTRIM(Major),
comp_id
)
, rownum_matches AS (
SELECT m.appNumber,
m.Major,
t.short_plan,
t.uf_rev_id,
t.fac_emplid,
m.rowcnt,
m.comp_id,
ROW_NUMBER() OVER (PARTITION BY m.appNumber order by newid()) AS rownum
FROM Match_NomineesWithReviewers m
JOIN #temp_reviewers_UF t ON t.short_plan != m.major
GROUP BY m.appNumber, m.Major, t.short_plan,
t.uf_rev_id, t.fac_emplid, m.rowcnt, m.comp_id
HAVING COUNT(t.uf_rev_id) <= #RevsPerRevieweruf
)
INSERT INTO #TempNomineeTable
SELECT uf_rev_id, fac_emplid, appNumber, Major, short_plan, null, 0, null, comp_id FROM rownum_matches rm
WHERE rownum <= rowcnt
group by uf_rev_id, fac_emplid, appNumber, Major, short_plan, comp_id
HAVING COUNT(uf_rev_id) <= #RevsPerRevieweruf

Custom query assistance needed

I have 3 tables: (simplified for this question)
REQUIREMENTS : ID (PK), RequirementDescription
CUSTOMERS : ID (PK), CustomerName
CUSTOMER_REQUIREMENTS (join table): ID (PK), RequirementID (FK to REQUIREMENTS table), CustomerID (FK to CUSTOMERS table), DateCompleted
Question: I need to make a gridview in asp.net that basically shows the requirements on the left, all the customers as column headings and the DateCompleted as the "intersection". How can I do this?
For example:
Try following sql and replace temp tables with your table:
CREATE TABLE #REQUIREMENTS
(
ID INT,
RequirementDescription VARCHAR(100)
)
CREATE TABLE #CUSTOMERS
(
ID INT,
CustomerName VARCHAR(100)
)
CREATE TABLE #CUSTOMER_REQUIREMENTS
(
ID INT,
RequirementID INT,
CustomerID INT,
DateCompleted DATE
)
INSERT INTO #REQUIREMENTS VALUES (1,'Requirement1')
INSERT INTO #REQUIREMENTS VALUES (2,'Requirement2')
INSERT INTO #REQUIREMENTS VALUES (3,'Requirement3')
INSERT INTO #REQUIREMENTS VALUES (4,'Requirement4')
INSERT INTO #CUSTOMERS VALUES (1,'JOHN')
INSERT INTO #CUSTOMERS VALUES (2,'MARY')
INSERT INTO #CUSTOMERS VALUES (3,'BOB')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,1,1,'2-2-2014')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,2,1,'2-2-2014')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,1,2,'2-2-2014')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,2,2,'2-2-2014')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,3,2,'2-2-2014')
INSERT INTO #CUSTOMER_REQUIREMENTS VALUES (1,4,2,'2-2-2014')
DECLARE #DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE #ColumnName AS NVARCHAR(MAX)
SELECT #ColumnName= ISNULL(#ColumnName + ',','') + QUOTENAME(CustomerName) FROM (SELECT DISTINCT CustomerName FROM #CUSTOMERS) AS Courses
SET #DynamicPivotQuery =
N'SELECT ReqID, ReqDes, ' + #ColumnName + '
FROM
(
SELECT
TEMP.ReqID,
TEMP.ReqDes,
TEMP.CusName,
CusReq.DateCompleted
FROM
(
SELECT
req.ID as ReqID,
req.RequirementDescription as ReqDes,
cus.ID as CusID,
cus.CustomerName as CusName
FROM
#REQUIREMENTS req,#CUSTOMERS cus
) AS TEMP
LEFT JOIN
#CUSTOMER_REQUIREMENTS as CusReq
ON
TEMP.ReqID = CusReq.RequirementID AND
Temp.CusID = CusReq.CustomerID
) AS TEMP1
PIVOT(MAX(TEMP1.DateCompleted)
FOR CusName IN (' + #ColumnName + ')) AS PVTTable'
EXEC sp_executesql #DynamicPivotQuery
DROP TABLE #CUSTOMER_REQUIREMENTS
DROP TABLE #CUSTOMERS
DROP TABLE #REQUIREMENTS

How to update column coming from TOP 1 of another table

I have 2 tables:
City table - columns CityID, Name, Period
Assets table - columns AssetID, Name
I have to update the Period column of the City table with AssetID of the Assets table matching with the top 1 where City.Name=Assets.Name. The Assets table have identical names for different assets.
Example Assets table:
AssetID Name
1 Asset1
2 Asset1
3 Asset2
4 Asset2
How can I do this? I tried with different queries but I am not able to get it.
UPDATE City
SET Period = a.AssetID
FROM (SELECT TOP 1 AssetID, Name FROM Assets ORDER BY AssetID ASC) AS a
WHERE City.Name = a.Name;
This should work:
update City
set Period = (
select top 1 a.AssetID
from Assets a
where City.Name = a.Name
order by a.AssetId asc)
Sample code to test:
create table #City (CityId varchar(20), [Name] varchar(20), Period varchar(20))
create table #Assets (AssetId varchar(20), [Name] varchar(20))
insert into #City values (1, 'Asset1', null)
insert into #City values (2, 'Asset2', null)
insert into #City values (3, 'Asset3', null)
insert into #Assets values (1, 'Asset1')
insert into #Assets values (2, 'Asset1')
insert into #Assets values (3, 'Asset1')
insert into #Assets values (4, 'Asset2')
insert into #Assets values (5, 'Asset2')
insert into #Assets values (6, 'Asset3')
insert into #Assets values (7, 'Asset3')
select * from #City
select * from #Assets
update #City
set Period = (
select top 1 a.AssetID
from #Assets a
where #City.Name = a.Name
order by a.AssetId asc)
select * from #City
drop table #City
drop table #Assets