0

だから私は次のクエリを持っています

SELECT SUM(s.price) as "totalSales", 
       sum(CASE 
             WHEN er.verified AND er.price = 1.010101 THEN s.price 
             WHEN er.verified AND er.price > 1.010101 THEN er.price ELSE 0 END) as "totalRet", 
       c.company as "client"
FROM "Esns" e 
  JOIN "EsnsSalesOrderItems" es on es."EsnId" = e."id" 
  JOIN "SalesOrderItems" s on es."SalesOrderItemId" = s.id
  JOIN "SalesOrders" so on so.id = s."SalesOrderId"
  JOIN "Clients" c on so."ClientId" = c.id                    
  LEFT OUTER JOIN  "EsnsRmas"  er on er."EsnId" = e.id
GROUP BY c.id

ただし、es に複数の「EsnId」がある場合は複数回カウントされ、その逆も同様です。

すべてが 1 回だけカウントされるように構成するにはどうすればよいですか?

4

1 に答える 1

1
SELECT
    SUM(s.price) as "totalSales", 
    sum(CASE 
        WHEN er.verified AND er.price = 1.010101 THEN s.price 
        WHEN er.verified AND er.price > 1.010101 THEN er.price ELSE 0 END) as "totalRet", 
    c.company as "client"
FROM "Esns" e 
    JOIN (
        select distinct "EsnId"
        from "EsnsSalesOrderItems"
    ) es on es."EsnId" = e."id" 
    JOIN "SalesOrderItems" s on es."SalesOrderItemId" = s.id
    JOIN "SalesOrders" so on so.id = s."SalesOrderId"
    JOIN "Clients" c on so."ClientId" = c.id                    
    LEFT OUTER JOIN  "EsnsRmas"  er on er."EsnId" = e.id
GROUP BY c.id
于 2013-02-27T17:40:13.477 に答える