0

好的,所以在下面的语句中,我试图将第一个pi.planinfoid选择作为第一个字段,并使其成为一个动态变量,以代替1736您在用作附加字段的两个选择语句中看到的。

即使值为 0 或 null,我还需要它仍然返回由两个 select 语句生成的两个字段。

如果有人能解决这个问题,我会提前感谢。

SELECT  pi.planinfoid,
        pi.description,
        count(DISTINCT p.planid) AS total,
        sum(dm.debitamount) AS Num,
        pi.minperiod,
        (
            SELECT count(p.planid)
            FROM plans p,
                    planinfo pi
            WHERE pi.planinfoid = p.planinfoid
                    AND pi.planinfoid = '1736'
                    AND p.closed = 0
        ) AS opened,
        (
            SELECT count(p.planid)
            FROM plans p,
                    planinfo pi
            WHERE pi.planinfoid = p.planinfoid
                    AND pi.planinfoid = '1736'
                    AND p.closed = 1
        ) AS closedd
FROM    planinfo pi, plans p, debitmovements dm
WHERE   pi.price > 0
        AND p.planinfoid = pi.planinfoid
        AND dm.planid = p.planid
        AND p.servicestart BETWEEN '2012-01-01' AND '2013-01-01'
GROUP BY    pi.description,
            pi.minperiod,
            pi.planinfoid
ORDER BY total DESC

那跑了,但无论如何似乎打开和关闭都与总数无关,

planid  plan descrip   min      TOTAL   PRICE   OPEN    CLOSE
1736    Additional IP   1   146 1926    101 545

从技术上讲,如果在该时间段内售出的总数为 146,则您不能有 101 个仍然打开和 545 个关闭,也许它的非拉数仍然打开和关闭仅在那个时间段打开的那些。146 open 的 uniqe 字段将是 p.planid,或者每个人基本上都有一个唯一的 p.planid,但到目前为止做得很好。

4

2 回答 2

1

尝试这个:

SELECT
  pi.planinfoid, 
  pi.description, 
  pi.minperiod,
  count(DISTINCT p.planid) AS total,
  sum(dm.debitamount) AS Num, 
  SUM(CASE WHEN p.closed = 0 THEN 1 ELSE 0 END) AS opened,
  SUM(CASE WHEN p.closed = 1 THEN 1 ELSE 0 END) AS closedd
FROM planinfo pi  
INNER JOIN plans           p ON p.planinfoid = pi.planinfoid
INNER JOIN debitmovements dm ON dm.planid    = p.planid 
WHERE pi.price > 0
  AND p.servicestart BETWEEN '2012-01-01' 
                         AND '2013-01-01'
GROUP BY pi.description,
         pi.minperiod,
         pi.planinfoid 
ORDER BY total DESC;

我在这里所做的是:

  • 我使用了隐式JOIN语法,即 ANSI-92 SQL 标准之一,而不是旧的。
  • COUNT()用两个CASE表达式替换了两个相关的子查询。
  • 我移到pi.planinfoid = 1736子句WHERE,你可以把它放在CASE表达式条件中。
  • 您将需要传递planinfoid您正在寻找的而不是@value.
于 2013-01-10T15:09:14.317 回答
0

我很难判断分组条件是否应该适用于计数。要明确计数,您可以使用子查询:

SELECT  pi.planinfoid,
        pi.description,
        count(DISTINCT p.planid) AS total,
        sum(dm.debitamount) AS Num,
        pi.minperiod,
        max(opened) as opened,
        max(closed) as closed
FROM    planinfo pi join
        plans p
        on p.planinfoid = pi.planinfoid join
        debitmovements dm
        on dm.planid = p.planid left outer join
        (select planinfoid, SUM(1-p.closed) as opened, SUM(p.closed) as closed
         from planinfo pi join
              plans p
              on pi.planinfoid = p.planinfoid
         group by planinfoid
        ) psum
        on psum.planinfoid = psum.planinfoid
WHERE   pi.price > 0 AND
        p.servicestart BETWEEN '2012-01-01' AND '2013-01-01'
GROUP BY    pi.description,
            pi.minperiod,
            pi.planinfoid
ORDER BY total DESC

但是,在您的问题的上下文中,您可以通过为内部子查询提供不同的别名来做您想做的事情:

    (
        SELECT count(p2.planid)
        FROM plans p2 join
             planinfo pi2
             on pi2.planinfoid = p2.planinfoid
        WHERE
                pi2.planinfoid = pi.planinfoid
                AND p2.closed = 0
    ) AS opened,
于 2013-01-10T15:15:12.087 回答