HAVING子句中的Oracle PL / SQL变量

时间:2015-10-08 18:34:44

标签: oracle plsql

我在将一个变量放入HAVING子句时遇到了一些麻烦,问题是我需要COUNT(*)大于变量。

我正在使用ORACLE Database XE 11.2

DECLARE


  cnt1 NUMBER;
  cnt2 NUMBER;
  res NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO cnt1
    FROM BESTELLING;

  SELECT COUNT(*)
    INTO cnt2
    FROM ARTIKEL;

  res := cnt1 / cnt2;
END;
/

SELECT A.Naam, COUNT(*) AS HOEVEEL_VERKOCHT
FROM Artikel A, Winkelwagen W
WHERE A.Artikel_ID = W.Artikel_ID
AND W.Datum_Besteld IS NOT NULL
GROUP BY A.Naam
HAVING COUNT(*) > ?res?
ORDER BY COUNT(*) DESC;

2 个答案:

答案 0 :(得分:2)

将PL / SQL中的查询直接放在主查询中,作为子查询:

.......
HAVING COUNT(*) > 
   (  SELECT COUNT(*) FROM BESTELLING )   /
   (  SELECT COUNT(*) FROM ARTIKEL )
ORDER BY COUNT(*) DESC;

答案 1 :(得分:1)

这种方式不起作用?

DECLARE


  cnt1 NUMBER;
  cnt2 NUMBER;
  result NUMBER;
BEGIN
  SELECT COUNT(*)
    INTO cnt1
    FROM BESTELLING;

  SELECT COUNT(*)
    INTO cnt2
    FROM ARTIKEL;

  result := cnt1 / cnt2;
SELECT A.Naam, res, COUNT(*) AS HOEVEEL_VERKOCHT
FROM Artikel A, Winkelwagen W
WHERE A.Artikel_ID = W.Artikel_ID
AND W.Datum_Besteld IS NOT NULL
GROUP BY A.Naam, res
HAVING COUNT(*) > result
ORDER BY COUNT(*) DESC;

END;
/

我将变量res更改为result,因为它似乎有一个名为res的列