각 상점의 제품 수/시스템 수를 찾으려고합니다. 표는 다음과 같다 :테이블을 조인 한 후 원래 수를 얻는 방법은 무엇입니까?
Store
Id
100
200
Customer
Id | dealerId
1 | 100
2 | 200
System
Id | CustomerId
20 | 2
30 | 2
40 | 2
50 | 1
Product
Id | SystemId
1000 | 20
2000 | 50
나는 얻기 위해 노력하고 있어요 :
storeId | Number of systems | number of products | average
100 | 1 | 1 | 1/1
200 | 3 | 1 | 1/3
내가이 쿼리를 작성했습니다. 적절한 수의 제품을 얻지 만 시스템 수는 내가 테이블에 합류하기 때문에 엉망입니다. 각 매장의 총 시스템 수를 구할 수있는 방법이 있습니까?
SELECT
s.Id as Store,
COUNT(Distinct SystemsIden) as NumOfSystems,
COUNT(distinct ProductIden) as NumOfProduct,
CAST(COUNT(distinct ProductIden)as float)/CAST(COUNT(Distinct SystemsIden) as
float) as average
FROM
Store s
INNER JOIN
(
Select systemsiden,CustomerIden,ProductIden, Customer.StoreId as
storeiden from
(
select Product.ID as ProductIden, System.Id as systemsiden, System.customerId as CustomerIden from product join Ssystem On System.Id = Product.SystemIdId
)
table1 join Customer
on Customer.Id = CustomerIden
)
table2 On s.Id = storeiden
GROUP BY
s.Id
? 십진법을 사용해야합니다. 수레는 대략적인 데이터 유형입니다. –
@Dan 기회는 mysql 또는 sql-server 중 하나를 사용하고있는 것입니다. 코드가 실제로 두 데이터베이스 모두에서 실행되어야하는 경우가 아니면 실제 용도로 사용되지 않으므로 다른 태그를 질문에서 제거하십시오. –