Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 计数查询接收/成分_Sql_Oracle_Top N - Fatal编程技术网

Sql 计数查询接收/成分

Sql 计数查询接收/成分,sql,oracle,top-n,Sql,Oracle,Top N,基本上,我一直在研究一个SQL问题,它要求显示每种成分以及它包含的食谱总数。它还必须只包括出现10次以上的成分,以及成分流行度的降序和ingrdesc的升序 各表如下: CREATE TABLE Ingredient ( idI NUMBER constraint pk_Ingredient PRIMARY KEY , ingrDesc VARCHAR2(100) constraint nn1Ingredient not null ); CREATE TABL

基本上,我一直在研究一个SQL问题,它要求显示每种成分以及它包含的食谱总数。它还必须只包括出现10次以上的成分,以及成分流行度的降序和
ingrdesc
的升序

各表如下:

CREATE TABLE Ingredient
(

idI NUMBER              constraint pk_Ingredient PRIMARY KEY ,

ingrDesc VARCHAR2(100)  constraint nn1Ingredient not null
);


CREATE TABLE Recipe
(

idR NUMBER                constraint pk_recipe PRIMARY KEY ,

recipeTitle VARCHAR2(200)  constraint nn1Recipe not null,
prepText VARCHAR2(4000),

cuisineType VARCHAR2(50),
mealType VARCHAR2(30) DEFAULT NULL,

CONSTRAINT ch_mealType CHECK (mealType IN ('starter', 'main', 'dessert', null))
);


CREATE TABLE RecpIngr
(

idR NUMBER ,

hidI NUMBER ,

CONSTRAINT pk_RecpIngr PRIMARY KEY (idR, idI),

CONSTRAINT fk1RecpIngr_recipe foreign key(idR) references Recipe,

CONSTRAINT fk2RecpIngr_ingredient foreign key(idI) references Ingredient
)
organization index;
到目前为止,我有一个疑问:

SELECT ingrDesc,
COUNT (idR) As num_of_recipes
FROM RespIngr
WHERE num_of_recipes <10
ORDER BY num_of_recipes DES, ingrDes ASC;
选择ingrDesc,
计算(idR)为配方的数量
来自respinger
其中num_of_recipes尝试以下方法:

SELECT ingrDesc,
COUNT (idR) As num_of_recipes
FROM RespIngr
GROUP BY ingrDesc
HAVING COUNT (idR) > 10
ORDER BY num_of_recipes DESC, ingrDesc ASC;

我不明白。您显示了一个来自表
RespIngr
的查询,但没有提到这样一个表。您将显示一个表
RecpIngr
,但该表不包含字段
ingrDesc

要获取显示的字段,查询必须包含包含配方和成分的表与包含成分描述的表的联接

with
RCount( IngID, RecipeCount )as(
    select  hidI, count(*)
    from    RecpIngr
    group by hidI
    having count(*) > 10
)
select  i.ingrDesc as "Ingredient", rc.RecipeCount as "Number of Recipes"
from    Ingredient  i
join    RCount      rc
    on  rc.IngID = i.idI;