3

I have a query, I need to get a row only if all the ID match in WHERE clause

Here is the query

SELECT CompanyId
FROM vendors
WHERE VendorId
IN (
'306145244', '1011073437'
)

I want all the CompanyId which has both vendorId '306145244' and '1011073437'

If one of the VendorId match, I don't want that companyId. Both VendorId should be matched

While grouping I need 2 rows like below

company Id   Vendor Id
1             306145244
1             1011073437
2             306145244
2             306145244

If possible I want this query in ORACLE.

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
Sahal
  • 4,046
  • 15
  • 42
  • 68

5 Answers5

2
SELECT  CompanyId
FROM    vendors
WHERE   VendorId IN ('306145244', '1011073437')
GROUP   BY CompanyId
HAVING  COUNT(*) = 2

OTHER LINK:


if vendorID is not unique for every CompanyId, a DISTINCT keyword is need to count only unique values,

SELECT  CompanyId
FROM    vendors
WHERE   VendorId IN ('306145244', '1011073437')
GROUP   BY CompanyId
HAVING  COUNT(DISTINCT VendorId) = 2
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • I have edited the question. Please check it. Your query will give only one row for vendors. I need 2 rows for ('306145244', '1011073437') – Sahal Apr 25 '13 at 06:40
0

You might need to take a look at ALL.

SELECT CompanyId
FROM vendors
WHERE VendorId = ALL (
    query to get numbers.
)

http://dev.mysql.com/doc/refman/5.5/en/all-subqueries.html

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0
SELECT `CompanyId` , count( `CompanyId` )
FROM `vendors`
WHERE VendorId
IN (
'306145244', '1011073437'
)
GROUP BY `CompanyId`
HAVING count( `CompanyId` ) = 2;

Use this, this is working for me and tested.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • I have edited the question. Please check it. Your query will give only one row for vendors. I need 2 rows for ('306145244', '1011073437') – Sahal Apr 25 '13 at 06:41
  • In this case I will get only one record for company, I need 2 record see the question – Sahal Apr 25 '13 at 06:51
0

In SQL Server what we can do is:

SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('306145244')
intersect
SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437')

But MYSQL doesn't support intersect. Hence we have to approach alternative to intersect in MYSQL

select distinct CompanyId
FROM vendors
where vendorId IN ('306145244')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437'))

Edit

select  CompanyId,VendorId
FROM vendors
where vendorId IN ('306145244')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('1011073437'))
union
select  CompanyId,VendorId
FROM vendors
where vendorId IN ('1011073437')
and CompanyId IN
(SELECT CompanyId
FROM vendors
WHERE VendorId
IN ('306145244'))

SQL Fiddle

Community
  • 1
  • 1
Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
0

Try this :

SELECT CompanyId,VendorId
FROM vendors
WHERE VendorId
IN (
select VendorId from vendors
)
order by CompanyId asc;

Hope it will help you.

JDGuide
  • 6,239
  • 12
  • 46
  • 64