Oracle: Update field to 'T' based on criteria in subquery - sql

I have a table that I'm attempting to add a boolean flag field to based on an aggregate query. Trying to put this out in the simplest form without providing customer information, I'll call the table MyTable with fields An, Ver, Co, and Rep.
MyTable
An Ver Co Rep NewFlag
A 1 Aa T
A 1 Ab F
A 1 Ac T
B 1 Ba F
B 1 Bb F
B 1 Bc T
C 1 Ca F
C 1 Cb T
C 1 Cc F
I want to set NewFlag = 'T' where there for a grouping of An, Ver, Co, there is only one Rep = 'T', otherwise it can remain null.
I've got a query that identifies the An, Ver, Co where the count of Reps='T' = 1, and want to use a result site like the following as my criteria to update the NewFlag field
QueryResults
An Ver Co
B 1 Bc
C 1 Cb
In a way, it would be something like this:
Update MyTable
Set NewFlag = 'T'
where
MyTable.An = QueryResults.An,
MyTable.Ver = QueryResults.Ver,
MyTable.Co = QueryResults.Co
QueryResults being the result set of my query that identifies those records with only one count of Rep = 'T'.
Outside of using a script to loop through and update records one-by-one, is there syntax where I can just insert my query in to update those particular fields?

You want a correlated subquery or in. Something like this:
Update MyTable
Set NewFlag = 'T'
where (An, Ver, Co) in (select An, Ver, Co
from <your query>
);

MERGE is perfect for this kind of assignment:
merge into mytable m
using ( select an, ver, co
from mytable
where rep = 'T'
group by an, ver, co
having count(*) = 1
) x
on ( m.an = x.an and m.ver = x.ver and m.co = x.co )
when matched then update set newflag = 'T'
;

Related

How can I query data from one table by using multiple conditions and by looking for a value in another table

I have three ORACLE SQL tables where I have to query DATA_TABLE and pull dataid's whose attr is 2000 and value is cat and also the value for attr = 2001 has to be available in NAME_TABLE oldname column and I have to display Name from DOC_TABLE(using dataid's we got from above) and Code from Name_Table
From the above table my output has to be
Name,Code
FirstName, DG
Because dataid 1 in DATA_TABLE for attr 2000 its value is Cat and for attr 2001 its value is dog which is available in NAME_TABLE oldname column, As Name where dataid=1 in DOC_TABLE is FirstName and code for dog in NAME_TABLE is DG
I would phrase your query as follows:
select do.name, na.code
-- start the join here
from doc_table do
-- pull dataid's from DATA_TABLE whose attr is 2000 and value is cat
inner join data_table da on da.dataid = do.dataid and da.attr = 2000 and da.value = 'cat'
-- pull code from NAME_TABLE
inner join name_table na on na.oldname = da.value
-- the value for attr = 2001 has to be available in NAME_TABLE
where exists(
select 1
from data_table da1
inner join name_table na1 on na1.oldname = da1.value
where da1.dataid = do.dataid and da1.attr = 2001
)
You can use analytical function as following:
Select distinct name, code from
(select do.name, na.code,
Count(case when da.value='cat' and da.attr=2000 then 1 end)
Over (Partition by do.dataid) as cat_cnt,
Count(case when na.code is not null and da.attr=2001 then 1 end)
Over (Partition by do.dataid) as code_cnt
from doc_table do
Left join data_table da on da.dataid = do.dataid
Left join name_table na on na.oldname = da.value)
Where cat_cnt > 0 and code_count > 0;
Cheers!!

sql function case returns more than one row

