一列作为参数都有和哪里(不包括)

时间:2018-01-02 23:36:12

标签: sql firebird

我有两个问题:

首先:

select p.prodid, p.name, max(b.ldate) as lastsale 
from prod p, buy b 
where p.id = b.idprod and b.id<>0  and b.wskus=0 and b.bufor=0
group by p.prodid, p.name
HAVING sum(b.curNo)=0
order by p.name asc

第二

select p.prodid, p.name, min(b.buydate) as oldest_buy 
from prod p, buy b         
where p.id = b.idprod and b.id<>0  and b.wskus=0 and b.bufor=0 and b.curNo>0
group by p.prodid, p.name
order by p.name asc

如何让他们加入JOIN作为结果列:

| p.prodid | p.name | lastsale | oldest_buy |
| 1        | ex1    | 1.1.18   | NULL       |
| 2        | ex2    | NULL     | 1.1.18     |

第一个查询的HAVING sum(b.curNo)=0与第二个查询的WHERE参数相同b.curNo>0我对如何使其工作有疑问。

2 个答案:

答案 0 :(得分:1)

如果没有您的输入数据,很难说,但这可能对您有用......

SELECT
  p.prodid,
  p.name,
  MIN(CASE WHEN b.curNo > 0 THEN b.buydate END)     AS oldest_buy,   -- MIN(buydate) WHERE curno>0
  CASE WHEN SUM(b.curNo) = 0 THEN MAX(b.ldate) END  AS lastsale      -- MAX(ldate) HAVING SUM(curNo) = 0
FROM
  prod p
INNER JOIN   -- Don't use "," use "JOIN"s, the standard for about 25 years...
  buy b
    ON p.id = b.idprod
WHERE
      b.id    <> 0
  AND b.wskus  = 0
  AND b.bufor  = 0
GROUP BY
  p.prodid,
  p.name
ORDER BY
  p.name  ASC


b.curNo > 0SUM(b.curNo) = 0移至CASE语句可能会产生额外的行,具体取决于数据的行为。没有更多细节或示例数据,我们无法分辨。

两次计算中的值都没问题,但我不能说出行数。

为了更明确一点,你可以做......

SELECT
  p.prodid,
  p.name,
  CASE WHEN MAX(b.curNo) > 0 THEN MIN(CASE WHEN b.curNo > 0 THEN b.buydate END) END    AS oldest_buy,
  CASE WHEN SUM(b.curNo) = 0 THEN MAX(b.ldate)                                  END   AS lastsale
FROM
  prod p
INNER JOIN   -- Don't use "," use "JOIN"s, the standard for about 25 years...
  buy b
    ON p.id = b.idprod
WHERE
      b.id    <> 0
  AND b.wskus  = 0
  AND b.bufor  = 0
GROUP BY
  p.prodid,
  p.name
HAVING
     SUM(b.curNo) = 0
  OR MAX(b.curNo) > 0
ORDER BY
  p.name  ASC


另一种可能性(再次因为你没有给出示例数据)是聚合然后加入。

这是基于您的意思p.curNo而不是b.curNo ......

SELECT
  p.prodid,
  p.name,
  CASE p.curNo > 0 THEN b.oldest_buy END   AS oldest_buy,
  CASE p.curNo = 0 THEN b.last_sale  END   AS lastsale
FROM
  prod p
INNER JOIN   -- Don't use "," use "JOIN"s, the standard for about 25 years...
(
  SELECT
     idprod,
     MIN(buydate)    AS oldest_buy,
     MAX(ldate)      AS last_sale
  FROM
     buy
  WHERE
        b.id    <> 0
    AND b.wskus  = 0
    AND b.bufor  = 0
)
  b
    ON p.id = b.idprod
ORDER BY
  p.name  ASC

答案 1 :(得分:0)

在union all之前将第一个查询放在子查询中。试试这个:

select 
t.prodid, t.name, t.lastsale, null as oldest_buy
from (select p.prodid, p.name, 
             max(b.ldate) as lastsale 
      from   prod p, buy b 
      where  p.id = b.idprod and b.id<>0  and 
             b.wskus=0 and b.bufor=0
      group by p.prodid, p.name
      HAVING sum(b.curNo)=0 ) t
union all
( select p.prodid, p.name, 
  null as lastsale, min(b.buydate) as oldest_buy 
  from prod p, buy b        
  where p.id = b.idprod and b.id<>0  and b.wskus=0 
  and b.bufor=0 and b.curNo>0
  group by p.prodid, p.name )
order by 2 asc