Question about SQL UPDATE with CASE statement - sql

So I have this SQL table that I need to update. Now we use a tool to do the process so if we just renamed the column then it wouldn't work because our tool would just drop the old column and add a new column with the new name, so we have to do it manually and enter this code into the build and exclude the table from our build.
The table we CURRENTLY have has a column called 'FeeTypeId'. We have another table, a Lookup table, that has a bunch of ID's relating to types and descriptions. We want that previous 'FeeTypeId' field on the other table I just mentioned, to contain these LookupId's which relate to their specific 'FeeDescription's. I will have an Excel sheet that contains the mappings, so for right now there is no real data, just logic.
Here is the script I wrote but I don't know if it will run. Can you guys help me out?
----------------------------------------------------------------------
--PRE_UPGRADE--
----------------------------------------------------------------------
CREATE TABLE dbo.TmpFeesToRules(OldValue varchar, NewValue varchar)
--populate with values from Excel
----------------------------------------------------------------------
--POST UPGRADE--
----------------------------------------------------------------------
UPDATE Lending.ApplicationFee
SET FeeTypeId =
CASE t.NewValue
WHEN <> null THEN (SELECT LookupID FROM tblLookup WHERE LookupType = 'FEE_CODE' AND LookupDesc = t.NewValue)
ELSE (SELECT LookupID FROM tblLookup WHERE LookupType = 'FEE_CODE' AND LookupCode = 'OTHER') END
-- else might change, might not even need an else
FROM TmpFeesToRules t INNER JOIN tblLookup l ON t.NewValue = l.LookupDesc
-- Drop the tmp table
DROP TABLE dbo.TmpFeesToRules

I can't really test this but the following is, according to Toad, valid SQL. Perhaps you can try to run this:
UPDATE Lending.ApplicationFee
SET FeeTypeId =
(
CASE WHEN t.NewValue IS NOT NULL THEN
(SELECT LookupID FROM tblLookup WHERE LookupType = 'FEE_CODE' AND LookupDesc = t.NewValue)
ELSE
(SELECT LookupID FROM tblLookup WHERE LookupType = 'FEE_CODE' AND LookupCode = 'OTHER')
END
)
FROM TmpFeesToRules t
INNER JOIN tblLookup l ON t.NewValue = l.LookupDesc

Related

finding int_ids that have more than one row with a flag set to Y

I have a table that can store multiple descriptions for each code. However there is a flag in that table that is to indicate which of those is the main or primary description. In some instances, we have codes that have more than one with this flag set to Y which is not correct.
I am having trouble coming up with the SQL to get all the rows in that table that have more than one description set to Y.
I've used this SQL to identify rows that do not have ANY dsp_fg = 'Y'
select *
from table A
where dsp_fg = 'N'
and not exists (select 1 FROM table where cod_int_id = A.cod_int_id AND dsp_fg = 'Y')
But I am having trouble writing the SQL to get me the cod_int_ids that have more than one Y record, can someone help?
SELECT int_id FROM A
WHERE dsp_fg = 'Y'
GROUP BY int_id
HAVING count(1) > 1
This is not perfect, but it identifies what I need.

updating values of a table from another table through a join changes all values to 1 single value

I have 3 tables: raw_sales, sales and details. raw_sales is being populated using COPY from a txt file. All the fields in raw_sales are either string or text. After importing, we run an sql to populate sales and details. There is a foreign key (sale_id) in details. Here's a sample INSERT command that we use to populate sales and details.
INSERT INTO sales (source, source_identifier)
(SELECT DISTINCT
'FOO' AS source,
"identifier" AS source_identifier
FROM raw_sales
LEFT JOIN sales
ON sales.source_identifier = raw_sales.identifier
AND sales.source = 'FOO'
WHERE sales.id IS NULL
AND identifier IS NOT NULL);
INSERT INTO details (sale_id, description)
(SELECT DISTINCT
sales.id AS sale_id,
"improvements" as description
FROM raw_sales
JOIN sales
ON sales.source_identifier = raw_sales.identifier
AND sales.source = 'FOO'
LEFT JOIN details AS existing
ON existing.sale_id = sales.id
WHERE existing.id IS NULL
AND "improvements" != '');
This seems to work fine. After this, there's another sql that's being ran to update existing tables. The query is as follows
UPDATE details SET
description = "improvements"
FROM raw_sales
JOIN sales
ON sales.source_identifier = raw_sales.identifier
AND sales.source = 'FOO'
JOIN details AS existing
ON existing.sale_id = sales.id
WHERE existing.id IS NOT NULL;
This query updates all rows in the details table to a single value, the first non-empty value from raw_sales table. How can I change the above sql so that it updates the existing records in the details table?
There are several problems with your query:
if details.id is a primary key (field id typically is), then what is the point in comparing it to NOT NULL? You're not using any left joins, there is no way it could possibly be NULL if it truly is an identifier.
UPDATE table t SET ... FROM ... requires linking the table t with something on the FROM section, but you're not, therefore each row of table will be updated to any single random row from the FROM results.
Perhaps you want to do this:
UPDATE details SET
description = "improvements"
FROM raw_sales
JOIN sales ON (sales.source_identifier = raw_sales.identifier AND sales.source = 'FOO')
JOIN details AS existing ON (existing.sale_id = sales.id)
WHERE existing.id = details.id;

