SQL聚合调用

SQL聚合调用,sql,postgresql,Sql,Postgresql,我有两张桌子 1) 顾客 2) 产品 我想要一个类似以下内容的查询: selectname from customer,selectcount(*)from products where state='SHIPPED',selectcount(*)from product where state='PENDING'for all developers,这将产生以下提到的结果: name | count_shipped | count_pending -------+---

我有两张桌子

1) 顾客

2) 产品

我想要一个类似以下内容的查询:

selectname from customer,selectcount(*)from products where state='SHIPPED',selectcount(*)from product where state='PENDING'for all developers
,这将产生以下提到的结果:

   name  | count_shipped | count_pending      
  -------+---------------+---------------
    xyz1 | 2             | 2
    xyz2 | 1             | 0
    xyz3 | 0             | 0
    xyz4 | 0             | 0
    xyz5 | 0             | 0

你也可以使用COUNT

SELECT c.name
     , COUNT(CASE WHEN state = 'shipped' THEN 'foo' END) count_shipped 
     , COUNT(CASE WHEN state = 'pending' THEN 'foo' END) count_pending 
  FROM customer c 
  LEFT
  JOIN products p  
    ON p.customer = c.id 
 GROUP  
    BY c.id;

你在使用Postgres和MySQL吗?这需要在两个DBMS上都运行吗?那么为什么要用MySQL标记它?以防万一,如果有人知道如何使用MySQL,我可以告诉他们如何将其映射到Postgres..:D@yrameshrao这就是一般sql的sql标记
   name  | count_shipped | count_pending      
  -------+---------------+---------------
    xyz1 | 2             | 2
    xyz2 | 1             | 0
    xyz3 | 0             | 0
    xyz4 | 0             | 0
    xyz5 | 0             | 0
SELECT
    c.id , 
    c.name ,
    SUM(IF p.state = 'Shipped',1,0) AS count_shipped,
    SUM(IF p.state = 'Pending',1,0) AS count_pending
FROM Customer AS c
LEFT JOIN Products AS p ON p.customer = c.id
GROUP BY c.id
select c.name,
       sum(case when p.state = 'Pending' then 1 else 0 end) as count_pending,
       sum(case when p.state = 'Shipped' then 1 else 0 end) as count_shipped
from customer c 
   left join products p on p.customer = c.id
group by name;
SELECT c.name
     , COUNT(CASE WHEN state = 'shipped' THEN 'foo' END) count_shipped 
     , COUNT(CASE WHEN state = 'pending' THEN 'foo' END) count_pending 
  FROM customer c 
  LEFT
  JOIN products p  
    ON p.customer = c.id 
 GROUP  
    BY c.id;