i want to select data from a table where the account code adn fundocde are equal to each other and where there in comp a and comp t in the class load code.
here is my sql
select * from dbo.SSCUKWorkingGLDataloadFile2
where AccountCode = AccountCode
and FundCode = FundCode
AND ClassLoadCode = 'Comp A' AND ClassLoadCode = 'Comp T'
i no there is account numbers which has both classloadcode so want to ull them back
they have the same accout code and fund code but different balances and class load codes. they just want the one thats has comp a and remove the comp t
To find the AccountCode+FundCode combinations where ClassLoadCode = 'Comp T' as well as a ClassLoadCode = 'Comp A', use this. It lists such rows where ClassLoadCode = 'Comp T'.
SELECT * FROM dbo.SSCUKWorkingGLDataloadFile2 a
WHERE
ClassLoadCode = 'Comp T'
AND EXISTS
(
SELECT * FROM dbo.SSCUKWorkingGLDataloadFile2 b
WHERE
a.AccountCode = b.AccountCode
AND a.FundCode = b.FundCode
AND ClassLoadCode = 'Comp A'
)
To delete those rows, replace the select with delete like this:
DELETE FROM dbo.SSCUKWorkingGLDataloadFile2 a
WHERE
ClassLoadCode = 'Comp T'
AND EXISTS
(
SELECT * FROM dbo.SSCUKWorkingGLDataloadFile2 b
WHERE
a.AccountCode = b.AccountCode
AND a.FundCode = b.FundCode
AND ClassLoadCode = 'Comp A'
)
The SELECT and the DELETE should be affecting exactly the same rows. When you run the SELECT, it lets you preview what you are going to delete.
If the select statement is not displaying exactly the rows that you want to delete, change the select query so you get exactly what you want to delete. Then replace the select * with delete, so you will delete exactly those rows.
If you have only 23 rows left after you delete, that probably means that there are only 46 rows in the table? How may rows were in the table before you deleted?
Also, is it possible that you have more than one row for a given combination of AccountCode, FundCode, and ClassLoadCode?
See this example. You can copy and paste this to an SSMS query window and run it to see what it does:
CREATE TABLE #tmp(AccountCode INT, FundCode INT, ClassLoadCode VARCHAR(32));
INSERT INTO #tmp VALUES
(1,100,'Comp T'),(1,100,'Comp A'),
(2,200,'Comp T'),
(3,300,'Comp A');
SELECT * FROM #tmp a
WHERE
ClassLoadCode = 'Comp T'
AND EXISTS
(
SELECT * FROM dbo.#tmp b
WHERE
a.AccountCode = b.AccountCode
AND a.FundCode = b.FundCode
AND ClassLoadCode = 'Comp A'
)
DELETE a FROM #tmp a
WHERE
ClassLoadCode = 'Comp T'
AND EXISTS
(
SELECT * FROM d*emphasized text*bo.#tmp b
WHERE
a.AccountCode = b.AccountCode
AND a.FundCode = b.FundCode
AND ClassLoadCode = 'Comp A'
)
I had an error in the DELETE statement. It should have been DELETE a FROM..." rather than "DELETE FROM"
DELETE a FROM dbo.SSCUKWorkingGLDataloadFile2 a
WHERE
ClassLoadCode = 'Comp T'
AND EXISTS
(
SELECT * FROM dbo.SSCUKWorkingGLDataloadFile2 b
WHERE
a.AccountCode = b.AccountCode
AND a.FundCode = b.FundCode
AND ClassLoadCode = 'Comp A'
)