SQL Server: Selecting Specific Records From a Table with Duplicate Records (Excluding Stale Data from a Query) - sql

I'm trying to put together a query (select preferably) in SQL server that works with a single table. Said table is derived from two sets of data. Records where SET = OLD represent old data, records where SET = NEW represent new data. My intention is as follows:
If record CODE = A, keep/include the record.
If record CODE = C, keep/include the record but delete/exclude the corresponding record from the old set under the same ACT value.
If record CODE = D, delete/exclude it along with its corresponding record from the old set under the same ACT value.
If CODE = '' (blank/null), keep the record but only if it exists in the OLD set (meaning their isn't a corresponding record from the new set with the same ACT value)
What the table looks like before logic is applied:
ACT|STATUS |CODE|SET|VALUE
222| | |OLD|1
333| | |OLD|2
444| | |OLD|3
111|ADDED |A |NEW|4
222|CHANGED|C |NEW|5
333|DELETED|D |NEW|6
What the table should look like after logic is applied (end result)
ACT|STATUS |CODE|SET|VALUE
444| | |OLD|3
111|ADDED |A |NEW|4
222|CHANGED|C |NEW|5
While I can probably put together a select query to achieve the end result above I doubt it will run efficiently as the table in question has millions of records. What is the best way to do this without taking a long time to obtain the end result?

Something like this. you will have to split your query and union.
--Old Dataset
SELECT O.*
FROM MyTable O
LEFT JOIN Mytable N ON O.ACT = N.ACT AND N.[SET] = 'NEW'
WHERE O.[SET] ='OLD'
AND ISNULL(N.CODE,'A') = 'A'
UNION
-- New records
SELECT N.*
FROM MyTable N
WHERE N.[SET] ='NEW'
AND CODE <> 'D'

Related

SQL DELETE Query help..joining the same table together

I have a database that I need to write a delete query for.
There are thousands of participants in the database, and the table includes their internal id (first column) and then employment statuses. The table is called EE_EMPL_CAT The category ID is CAT_ID, and STAT_CD is the status of that category (employment category):
Now I need to make a delete query to delete the rows that include CAT_ID=4433 & STAT_CD ='NO'. But I only need these deleted if that same participant (=prsn_intn_id) also has CAT_ID=430 AND STAT_CD='AFACTF'. Also, to make sure all of these are current, I included an infinite end date (EFENDDT='2299-12-31').
My attempt at the code:
#UPDATE
#AQUA-435
#TOLERANCE-435
DELETE FROM &DATABASE..EE_EMPL_CAT A,
& DATABASE..EE_EMPL_CAT B
WHERE A.PRSN_INTN_ID = B.PRSN_INTN_ID
AND A.EMPL_CAT_ID = 430
AND A.EE_EMPLCAT_EFENDDT = '2299-12-31'
AND A.EMPL_STAT_CD = 'AFACTF'
AND B.EMPL_CAT_ID = 4433
AND B.EE_EMPLCAT_EFENDDT = '2299-12-31'
AND B.EMPL_STAT_CD = 'NO'

JOINS and absentee values

INSERT INTO Shipments (Column1...Column200)
SELECT
O.Value1,...
CL.Value199,
isnull(P.PriceFactor1,1)
FROM Orders O
JOIN Clients CL on O.ClientNo = CL.ClientNo
JOIN Calc C on CL.CalcCode = C.CalcCode
JOIN Prices P on CL.PriceKey = P.PriceKey
WHERE O.PriceFactor1 = P.PriceFactor1
AND O.PriceFactor2 = P.PriceFactor2
AND O.PriceFactor3 = P.PriceFactor3
The above query (part of a new stored procedure meant to replace an old and nasty one that used a cursor...) fails to return some rows, because the rows in Orders do not have matching rows in Prices. In such cases, we want the last value in the INSERT list to be 1 by default. Instead, the row is never built; or, when we tried to fix it by changing the WHERE conditions, it brought PriceFactor1 from a different row, which is also no good.
How it's supposed to work:
A row is created in table Orders. A third-party program then executes a stored procedure (Asp_BuildShipments) and displays the results once they have been inserted into table Shipments. This SP is meant to populate the table Shipments by pulling values from Orders, Clients, Drivers, Vehicles, Prices, Routes, and others. It's a long SP, and the array of tables is big and varied.
In table Orders:
PriceFactor1 | PriceFactor2 | PriceFactor3
12 | 10 | 8
In table Prices:
PriceFactor1 | PriceFactor2 | PriceFactor3
18 | 12 | 10
In a case such as this, the SP needs to recognize that no such rows exist in Prices and use a default value of 1 rather than skipping the row or pulling the price from a different row.
We've tried isnull(), CASE statements, and WHERE EXISTS, but to no avail.
The new SP is set based, and we want to leave it that way - the old one took minutes, the new one takes only a few seconds. But without passing row by row, we aren't sure how to check each individual Order to see if has a matching Price before building the row in Shipments.
I know there are details missing here, but I didn't want to write a 1,000 page question. If these details are insufficient, I'll post as much as I need to to help get your brains storming. Been stuck on this for a while now...
Thanks in advance :)
Could this be as simple as:
SELECT
O.Value1,...
CL.Value199,
isnull(P.PriceFactor1,1)
FROM Orders O
JOIN Clients CL on O.ClientNo = CL.ClientNo
JOIN Calc C on CL.CalcCode = C.CalcCode
LEFT JOIN Prices P on CL.PriceKey = P.PriceKey
AND O.PriceFactor1 = P.PriceFactor1
AND O.PriceFactor2 = P.PriceFactor2
AND O.PriceFactor3 = P.PriceFactor3
Richard Hansell's answer should work if the problem is as you suggest there are no matching rows in Prices. Since it's not working the problem is in the other joins.
One of these joins filters out data.
JOIN Clients CL on O.ClientNo = CL.ClientNo
JOIN Calc C on CL.CalcCode = C.CalcCode

