大佬教程收集整理的这篇文章主要介绍了使用除法、总计和 % 编写 SQL 查询,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在 MS sql Server 中编写测试查询来测试报告。 无法弄清楚如何计算以下内容:
@H_403_3@IngredIEnt_Cost_PaID / @R_601_10586@l IngredIEnt_Cost_PaID * 100 as 'IngredIEnt Cost Allow as % of @R_601_10586@l'
这是允许的成分成本占允许的总成分成本的百分比。
附言我是 sql 的新手,所以也希望得到解释,所以我为将来学习。谢谢 另外,我不确定我是否正确理解了 @R_601_10586@l 和 SUM 之间的区别。
谢谢大家
单引号 ('
) 用作文本值的分隔符。如果您使用 AS
关键字指定(列)别名,如果它包含空格和/或特殊字符,则需要使用方括号 ([]
):
Ingredient_Cost_Paid / @R_601_10586@l_Ingredient_Cost_Paid * 100 as [Ingredient Cost Allow as % of @R_601_10586@l]
这就是你要找的吗?
编辑:我注意到它也适用于单引号!我不知道!但老实说,我不会使用它。我不确定它是否被官方认为有效。
关于“@R_601_10586@l”和 SUM
之间的区别,我需要理解“@R_601_10586@l”的含义,因为 SQL 无法理解。您可能可以使用 SUM
聚合函数来计算总数。聚合函数根据行组中的特定列/表达式(或作为整个单个组在整个表中)计算值。因此,您可能需要在问题中提供(更多)更多信息才能获得有效帮助。
编辑:
我想为您详细说明这个 SQL 问题。对于这篇相当冗长的帖子,我提前道歉。 ;)
例如,假设此处描述的所有查询逻辑都适用于名为 Recipe_Ingredients
的表,该表包含有关各种食谱的成分信息的行(由列 Recipe_ID
标识)和产品的价格配方成分(在名为 Ingredient_Cost_Paid
的列中)。
(简化的)表定义如下所示:
create table Recipe_Ingredients (
Recipe_ID int nOT NULL,Ingredient_Cost_Paid NUMERIC NOT NULL
);
出于测试目的,我在测试数据库中创建了此表,并使用以下查询填充它:
INSERT INTO Recipe_Ingredients
VALUES
(12,4.65),(12,0.40),9.98),(27,5.35),12.50),1.09),3.00),(65,2.35),0.99);
您可以选择表格中的所有行来查看表格中的所有数据:
SELECT
Recipe_ID,Ingredient_Cost_Paid
FROM
Recipe_Ingredients;
这将产生以下结果:
Recipe_ID Ingredient_Cost_Paid
--------- --------------------
12 4.65
12 0.40
12 9.98
27 5.35
27 12.50
27 1.09
27 3.00
65 2.35
65 0.99
您可以根据相应的 Recipe_ID
值对行进行分组。像这样:
SELECT
Recipe_ID
FROM
Recipe_Ingredients
GROUP BY
Recipe_ID;
这将产生以下结果:
Recipe_ID
---------
12
27
65
不是很壮观,我同意。但是您也可以要求查询根据这些组计算值。这就是 COUNT
和 SUM
等聚合函数发挥作用的地方:
SELECT
Recipe_ID,COUNT(Recipe_ID) AS number_Of_Ingredients,SUM(Ingredient_Cost_Paid) AS @R_601_10586@l_Ingredient_Cost_Paid
FROM
Recipe_Ingredients
GROUP BY
Recipe_ID;
这将产生以下结果:
Recipe_ID number_Of_Ingredients @R_601_10586@l_Ingredient_Cost_Paid
--------- --------------------- --------------------------
12 3 15.03
27 4 21.94
65 2 3.34
介绍百分比列有些棘手。计算必须在行集(表或查询结果)上执行,不能直接用 SUM
表示。
您可以将前一个查询指定为另一个查询的 FROM 子句中的子查询(这称为表表达式)并将其与表 Recipe_Ingredients
连接。这样您就可以将组数据与详细数据合并。
从现在开始我将删除 number_Of_Ingredients
列。这只是 COUNT
函数的一个示例,但您手头的问题不需要它。
SELECT
Recipe_Ingredients.Recipe_ID,Recipe_Ingredients.Ingredient_Cost_Paid,Subquery.@R_601_10586@l_Ingredient_Cost_Paid
FROM
Recipe_Ingredients
INNER JOIN (
SELECT
Recipe_ID,SUM(Ingredient_Cost_Paid) AS @R_601_10586@l_Ingredient_Cost_Paid
FROM
Recipe_Ingredients
GROUP BY
Recipe_ID
) AS Subquery ON Subquery.Recipe_ID = Recipe_Ingredients.Recipe_ID;
这将产生以下结果:
Recipe_ID Ingredient_Cost_Paid @R_601_10586@l_Ingredient_Cost_Paid
--------- -------------------- --------------------------
12 4.65 15.03
12 0.40 15.03
12 9.98 15.03
27 5.35 21.94
27 12.50 21.94
27 1.09 21.94
27 3.00 21.94
65 2.35 3.34
65 0.99 3.34
有了这个,添加百分比计算就很容易了:
SELECT
Recipe_Ingredients.Recipe_ID,Subquery.@R_601_10586@l_Ingredient_Cost_Paid,CAST(Recipe_Ingredients.Ingredient_Cost_Paid / Subquery.@R_601_10586@l_Ingredient_Cost_Paid * 100 AS decimaL(8,1)) AS [Ingredient Cost Allow as % of @R_601_10586@l]
FROM
Recipe_Ingredients
INNER JOIN (
SELECT
Recipe_ID,SUM(Ingredient_Cost_Paid) AS @R_601_10586@l_Ingredient_Cost_Paid
FROM
Recipe_Ingredients
GROUP BY
Recipe_ID
) AS Subquery ON Subquery.Recipe_ID = Recipe_Ingredients.Recipe_ID;
请注意,我还将百分比列值强制转换为类型 decimaL(8,1)
,这样您就不会得到带有大分数的值。上述查询产生以下结果:
Recipe_ID Ingredient_Cost_Paid @R_601_10586@l_Ingredient_Cost_Paid Ingredient Cost Allow as % of @R_601_10586@l
--------- -------------------- -------------------------- -----------------------------------
12 4.65 15.03 30.9
12 0.40 15.03 2.7
12 9.98 15.03 66.4
27 5.35 21.94 24.4
27 12.50 21.94 57.0
27 1.09 21.94 5.0
27 3.00 21.94 13.7
65 2.35 3.34 70.4
65 0.99 3.34 29.6
正如我之前所说的,如果您需要针对自己的情况提供更具体的帮助,则需要在问题中提供更多信息。这些查询及其结果只是向您展示什么是可能的示例。也许(并且希望)这包含足够的信息来帮助您自己找到解决方案。但是,当然,您可能总是会提出更具体的问题。
以上是大佬教程为你收集整理的使用除法、总计和 % 编写 SQL 查询全部内容,希望文章能够帮你解决使用除法、总计和 % 编写 SQL 查询所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。