如何使用多个计数和sql server 2008的条件?

时间:2014-04-09 07:46:58

标签: sql sql-server

我有这两个查询

1

select CL_Clients.cl_id,CL_Clients].cl_name,COUNT(*) AS number_of_orders
from CL_Clients,CLOI_ClientOrderItems
where CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id

2

select CL_Clients.cl_id,count(cloi_current_status) as dis
from CLOI_ClientOrderItems,CL_Clients
where cloi_current_status]='12'
and CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id,CLOI_ClientOrderItems.cloi_current_status

我有这个专栏我需要把计数功能和条件放在哪里

[cloi_current_status] 166 三十 三十 三十 150 150 150 150 150 150 150

2 个答案:

答案 0 :(得分:0)

一切都取决于你想要获得的数据,但你可以这样做。

SELECT Column_x, Column_y, etc..
FROM ClL_Clients a
JOIN (select CL_Clients.cl_id,CL_Clients].cl_name,COUNT(*) AS number_of_orders
from CL_Clients,CLOI_ClientOrderItems
where CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id) b

on a.cl_id = b.cl_id

JOIN (select CL_Clients.cl_id,count(cloi_current_status) as dis
from CLOI_ClientOrderItems,CL_Clients
where cloi_current_status]='12'
and CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id,CLOI_ClientOrderItems.cloi_current_status) c

on a.cl_id = c.cl_id
Group by BLAH BLAH

希望这能让你朝着正确的方向前进。

答案 1 :(得分:0)

很简单,您只需封装查询并为其结果集添加别名,然后在常用列上的别名之间进行JOIN。 (在下面的查询中, 假设 您将通过客户端ID加入)

SELECT *
FROM (
    SELECT CL_Clients.cl_id,
        CL_Clients].cl_name,
        COUNT(*) AS number_of_orders
    FROM CL_Clients,
        CLOI_ClientOrderItems
    WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,
        CL_Clients.cl_id
    ) A
INNER JOIN (
    SELECT CL_Clients.cl_id,
        count(cloi_current_status) AS dis
    FROM CLOI_ClientOrderItems,
        CL_Clients
    WHERE cloi_current_status] = '12'
        AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,
        CL_Clients.cl_id,
        CLOI_ClientOrderItems.cloi_current_status
    ) B
    ON A.cl_id = B.cl_id
WHERE ...
GROUP BY ...

这将被视为单独的结果集,因此您也可以使用WHERE或仅GROUP BY过滤结果,就像在普通SELECT中一样。

<强>更新

要回答评论中的问题,当您加入两个具有相同值的列并使用

的表时

SELECT * FROM A INNER JOIN B *将显示联接返回的所有列,表示A中的所有列和B中的所有列,这就是您有重复列的原因。

如果要过滤返回的列,可以指定要返回的列。因此,在您的情况下,顶部SELECT *可以替换为

SELECT A.cl_id, A.cl_name, A.number_of_orders, B.dis因此,您的查询变为:

SELECT A.cl_id, A.cl_name, A.number_of_orders, B.dis
FROM (
    SELECT CL_Clients.cl_id,
        CL_Clients].cl_name,
        COUNT(*) AS number_of_orders
    FROM CL_Clients,
        CLOI_ClientOrderItems
    WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,
        CL_Clients.cl_id
    ) A
INNER JOIN (
    SELECT CL_Clients.cl_id,
        count(cloi_current_status) AS dis
    FROM CLOI_ClientOrderItems,
        CL_Clients
    WHERE cloi_current_status] = '12'
        AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
    GROUP BY CL_Clients.cl_name,
        CL_Clients.cl_id,
        CLOI_ClientOrderItems.cloi_current_status
    ) B
    ON A.cl_id = B.cl_id

更新#2

对于您的上一个问题,您需要在大查询结束时GROUP BY并使用HAVING条件,如下所示:

GROUP BY A.cl_id, A.cl_name, A.number_of_orders, B.dis
HAVING COUNT(cloi_current_status) > 100