Creating filter with SQL queries

I am trying to create a filter with SQL queries but am having trouble with numeric values linking to other tables.
Every time I try to link to another table, it takes the same record and repeats it for every element in the other table.
For example, here is query:
SELECT ELEMENTS.RID,TAXONOMIES.SHORT_DESCRIPTION,[type],ELEMENT_NAME,ELEMENT_ID,SUBSTITUTION_GROUPS.DESCRIPTION,namespace_prefix,datatype_localname
FROM ELEMENTS,SUBSTITUTION_GROUPS,TAXONOMIES,SCHEMAS,DATA_TYPES
WHERE ELEMENTS.TAXONOMY_ID = TAXONOMIES.RID AND ELEMENTS.ELEMENT_SCHEMA_ID = SCHEMAS.RID AND
ELEMENTS.DATA_TYPE_ID = DATA_TYPES.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = 0
The last line is the actual filtering criteria.
Here is an example result:
There should only be ONE result (Item has an RID of 0). But it's repeating a copy of the one record for every result inside the substitution groups table (there's 4).
Here is my database schema for reference. The lines indicate relationships between tables and the circles indicate the values I want:
You're forgot to join between ELEMENTS and SUBSTITUTION_GROUPS in your query.
SELECT
ELEMENTS.RID,TAXONOMIES.SHORT_DESCRIPTION,[type],ELEMENT_NAME,ELEMENT_ID,SUBSTITUTION_GROUPS.DESCRIPTION,namespace_prefix,datatype_localname
FROM
ELEMENTS,SUBSTITUTION_GROUPS,TAXONOMIES,SCHEMAS,DATA_TYPES
WHERE
ELEMENTS.TAXONOMY_ID = TAXONOMIES.RID AND ELEMENTS.ELEMENT_SCHEMA_ID = SCHEMAS.RID
AND ELEMENTS.DATA_TYPE_ID = DATA_TYPES.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = SUBSTITUTION_GROUPS.RID
AND ELEMENTS.SUBSTITUTION_GROUP_ID = 0

Update 1 field in a table from another field in a different table (OS400, not a 1 to 1 relationship)