Going to use this query as a subquery, the problem is it returns many rows of duplicates. Tried to use COUNT() instead of exists, but it still returns a multiple answer.
Every table can only contain one record of superRef.
The below query I`ll use in SELECT col_a, [the CASE] From MyTable
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = myTable.sysno AND A_specAttr = 'value')
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo WHERE C_superRef = myTable.sysno AND b_type = 2)
THEN 2
ELSE (SELECT C_intType FROM C
WHERE C_superRef = myTable.sysno)
END
FROM A, B, C
result:
3
3
3
3
3
3...
What if you did this? Because Im guessing you are getting an implicit full outer join A X B X C then running the case statement for each row in that result set.
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM ( SELECT COUNT(*) FROM A ) --This is a hack but should work in ANSI sql.
--Your milage my vary with different RDBMS flavors.
DUAL is what I needed, thanks to Thorsten Kettner
SELECT CASE
WHEN
EXISTS (SELECT 1 FROM A WHERE
A_superRef = 1000001838012)
THEN 3
WHEN EXISTS (SELECT 1 FROM B
INNER JOIN С ON С_ReferenceForB = B_sysNo AND C_superRef = 1000001838012 )
THEN 2
ELSE (SELECT C_type FROM C
WHERE C_superRef = 1000001838012)
END
FROM DUAL

Selective update in SQL Server

I've created a junction table like this one:
http://imageshack.us/scaled/landing/822/kantotype.png
I was trying to figure out a query that could able to select some rows - based on the PokémonID - and then updating only the first or second row after the major "filtering".
For example:
Let's suppose that I would like to change the value of the TypeID from the second row containing PokémonID = 2. I cannot simply use UPDATE KantoType SET TypeID = x WHERE PokémonID = 2, because it will change both rows!
I've already tried to use subqueries containing IN,EXISTS and LIMIT, but with no success.
Its unclear what are your trying to do. However, you can UPDATE with JOIN like so:
UPDATE
SET k1.TypeID = 'somethng' -- or some value from k2
FROM KantoType k1
INNER JOIN
(
Some filtering and selecting
) k2 ON k1.PokémonID = k2.PokémonID
WHERE k1.PokémonID = 2;
Or: if you want to UPDATE only the two rows that have PokémonID = 2 you can do this:
WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY TypeID) rownum
FROM KantoType
WHERE PokemonID = 2
)
UPDATE c
SET c.TypeID = 5
FROM CTE c
WHERE c.rownum = 1;
SQL Fiddle Demo
I can suggest something like this if you just need to update a single line in your table:
UPDATE kantotype
SET
type = 2
WHERE pokemon = 2
AND NOT EXISTS (SELECT * FROM kantotype k2
WHERE kantotype.type > k2.type
AND kantotype.pokemon = k2.pokemon)
It would be easier to get the first or last item of the table if you had unique identifier field in your table.
Not sure even if you are trying to update the row with PokemenID =2 by doing a major filtering on TypeID... So just out of assumptiong (big one), you can give a try on Case
UPDATE yourtable a
LEFT JOIN youtable b on a.pokeid = b.pokeid
SET a.typeid = (CASE
WHEN a.typeid < b.typeid THEN yourupdatevalue
WHEN a.typeid > b.typeid THEN someothervalue
ELSE a.typeid END);
If you know the pokemon ID and the type id then just add both to the where clause of your query.
UPDATE KantoType
SET TypeID = x
WHERE PokémonID = 2
AND TypeID=1
If you don't know the type ID, then you need to provide more information about what you're trying to accomplish. It's not clear why you don't have this information.
Perhaps think about what is the unique identifier in your data set.

SQL query get record table A based on parentid Table B

[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned out I also needed a fourth table / view)]"
SELECT A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM VIEWA AS A
INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID
AND B.Name = N'Collect' AND DAPR.Value = 1
INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID
AND C.Name='Assemble'
AND C.Value = 1
INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId
AND D.Name = 'IndexY'
WHERE (A.SalesID = #SalesID)
[UPDATE: I've made a mistake assuming IndexY/Color and ProdId where fields Table A is some kind of parameter / property table with only 5 columns ID - NAME - VALUE - ARTICLERESULTID - PRODID. IndexY is a value of the Name field..]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
ID Name Value ArticleResultID ProdID Color
1 Operation Collect 110 10 BLACK
2 IndexY 10 110 10 -
3 Operation Collect 101 11 WHITE
Table B
ID ParentID Name Value
101 110 Assemble 1
101 100 Assemble 0
Steps:
Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A
In the above scenario thats ORG_A.ArticleResultID = 11 --> B.ParentID = 110 --> NEW_A.ARTICLERESULTID = 110 --> (IndexY - Value - 10)
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
[OLD DESCRIPTION:]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
Name Value ArticleResultID IndexY Color ProdID
Operation Collect 110 10 - 0
Operation Collect 101 _ Black 100
Table B
ID ParentID Name Value Dx Dy
101 110 Assemble 1 1000 500
101 100 Assemble 0 400 300
I want to fetch all records from A where NAME equals 'operation', VALUE equals 'Collect' and PRODID = '100' but I also want (here's my problem) the IndexY value of the record in Table A with the PARENTID in Table B which joins on TABLE B.ID = A.ArticleResultID AND Name = 'Assemble' and VALUE = '1'
In the above scenario thats ParentID 110 which gives me the record in Table A with ArticleResultID 110 with IndexY (10).
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
Since your requirements are not the clearest. How about this:
SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
Here is a sqlfiddle with a working version
If you just want the record with the 110 articleresultid, then:
SELECT a2.*
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN join ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
see the sqlfiddle for the second example
Does this work for you?
SELECT a.*,c.IndexY
FROM [TableA] a
JOIN [TableB] b ON a.ArticleResultId = b.id
AND b.Name ='Assemble'
AND b.Value = 1
JOIN [TableA] c ON b.ParentId = c.ArticleResultID
WHERE a.name = 'Operation'
AND a.Value = 'Collect'
AND a.ProdId = 100

Using (IN operator) OR condition in Where clause as AND condition

Please look at following image, I have explained my requirements in the image.
alt text http://img30.imageshack.us/img30/5668/shippment.png
I can't use here WHERE UsageTypeid IN(1,2,3,4) because this will behave as an OR condition and fetch all records.
I just want those records, of first table, which are attached with all 4 ShipmentToID .
All others which are attached with 3 or less ShipmentToIDs are not needed in result set.
Thanks.
if (EntityId, UsageTypeId) is unique:
select s.PrimaryKeyField, s.ShipmentId from shipment s, item a
where s.PrimaryKeyField = a.EntityId and a.UsageTypeId in (1,2,3,4)
group by s.PrimaryKeyField, s.ShipmentId having count(*) = 4
otherwise, 4-way join for the 4 fields,
select distinct s.* from shipment s, item a, item b, item c, item d where
s.PrimaryKeyField = a.EntityId = b.EntityId = c.EntityId = d.EntityId and
a.UsageTypeId = 1 and b.UsageTypeId = 2 and c.UsageTypeId = 3 and
d.UsageTypeId = 4
you'll want appropriate index on (EntityId, UsageTypeId) so it doesn't hang...
If there will never be duplicates of the UsageTypeId-EntityId combo in the 2nd table, so you'll never see:
EntityUsageTypeId | EntityId | UsageTypeId
22685 | 4477 | 1
22687 | 4477 | 1
You can count matching EntityIds in that table.
WHERE (count(*) in <tablename> WHERE EntityId = 4477) = 4
DECLARE #numShippingMethods int;
SELECT #numShippingMethods = COUNT(*)
FROM shippedToTable;
SELECT tbl1.shipmentID, COUNT(UsageTypeId) as Usages
FROM tbl2 JOIN tbl1 ON tbl2.EntityId = tbl1.EntityId
GROUP BY tbl1.EntityID
HAVING COUNT(UsageTypeId) = #numShippingMethods
This way is preferred to the multiple join against same table method, as you can simply modify the IN clause and the COUNT without needing to add or subtract more tables to the query when your list of IDs changes:
select EntityId, ShipmentId
from (
select EntityId
from (
select EntityId
from EntityUsage eu
where UsageTypeId in (1,2,3,4)
group by EntityId, UsageTypeId
) b
group by EntityId
having count(*) = 4
) a
inner join Shipment s on a.EntityId = s.EntityId