Sql Server - Update multiple rows based on the primary key (Large Amount of data)

struggling with this one, quite a lengthy description so ill explain best I can:
I have a table with 12 columns in, 1 being a primary key with identity_insert, 1 a foreign key , the other 10 being cost values, ive created a statement to group them into 5 categories, shown below:
select
(ProductID)ProjectID,
sum(Cost1)Catagory1,
sum(Cost2)Catagory2,
sum(Cost3 + Cost4 + Cost5 + Cost6 + Cost7) Catagory3,
sum(Cost 8 + Cost 9)Catagory4,
sum(Cost10)Catagory5
from ProductTable group by ProductID
ive changed the names of the data to make it more generic, they aren't actually called Cost1 etc by the way ;)
the foreign key can appear multiple times (ProductID) so in the above query the related fields are calculated together based upon this... Now what ive been trying to do is put this query into a table, which i have done successfully, and then update the data via a procedure. the problem im having is that all the data in the table is overwritten by row 1 and when theres is thousands of rows this is a problem.
I have also tried putting the above query into a view and the same result... any suggestions would be great :)
update NewTable set
ProductID = (ProductView.ProductID ),
Catagory1 = (ProductView.Catagory1 ),
Catagory2 = (ProductView.Catagory2 ),
Catagory3 = (ProductView.Catagory3 ),
Catagory4 = (ProductView.Catagory4 ),
Catagory5 = (ProductView.Catagory5 )
from ProductView
I need something along the lines like above.... but one that doesn't overwrite everything with row 1 haha ;)
ANSWERED BY: Noman_1
create procedure NewProducts
insert into NewTable
select ProductID.ProductTable,
Catagory1.ProductView,
Catagory2.ProductView,
Catagory3.ProductView,
Catagory4.ProductView,
Catagory5.ProductView
from ProductView
inner join ProductTable on ProductView.ProductID = ProductTable.ProductID
where not exists(select 1 from NewTable where ProductView.ProductID = NewTable.ProductID)
above procedure locates the new Product that has been created within a view, the procedure query detects that there is a Product that is not located in the NewTable and inserts it via the procedure
As far as i know, and since you want to update all the products in the table, and each product uses all the sums of the product itself from origin, you actually need to update each row 1 by 1, and as consecuence when you do an update like the next, its your only main way
update newtable
set category1 = (select sum(cost1) from productTable where productTable.productId = newtable.ProductID),
category2 = (select sum(cost2) from productTable where productTable.productId = newtable.ProductID),
etc..
Keep in mind that if you have new products, they wont get inserted with the update, you would need like this in order to add them:
Insert into newtable
Select VALUES from productTable a where productId not exists(select 1 from newTable b where a.ProductId = b.ProductId);
A second way, and since you want allways to update all the data, is to simply truncate and do a insert select right after.
Maybe on an Oracle, you would be albe to use a MERGE but im unaware if it would really improve anything.
I asume that simply having a view would not work due the amount of data you state you have.
EDIT, I never knew that the MERGE STATMENT is actually avaiable on SQL Server 2008 and above, with this single statment you could do an UPDATE/INSERT on all but it's efficiency is unknown to me, you may want to test it with your high amount of data:
MERGE newtable AS TARGET
USING select ProductId, sum(cost1) cat1, sum(cost2) cat2 ...
FROM productTable Group by ProductId AS SOURCE
ON TARGET.ProductId = SOURCE.ProductID
WHEN MATCHED
THEN UPDATE SET TARGET.category1 = cat1, TARGET.category2 = cat2...
WHEN NOT MATCHED
THEN INSERT (ProductId, category1, category2,...)
VALUES (SOURCe.ProductId, SOURCE.cat1, SOURCE.cat2...);
More info about merge here:
http://msdn.microsoft.com/library/bb510625.aspx
The example at the end may give you a good overview of the sintax
You haven't given any join condition. SQL Server cannot know that you meant to update rows matched by productid.
update NewTable set
ProductID = (ProductView.ProductID ),
Catagory1 = (ProductView.Catagory1 ),
Catagory2 = (ProductView.Catagory2 ),
Catagory3 = (ProductView.Catagory3 ),
Catagory4 = (ProductView.Catagory4 ),
Catagory5 = (ProductView.Catagory5 )
from NewTable
join ProductView pv on NewTable.productid = pv.productid
You don't need a view. Just past the view query to the place where I said ProductView. Of course, you can use a view.