Im trying to update a field in a table from another field in a different table.
The table being updated will have multiple records that need updating from 1 match in the other table.
Example, i have a 1 million row sales history file. Those million records have aproximately 40,000 different sku codes, each row has a date and time stamp. Each sku will have multiple records in there.
I added a new field called MATCOST (material cost).
I have a second table containing SKU and the MATCOST.
So i want to stamp every line in table 1 with the corresponding SKU's MATCOST in table2. I cannot seem to achieve this when its not a 1 to 1 relationship.
This is what i have tried:
update
aulsprx3/cogtest2
set
matcost = (select Matcost from queryfiles/coskitscog where
aulsprx3/cogtest2.item99 = queryfiles/coskitscog.ITEM )
where
aulsprx3/cogtest2.item99=queryfiles/coskitscog.ITEM
But that results in the SQL error: Column qualifier or table COSKITSCOG undefined and highlighting the q in the last reference to queryfiles/coskitscog.Item
Any idea's ?
Kindest Regards
Adam
Update: This is what my tables look like in principle. 1 Table contains the sales data, the other contains the MATCOSTS for the items that were sold. I need to update the Sales Data table (COGTEST2) with the data from the COSKITCOG table. I cannot use a coalesce statement because its not a 1 to 1 relationship, most select functions i use result in the error of multiple selects. The only matching field is Item=Item99
I cant find a way of matching multiple's. In the example we would have to use 3 SQL statements and just specify the item code. But in live i have about 40,000 item codes and over a million sales data records to update. If SQL wont do it, i suppose i'd have to try write it in an RPG program but thats way beyond me for the moment.
Thanks for any help you can provide.
Ok this is the final SQL statement that worked. (there were actually 3 values to update)
UPDATE atst2f2/SAP20 ct
SET VAL520 = (SELECT cs.MATCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
VAL620 = (SELECT cs.LABCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
VAL720 = (SELECT cs.OVRCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20),
WHERE ct.pnum20 IN (SELECT cs.ITEM
FROM queryfiles/coskitscog cs)
This more compact way to do the same thing should be more efficient, eh?
UPDATE atst2f2/SAP20 ct
SET (VAL520, VAL620, VAL720) =
(SELECT cs.MATCOST, cs.LABCOST, cs.OVRCOST
FROM queryfiles/coskitscog cs
WHERE cs.ITEM = ct.pnum20)
WHERE ct.pnum20 IN (SELECT cs.ITEM
FROM queryfiles/coskitscog cs)
Qualify the columns with correlation names.
UPDATE AULSPRX3/COGTEST2 A
SET A.matcost = (SELECT matcost
FROM QUERYFILES/COSKITSCOG B
WHERE A.item99 = B.item)
WHERE EXISTS(SELECT *
FROM QUERYFILES/COSKITSCOG C
WHERE A.item99 = C.item)
From UPDATE, I'd suggest:
update
aulsprx3/cogtest2
set
(matcost) = (select Matcost from queryfiles/coskitscog where
aulsprx3/cogtest2.item99 = queryfiles/coskitscog.ITEM)
where
aulsprx3/cogtest2.item99=queryfiles/coskitscog.ITEM
Note the braces around matcost.

outer query to list only if its rowcount equates to inner subquery

Need help on a query using sql server 2005
I am having two tables
code
chargecode
chargeid
orgid
entry
chargeid
itemNo
rate
I need to list all the chargeids in entry table if it contains multiple entries having different chargeids
which got listed in code table having the same charge code.
data :
code
100,1,100
100,2,100
100,3,100
101,11,100
101,12,100
entry
1,x1,1
1,x2,2
2,x3,2
11,x4,1
11,x5,1
using the above data , it query should list chargeids 1 and 2 and not 11.
I got the way to know how many rows in entry satisfies the criteria, but m failing to get the chargeids
select count (distinct chargeId)
from entry where chargeid in (select chargeid from code where chargecode = (SELECT A.chargecode
from code as A join code as B
ON A.chargecode = B.chargeCode and A.chargetype = B.chargetype and A.orgId = B.orgId AND A.CHARGEID = b.CHARGEid
group by A.chargecode,A.orgid
having count(A.chargecode) > 1)
)
First off: I apologise for my completely inaccurate original answer.
The solution to your problem is a self-join. Self-joins are used when you want to select more than one row from the same table. In our case we want to select two charge IDs that have the same charge code:
SELECT DISTINCT c1.chargeid, c2.chargeid FROM code c1
JOIN code c2 ON c1.chargeid != c2.chargeid AND c1.chargecode = c2.chargecode
JOIN entry e1 ON e1.chargeid = c1.chargeid
JOIN entry e2 ON e2.chargeid = c2.chargeid
WHERE c1.chargeid < c2.chargeid
Explanation of this:
First we pick any two charge IDs from 'code'. The DISTINCT avoids duplicates. We make sure they're two different IDs and that they map to the same chargecode.
Then we join on 'entry' (twice) to make sure they both appear in the entry table.
This approach gives (for your example) the pairs (1,2) and (2,1). So we also insist on an ordering; this cuts to result set down to just (1,2), as you described.