+--------------+--------------+------------+
| company_name | address_type | address |
+--------------+--------------+------------+
| Company A | Billing | 111 Street |
| Company A | Shipping | 111 Street |
| Company B | Billing | 222 Street |
| Company B | Shipping | 333 street |
| Company B | Shipping | 444 street |
+--------------+--------------+------------+
我有一张类似的表。
我需要的是所有公司的账单地址和送货地址不同的公司。
注意 - 每家公司只有一个帐单邮寄地址。但它可以有多个送货地址
这似乎是一个相当简单的查询,但我不仅能够得到它。
我的尝试 - 我尝试减去'来自Billing的所有送货地址,但没有输出。 明显没有帮助
查询:
select company_name
from tableA
where address_type='Billing'
and company_name not in (select to_char(company_name) from tableA where address_type='Shipping');
输出应公司B (因为它的结算和送货地址不同)
编辑1:尝试过Indra的查询,但它会永远运行。没有回复select A.* from company A inner join company B on A.company_Name = B.company_Name
and (A.address_type = 'Billing' and B.address_type = 'Shipping')
AND A.address <> B.address
答案 0 :(得分:1)
如何使用join
?以下显示了所有不同的对:
select tb.*, ts.*
from company tb join
company ts
on tb.company_name = ts.company_name and
ts.address_type = 'shipping' and
tb.address_type = 'billing' and
ts.address <> tb.address;
如果你只想要不同的公司:
select company_name
from company t
group by company_name
having count(distinct case when t.address_type = 'billing' then address end) = 1 and
count(distinct case when t.address_type = 'shipping' then address end) = 1 and
(max(case when t.address_type = 'billing' then address end) <>
max(case when t.address_type = 'shipping' then address end)
);
注意:这还会检查只有一个不同的结算和送货地址。
答案 1 :(得分:0)
使用以下查询
buildscript {
repositories {
...
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.google.gms:google-services:1.3.1'
}
}
答案 2 :(得分:0)
在这个SQL Fiddle example中,您可以找到另一家公司的其他方法:
+--------------+--------------+------------+
| company_name | address_type | address |
+--------------+--------------+------------+
| Company A | Billing | 111 Street |
| Company A | Shipping | 111 Street |
| Company B | Billing | 222 Street |
| Company B | Shipping | 333 street |
| Company B | Shipping | 444 street |
| Company C | Shipping | 555 street |
| Company C | Shipping | 666 street |
| Company C | Billing | 555 street |
| Company C | Billing | 666 street |
+--------------+--------------+------------+
POSTDATA :SQL小提琴适用于我。这就是所有代码:
架构:
CREATE TABLE companies
(
company_name VARCHAR2(40),
address_type VARCHAR2(40),
address VARCHAR2(40)
);
insert into companies values ('Company A','Billing','111 Street' );
insert into companies values ('Company A','Shipping','111 Street' );
insert into companies values ('Company B','Billing','222 Street' );
insert into companies values ('Company B','Shipping','333 street' );
insert into companies values ('Company B','Shipping','444 street');
insert into companies values ('Company C','Billing','555 Street' );
insert into companies values ('Company C','Billing','666 Street' );
insert into companies values ('Company C','Shipping','555 Street' );
insert into companies values ('Company C','Shipping','666 Street' );
Sentece:
SELECT DISTINCT(comp.company_name) FROM
(select company_name, address, count(*) regs
from companies
group by company_name, address) comp
WHERE MOD(comp.regs,2) = 1;
结果:
Company B