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.
Related
So, I have a table table with three columns I am interested in : value, entity and field_entity.
There are other columns that are not important for this question. There are many different types of entity, and some of them can have the same field_entity, but those two columns determine what the column value refers to (if it is an id number for a person or the address or some other thing)
If I need the name of a person I would do something like this:
select value from table where entity = 'person' and field_entity = 'person_name';
My problem is I need to get a lot of different values (names, last names, address, documents, etc.), so the way I am doing it now is using a left join like this:
select
doc_type.value as doc_type,
doc.value as doc,
status.value as status
from
table doc
-- Get doc type
left join table doc_type
on doc.entity = doc_type.entity
and doc.transaction_id = doc_type.transaction_id
and doc_type.field_entity = 'document_type'
-- Get Status
left join table status
on doc.entity = status.entity
and doc.transaction_id = status.transaction_id
and status.field_entity = 'status'
where doc.entity = 'users' and doc.field_entity = 'document' and doc.transaction_id = 11111;
There are 16 values or more, and this can get a bit bulky and difficult to maintain, so I was wondering if some of you can point out a better way to do this?
Thanks!
I assume that you are not in position to alter the table structure, but can you add views to the database? If so, you can create views based on the different types of entities in your table.
For example:
CREATE VIEW view_person AS
SELECT value AS name
FROM doc
WHERE doc.entity = 'person'
AND doc.field_entity = 'name';
Then you can write clearer queries:
SELECT view_person.name FROM view_person;
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.
I have data in one table (OWNER) i'm trying to use to update the data in another table (TAX_BILL_INFO). I've written an update statement to take the value from one column (owner.ownername) and insert that value into another column (tax_bill_info.mailername) where they have the same ID (taxbillid). below is my statement that I used:
UPDATE TAX_BILL_INFO
SET Mailername = OWNER.Ownername
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
where taxyear = '2013' and (mailername = '' or mailername = ' ' or mailername is null) and (purchasername = '' or purchasername = ' ' or purchasername is null)
The columns are updating, it is only putting the FIRST value from OWNER into EVERY column in tax_bill_info. (ex. john smith, john smith, john smith). Are my where criteria throwing this off? Or could it be something else?
EDIT
If I use this select query:
select owner.OwnerName
FROM owner INNER JOIN
TAX_BILL_INFO ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
the names pull as they should. If i do a select query from the table I need the data entered into, it gives me the same dupliacte name for every row:
SELECT MAILERNAME
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID
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.
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