sql select rows with duplicate data - sql

I need help with this select statement in my relational table
(both supplier and products are composite primary key which uniquely identified each rows
Supplier Products
ABC Toys
ABC Snacks
ZXC Snacks
ZXC Food
QWE Toys
ABC Food
I need to find the supplier that does not supply toys
so i shoud only get ZXC
I try the following but it give me ABC, ZXC
select distinct Supplier
from table
where NOT (Products ='Toys');
(I am using oracle) how should my query be? Thanks

select distinct supplier
from table
where supplier not in (select supplier from table where products = 'Toys')

You can group by Supplier and use having clause evaluated to true if conditional count is equal to 0 (no matches within group):
select Supplier
from table
group by Supplier
having count(case when Products = 'Toys' then Products end) = 0

Another way to do it with NOT EXISTS
select distinct
supplier
from
table t1
where
not exists (select * from table t2 where t1.supplier = t2.supplier and t2.products = 'Toys')

Lots of ways to do this here's a couple. I've found exists and not exists to generally be the fastest but it always depends on indexes and your system. So look at execution plan for best use in your environment.
using a left join
SELECT T.SUPPLIER
FROM TABLE T
LEFT JOIN TABLE T2
on T.Supplier = T2.Supplier
and T2.Products = 'Toys'
where T2.Products is null
Using not exists.
SELECT supplier
FROM table T
WHERE NOT EXISTS (SELECT 1
FROM Table A
WHERE T.Supplier = A.Supplier and Products = 'Toys')

Related

join single value from one table to multiple rows table - Oracle

I have the following tables in Oracle:
Table 1 Table 2
AllCustomers ProductCode Customers
5200000 ABC 15265
DEF 156890
In Oracle, I want to join them both, like this:
Table 3
ProductCode Customers AllCustomers
ABC 15265 5200000
DEF 156890 5200000
How can I join these tables? As you can see, they do not have a key field to join. I just need to populate a third column in the new table with the same value in it, which would be the one from AllCustomers. Thanks in advance!
Maybe you can try Cross join
SELECT t2.*,t1.*
FROM Table1 t1 CROSS JOIN Table2 t2
You can achieve your goal without a join, like so:
SELECT ProductCode, Customers, (SELECT AllCustomers FROM Table1 WHERE ROWNUM = 1)
FROM Table2

Query a table that have 2 cols with multiple criteria

I have a table with the following structure and Example data:
Now I want to query the records that have value equals to # and #.
For example according to the above image, It should returns 1 and 2
id
-----
1
2
Also if the parameters were #, # and $ It should give us 1. Because only the records with id 1 have all the given values.
id
-----
1
You can use a group by and having to get the distinct Id's that contain a distinct count of the number of items you're looking for
SELECT Id
FROM Table
WHERE Value IN ('#','$')
GROUP BY Id
HAVING COUNT(DISTINCT Value) = 2
SELECT Id
FROM Table
WHERE Value IN ('#','$','#')
GROUP BY Id
HAVING COUNT(DISTINCT Value) = 3
SQL Fiddle you can use this link to test
There's several ways to do this.
The subquery method:
SELECT DISTINCT Id
FROM Table
WHERE Id IN (SELECT Id FROM Table WHERE Value = '#')
AND Id IN (SELECT Id FROM Table WHERE Value = '#');
The correlated subquery method:
SELECT DISTINCT t.Id
FROM Table t
WHERE EXISTS (SELECT 1 FROM Table a WHERE a.Id = t.Id and a.Value = '#')
AND EXISTS (SELECT 1 FROM Table b WHERE b.Id = t.Id and b.Value = '#');
And the INTERSECT method:
SELECT Id FROM Table WHERE Value = '#'
INTERSECT
SELECT Id FROM Table WHERE Value = '#';
Best performance will depend on RDBMS vendor, size of table, and indexes. Not all RDBMS vendors support all methods.
Maybe a multiple self join like this?
select
distinct t1.id
from
table t1
join table t2 on (t1.id=t2.id)
join table t3 on (t1.id=t3.id)
...
where
t1.value='#' and
t2.value='#' and
t3.value='$' and
...

How do I just get the first matching row?

I have a fairly complex SQL query - part of which requires to look up a company_ID value found in the first table to obtain the company_Name in the second table. The second table may have variants of the company name, but that is OK - I just need the first match.
So, tableA looks something like this (approx 2 dozen columns and many rows)
company_ID (CHAR(12))
161012348876
561254435253
103929478273
141567643542
tableB looks something like this
company_ID (Integer) Company_name
161012348876 Watson & Jones Ltd
161012348876 Watson and Jones
561254435253 Fictional Co. plc
103929478273 Made Up Corp.
161012348876 Watson Jones Ltd
141567643542 Thingymajig Gmbh.
This query will return multiple rows for 161012348876. What're good ways just to get one row returned for each matching company_id (i.e. 4 rows instead of 6)?
SELECT *, t2.company_name
FROM tableA t1
JOIN tableB t2 ON t1.company_id = cast(t2.company_id as CHAR(12))
I am using Teradata SQL.
Any help much appreciated.
SELECT *, t2.company_name
FROM tableA t1
JOIN tableB t2 ON t1.company_id = cast(t2.company_id as CHAR(12))
GROUP BY t1.company_id
Will return 1 row for each unique t1.company_id
The following query will get one Name for each company id. The Group by t2.company_id and MAX(t2.company_name) will get a unique name for each id and then join it with tableA.
SELECT t1.Company_ID, t2.company_name
FROM tableA t1
JOIN (SELECT t2.company_id , MAX(t2.company_Name) [aName]
FROM tableB t2 GROUP BY t2.company_id ) as t3
ON t1.company_id = cast(t3.company_id as CHAR(12))
Instead of user2989408's MAX subquery you can also do a
SELECT company_id , company_Name
FROM tableB
QUALIFY ROW_NUMBER() OVER (PARTITION BY company_id ORDER BY company_name) = 1
--if you don't care about MIN/MAX or want a more random result:
QUALIFY COUNT(*) OVER (PARTITION BY company_id ROWS UNBOUNDED PRECEDING) = 1
But assuming that *company_id* is the PI of tableB the MAX will probably perform better.

how to select only 1 unique data when joining two tables, if the table structure is one to many?

i am using an oracle database, i have two tables.
table A
primary key = productid
table B
references productid of table A
primary key = imageid
flow:
each product should have 4 images stored in the table B (mandatory)
problem:
there are some products that has only 2 or sometimes 3 or sometimes 1 image only
despite of the fact that the 4 images rule is mandatory based from code level.
Question:
how to count unique number of products that has images in table b?Because, if I do
select count(*) from tableA join tableB on tableA.productid = tableB.productid
the result is double, because it's a one to many...as in , one product has many images.
So let's say productID = 12345 has 4 images in table B, once I ran my query, the result is 4 , when i want to only get 1...so how?
SELECT Count(DISTINCT TableA.productid)
FROM TableA
JOIN TableB ON TableA.productid = TableB.productid;
Do a sub query with where exists
select count(*) from tableA
where exists (select 1 from tableB where tableA.productid = tableB.productid)
SELECT COUNT(*) FROM
(
select A.productId from tableA A join tableB B on A.productid = B.productid
GROUP BY A.productId
HAVING COUNT(B.imageId) > 1 ) T

SQLite select from mutliple tables based on column value

I have a table with columns id, product_type, product_id, quantity all as integer.
I need select data from another tables depending on product_type. For example. If product_type is 0 then select from tableA, if product_type is 1 then select from tableB etc. I tryied to find solution how to create select but unsuccessfully. Can someone help me please. I appreciate every help. Thank you.
Join the master table with the individual product tables, but use a left join and include the product_type = x filter in the join condition so that only the desired records are actually joined.
This will result in many NULL values; use coalesce to get a non-NULL value for the output:
SELECT sales.id,
sales.quantity,
sales.product_type,
coalesce(tableA.name, tableB.name) AS name,
coalesce(tableA.color, tableB.color) AS color,
tableA.numberOfAs -- is NULL for other product types
FROM sales
LEFT JOIN tableA ON sales.product_type = 0 AND
sales.product_id = tableA.product_id
LEFT JOIN tableB ON sales.product_type = 1 AND
sales.product_id = tableB.product_id
WHERE ...
Sounds like you need a CASE statement
SELECT
CASE
WHEN product_type = 1 THEN (SELECT column FROM table1 WHERE ...)
WHEN product_type = 2 THEN (SELECT column FROM table2 WHERE ...)
END
FROM table
Probably could be made more efficient by using JOINS but it would depend on your schema.