Search Postgresql Archives
Query Improvement??
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
- Subject: Query Improvement??
- From: Bret Stern <bret_stern@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 05 Nov 2017 13:39:34 -0800
- Organization: Machine Management
- Reply-to: bret_stern@xxxxxxxxxxxxxxxxxxxxx
Goal is to return all vendors which exist in all three companies
I think I got lucky figuring this out. Is there an obviously better way?
combined_item_master looks like this:
company_code character varying(10) NOT NULL,
primary_vendor_no character varying(7)
..more fields
data looks like this:
company_code | primary_vendor
AAA 003
BBB 004
CCC 001
CCC 004
AAA 123
BBB 123
CCC 123
BBB 003
Query returns all primary_vendor_no (as vendor_locations) which exist in all three companies
results:
vendor_locations
123
Here's the query
select primary_vendor_no, count(primary_vendor_no) as vendor_locations
from
(
SELECT distinct primary_vendor_no, company_code
FROM combined_item_master
group by primary_vendor_no, company_code
) as a
group by primary_vendor_no
having count(primary_vendor_no)=3
order by vendor_locations DESC, primary_vendor_no
Thanks
Bret
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]
[Postgresql Jobs]
[Postgresql Admin]
[Postgresql Performance]
[Linux Clusters]
[PHP Home]
[PHP on Windows]
[Kernel Newbies]
[PHP Classes]
[PHP Books]
[PHP Databases]
[Postgresql & PHP]
[Yosemite]