[英]Oracle 10g Select in Query
I would like to formulate a query that returns the vin
, customer_id
and model_id
where the same customer_id
has multiple records that have a model_id
of " toyota " from the CAR
table. 我想制定一个查询,该查询返回
vin
, customer_id
和model_id
,其中相同的 customer_id
具有多个记录,这些记录的CAR
表中的model_id
为“ toyota ”。 The vin
in the primary key and unique. vin
在主键上是唯一的。 Any ideas? 有任何想法吗? I'm using Oracle 10g.
我正在使用Oracle 10g。
CAR Table: 车表:
vin varchar (primary key)
customer_id varchar
model_id varchar
Data: 数据:
vin customer_id model_id
------------------------------------
222313XYZ, cust1234, toyota
123232SSS, cust1234, toyota
111111ERE, cust1111, bmw
999999LKO, cust1224, bmw
Oracle 11g R2 Schema Setup : Oracle 11g R2架构设置 :
CREATE TABLE car
(vin varchar2(9), customer_id varchar2(8), model_id varchar2(6))
;
INSERT ALL
INTO car (vin, customer_id, model_id)
VALUES ('222313XYZ', 'cust1234', 'toyota')
INTO car (vin, customer_id, model_id)
VALUES ('123232SSS', 'cust1234', 'toyota')
INTO car (vin, customer_id, model_id)
VALUES ('111111ERE', 'cust1111', 'bmw')
INTO car (vin, customer_id, model_id)
VALUES ('999999LKO', 'cust1234', 'bmw')
SELECT * FROM dual
;
Query 1 : in case you want to see all models whose owners also own more than one toyota 查询1 :如果您要查看其所有者也拥有多个丰田汽车的所有车型
select vin, customer_id, model_id from (
select vin, customer_id, model_id,
count( decode(model_id, 'toyota', 1) ) over (partition by customer_id) cnt
from car
)
where cnt > 1
| VIN | CUSTOMER_ID | MODEL_ID |
--------------------------------------
| 999999LKO | cust1234 | bmw |
| 222313XYZ | cust1234 | toyota |
| 123232SSS | cust1234 | toyota |
Query 2 : in case you want to see only toyotas whose owners own more than one of them: 查询2 :如果您只想查看所有者拥有不止一个的丰田汽车:
select vin, customer_id, model_id from (
select vin, customer_id, model_id,
count(*) over (partition by customer_id) cnt
from car
where model_id = 'toyota'
)
where cnt > 1
| VIN | CUSTOMER_ID | MODEL_ID |
--------------------------------------
| 123232SSS | cust1234 | toyota |
| 222313XYZ | cust1234 | toyota |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.