'SELECT IN' with item list containing duplicates - sql

I'm doing
SELECT Name WHERE Id IN (3,4,5,3,7,8,9)
where in this case the '3' Id is duplicated.
The query automatically excludes the duplicated items while for me would be important to get them all.
Is there a way to do that directly in SQL?

The query doesn't exclude duplicates, there just isn't any duplicates to exclude. There is only one record with id 3 in the table, and that is included because there is a 3 in the in () set, but it's not included twice because the 3 exists twice in the set.
To get duplicates you would have to create a table result that has duplicates, and join the table against that. For example:
select t.Name
from someTable t
inner join (
select id = 3 union all
select 4 union all
select 5 union all
select 3 union all
select 7 union all
select 8 union all
select 9
) x on x.id = t.id

Try this:
SELECT Name FROM Tbl
JOIN
(
SELECT 3 Id UNION ALL
SELECT 4 Id UNION ALL
SELECT 5 Id UNION ALL
SELECT 3 Id UNION ALL
SELECT 7 Id UNION ALL
SELECT 8 Id UNION ALL
SELECT 9 Id
) Tmp
ON tbl.Id = Tmp.Id

Related

oracle how to return a list and join to a table?

In oracle is it possible to join a static list to a table?
The list I have is something like this
ID
1
2
3
4
5
6
I don't want to create a table for this list
But then I want to join the list to an existing table that has the ID's in it... hoping to do a left join with the list
Is this possible?
You are lookig for a WITH clause that contains UNIONs of SELECT FROM DUAL.
Like :
WITH my_list AS (
select 'A' my_value from dual
UNION ALL select 'B' my_value from dual
UNION ALL select 'C' my_value from dual
)
SELECT
*
FROM
my_list
LEFT JOIN my_table ON my_table.my_field = my_list.my_value
;
You can generate the ID list in a CTE and then join it to whatever you want.
with id_list as (
select rownum as id
from dual
connect by level <= 6
)
select * from id_list;
ID
1
2
3
4
5
6
https://livesql.oracle.com/apex/livesql/s/hm2mczgx5udiig9vhryo86mfm

How to filter hardcoded list via table in DB2?

I am new to DB2 and facing a problem right now.
I have a hardcoded list of identityNo's and a table which has a column called identityNo. I want to find the identityNo's such that they will be in the hardcoded list, but not in the any row of the table.
Example:
List: 1, 2, 3, 4
select count(*) from myTable where identityNo = 1 => 3
select count(*) from myTable where identityNo = 2 => 0
select count(*) from myTable where identityNo = 3 => 1
select count(*) from myTable where identityNo = 4 => 0
Expected Result: 2 and 4 (any format acceptable)
In DB2, you can do:
select i.id, count(t.identityNo)
from (select 1 as id from sysibm.sysdummy1 union all
select 2 from sysibm.sysdummy1 union all
select 3 from sysibm.sysdummy1 union all
select 4 from sysibm.sysdummy1
) i left join
mytable t
on t.identityNo = i.id
group by i.id
order by i.id;
Try
VALUES (1),(2),(3),(4) MINUS SELECT identityNo FROM myTable
i.e.
create table myTable(identityNo int);
insert into myTable values (1),(3);
VALUES (1),(2),(3),(4) MINUS SELECT identityNo FROM myTable;
1
-----------
4
2
2 record(s) selected.

Sql in oracle to find out missing records from its distinct values

I am sorry , this one is not working... May be I should have clarified this earlier. The values A,B,C,D etc... Are the Distinct values for CODE in the Table. There are several hundreds of IDs in the table and each ID can have one to many Code values. In the above example assume that there are 5 distinct values of Code from table A. There are 3 IDs and each ID is associated in Table A as follows
ID Code
1 A
1 B
1 C
2 D
2 A
3 B
3 C
4 A
4 B
4 C
4 D
4 E
As you see above there are several IDs associated with different Code values. I need the result as follows
ID CODE
1 D
1 E
2 B
2 C
2 E
3 A
3 D
3 E
ID 4 should not return anything because it contain all possible Codes (in this case A,B,C,D,E)
First you should take distinct values for both column in different sub-query, second cross join them - that will give you all possible combination,
finally exclude combination which are already presnet
select *
from
(select distinct ID
from your_table) ytI, /* this sub-query will return all possible ID */
(select distinct code
from your_table) ytc /* this sub-query will return all possible code */
where (ytI.ID,ytc.Code) /* there will be cross-join as there are no join condition between first two tables*/
not in /* exclude those records which are already present */
(select id,code
from your_table yt_i)
try this
select T2.ID, T1.missing_value
from
(
select 'A' missing_value from dual UNION
select 'B' from dual UNION
select 'C' from dual UNION
select 'D' from dual UNION
select 'E' from dual
) T1,
(
select distinct id from MYTABLE
) T2
WHERE NOT EXISTS
(
SELECT * FROM MYTABLE M WHERE M.CODE = T1.missing_value and M.ID = T2.ID
)
ORDER BY T2.ID, T1.missing_value

Union Select Only One Row

I Have a query with Two Select Clause combines with UNION.I want to select only top first row. How can i do that Using Union ?
Select Fault,OccurredOn From ATMStatus Where Ticket=189703 // This Will retrieve single record as the primary key is applied
Union
Select Fault,OccurredOn From ATMStatusHistory Where Resolved=0 AND Ticket=189703 Order By OccurredOn Desc
select top 1 * from
(
Select Fault,OccurredOn
From ATMStatus
Where Ticket=189703
Union
Select Fault,OccurredOn
From ATMStatusHistory
Where Resolved=0 AND Ticket=189703
) x
Order By OccurredOn Desc
This returns 2 rows:
select 1 as id
union
select 2 as id
This returns 1 row:
select top 1 * from (
select 1 as id
union
select 2 as id
) as x
order by id

mysql - order by frequency with multiple columns

I have a table with 5 columns:
tag 1
tag 2
tag 3
tag 4
tag 5
If I want to show results ordered by the popularity(frequency) of those tags, what kind of query would i use?
Because the table isn't normalized, you'll have to flatten it first:
SELECT a.column, a.tag1 AS tag
FROM TABLE a
UNION ALL
SELECT b.column, b.tag2
FROM TABLE b
UNION ALL
SELECT c.column, c.tag3
FROM TABLE c
UNION ALL
SELECT d.column, d.tag4
FROM TABLE d
UNION ALL
SELECT e.column, e.tag5
FROM TABLE e
...before you can count them:
SELECT t.tag, COUNT(*) tag_popularity
FROM (SELECT a.column, a.tag1 AS tag
FROM TABLE a
UNION ALL
SELECT b.column, b.tag2
FROM TABLE b
UNION ALL
SELECT c.column, c.tag3
FROM TABLE c
UNION ALL
SELECT d.column, d.tag4
FROM TABLE d
UNION ALL
SELECT e.column, e.tag5
FROM TABLE e) x
GROUP BY x.tag
ORDER BY tag_popularity DESC