I have really basic knowledge in SQL and this seems like a task that requires a complex query to avoid doing multiple queries in a program loop.
I have a table such as the following with 'filename' as UNIQUE column:
title subtitle filename comment selected
abc 123 f1.txt abc 0
xyz 999 f2.txt bla 0
abc 123 f3.txt ppp 0
poc 232 f4.txt ppp 0
xyz 220 f5.txt ppp 0
xyz 999 f6.txt ppp 0
I need to update the 'selected' column to 1 for only a single row (doesn't matter which) for each unique (title, subtitle) pair. This is how the table should be after the query is processed:
abc 123 f1.txt abc 1
xyz 999 f2.txt bla 1
abc 123 f3.txt ppp 0
poc 232 f4.txt ppp 1
xyz 220 f5.txt ppp 1
xyz 999 f6.txt ppp 0
What is the best way to achieve this?
First, get the IDs of those unique rows:
SELECT MIN(rowid) -- or whatever your primary key is
FROM MyTable
GROUP BY title, subtitle
(It does not matter whether you use MIN or MAX.)
Then update those rows:
UPDATE MyTable
SET selected = 1
WHERE rowid IN (SELECT MIN(rowid)
FROM MyTable
GROUP BY title, subtitle)
In SQLite, I think you can do this:
update table
set selected = 1
where not exists (select 1
from table t2
where t2.name < t.name or (t2.name = t.name and t2.title < t.title)
);
Related
I have the below sample data set and I'm trying to come up with a query to find one or more duplicate rows from the same table
TABLE A: with 2 columns as below
CODE_NAME, RESULT
ABC 1
BBC 1
ZZZ 5
ZZZ 6
ZZZ 7
KBC 2
ZBC 2
CCC 2
XYZ 3
MNC 4
And my output should give all the unique rows with duplicate values in the result column such as below
CODE_NAME, RESULT
ABC 1
BBC 1
KBC 2
ZBC 2
CCC 2
i tried below but its not giving me correct result
select A t1, A t2
where A.result = b.result
and a.code_name <> b.code_name
Appreciate other suggestions.
You can use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.result = t.result and t2.code_name <> t.code_name
);
For performance on a large dataset, you want an index on (result, code_name).
You might find it more convenient to have one row per duplicated result:
select result,
listagg(code_name, ',') within group (order by code_name)
from t
group by result
having count(*) > 1;
I'm having a trouble writing a query in ORACLE. I have a Table that contains values. for example:
ID quantity partID
123 50 10
100 20 10
100 30 11
123 null 8
456 null 100
789 25 123
456 50 9
I want to get all rows that has same ID but quantities to be 50 and null (exact same pairs of 50 and null only). for the given example I would like to get:
ID quantity partID
123 50 10
123 null 8
456 50 9
456 null 100
I tried inner join but it doesn't provide the exact output as expected.
You may try :
select ID, quantity, partID
from tab
where ID in
(
select ID
from tab
where nvl(quantity,50)=50
group by ID
having count(distinct nvl(quantity,0) )>1
);
ID QUANTITY PARTID
123 50 10
123 (null) 8
456 (null) 100
456 50 9
SQL Fiddle Demo
P.S. you may get the same results by commenting out having count(ID)=2 also but for those cases there may not exist one of 50 or null for values of quantity.
You can use exists:
select t.*
from t
where (t.quantity = 50 and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value is null)
) or
(t.quantity is null and
exists (select 1 from t t2 where t2.id = t.id and t2.partid = t.partid and t2.value = 50)
) ;
I have two tables, I want to match values in both tables/pairing fields as below
In table 1 records 1 and 2 should be updated in MatchRef field as matched
In table 2 records 1 and 2 should be updated in matchref field as matched
but in Table 1 the record 3 should not updated as matched as there is no matching field in table 2
Table 1
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
3 AAA 100
4 BBB 100
Table 2
ID MatchField MatchValue MatchRef
1 AAA 100
2 AAA 100
4 BBB 100
Here ID is not an unique filed, its just auto number.
How to do it in Ms Access? There will be 600 000 records on average in each table and working on a Citrix environment.
Here is how you would find matching records as far as I can tell --
SELECT
*
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
AND T1.MATCHFIELD = T2.MATCHFIELD
I have a table with the following structure:
Id MemberId Field1 Field2 Data
--------------------------------------------------
1 1 12 abc 1232
2 2 13 asl 234
3 2 12 abc 2345
4 1 3 sd sfsd
5 1 5 45r ffgf
Given parameters member1 and member2, I have to return all id's from member1 that don't have the same values in Field1 and Field2 with member2.
Output example:
member1 = 1
member2 = 2
Expected output:
4
5
Because the the first record and the third record have the same values for Field1 and Field2.
How to achieve this?
I'm using SQL Server 2014.
Edit: I am not allowed to use cursors and temp tables (like #tempTable), I can use only table variables
If I understand correctly, you can use not exists:
select t.*
from t
where not exists (select 1
from t t2
where t2.member1 = t.member1 and
t2.id <> t.id and
(t2.field1 = t.field1 or t2.field2 = t.field2)
);
I have a table like this on SQL Server:
code description percentage
123 abc 1
123 oke 0
123 cfd 0
234 kde 2
234 kfc 0
234 kfc 0
How can I update the description of all '0 percentage' records to non-zero percentage record for each code group? e.g. the result I'm after is:
code description percentage
123 abc 1
123 abc 0
123 abc 0
234 kde 2
234 kde 0
234 kde 0
Update T1 set description = T2.Description from YourTable T1
inner join
( select code, description from yourTable group by code, description
where percentage <> 0 ) T2 on T1.Code = T2.Code
where T1.Code = 0
Edited
This consider that each group has only zero on only one another description.
Try
UPDATE Table
SET description=(SELECT TOP 1 description
FROM Table t
WHERE t.code = Table.code AND percentage<>'0')
WHERE percentage='0'