coldfusion Hide/remove a name from a menu/list

I have an insert form that I did in adobe coldfusion. This insert form inserts information from the men on my team into a databases table called squadreport. After every insert into my squadreport table, I would like to remove the name of the person that I just inserted from the menu list of available names of those on my team who have not been inserted yet. This way I won’t accidently reinsert their names. The menu list are generated from my NAMES tables. The name.foiid field from my NAMES table matches my squadreport.squfoiid field from my SQUADREPORT table.
Example: If the squadreport.squfoiid is already recorded with the start date of ‘2012-11-18’ and a end date of ‘2012-11-24’, then I want to prevent the reinsertion of same person with the same dates etc.
My current syntax will generate a blank list.
Here’s is what my current syntax look like:
<cfquery name="brother" datasource="master">
SELECT name.foiid, squadlt, squadlt.ltid, CONCAT(name.fname,' ',name.lname) AS teammember
FROM name
LEFT JOIN name ON name.foiid = squadreport.squfoiid
LEFT JOIN squadlt ON ltid = squadreport.sqult
WHERE name.foiid is null
AND squweekbegin =’2012-11-18’
AND squweekend = ‘2012-11-24’
AND squadlt = '3'
AND ltid = '3'
AND CITY = 'sandiego'
AND STATUS <> 'd'
AND STATUS <> 'T'
AND Form4444Complete = 'yes'
ORDER BY teammember
</cfquery>
Assuming you're inserting the rows into SquadReport with matching foiid columns, you'd just need to exclude the foiid values which exist in the SquadReport table. If there are other conditions like the squad member has to have been activated, or deployed or something, just add it to the exclusion clause.
<cfquery name="brother" datasource="master">
SELECT name.foiid, squadlt, squadlt.ltid, CONCAT(name.fname,' ',name.lname) AS teammember
FROM name
LEFT JOIN name ON name.foiid = squadreport.squfoiid
LEFT JOIN squadlt ON ltid = squadreport.sqult
WHERE name.foiid is null
AND squweekbegin =’2012-11-18’
AND squweekend = ‘2012-11-24’
AND squadlt = '3'
AND ltid = '3'
AND CITY = 'sandiego'
AND STATUS <> 'd'
AND STATUS <> 'T'
AND Form4444Complete = 'yes'
<!--- new code here --->
AND name.foiid NOT IN (SELECT foiid FROM SquadReport)
<!--- end new code --->
ORDER BY teammember
</cfquery>
So what you basically want to do is to display people not in the table squadreport? Something like this should work:
select name.foiid
, CONCAT(name.fname,' ',name.lname) AS teammember
from name
where not exists
(select 1
from squadreport
where squfoiid = name.foiid
and other constraints
)
and other constraints
I assume the code you posted is early development code. Once you go to production you will wanting to use date objects instead of strings and query parameters for any variables.

Updating a table by referencing another table

I have a table CustPurchase (name, purchase) and another table CustID (id, name).
I altered the CustPurchase table to have an id field. Now, I want to populate this newly created field by referencing the customer ids from the CustID table, using:
UPDATE CustPurchase
SET CustPurchase.id = CustID.id
WHERE CustPurchase.name = CustID.name;
I keep getting syntax errors!
I believe you are after the useful UPDATE FROM syntax.
UPDATE CustPurchase SET id = CI.id
FROM
CustPurchase CP
inner join CustID CI on (CI.name = CP.name)
This might have to be the following:
UPDATE CustPurchase SET id = CI.id
FROM
CustID CI
WHERE
CI.name = CustPurchase.name
Sorry, I'm away from my Postgres machine; however, based upon the reference, it looks like this is allowable. The trouble is whether or not to include the source table in the from_list.
Joining by name is not an ideal choice, but this should work:
UPDATE custpurchase
SET id = (SELECT c.id
FROM CUSTID c
WHERE c.name = custpurchase.name)
The caveat is that if there's no match, the value attempting to be inserted would be NULL. Assuming the id column won't allow NULL but will allow duplicate values:
UPDATE custpurchase
SET id = (SELECT COALESCE(c.id, -99)
FROM CUSTID c
WHERE c.name = custpurchase.name)
COALESCE will return the first non-NULL value. Making this a value outside of what you'd normally expect will make it easier to isolate such records & deal with appropriately.
Otherwise, you'll have to do the updating "by hand", on a name by name basis, to correct instances that SQL could not.