求教一道SQL题目
一个简单的表Proudect有以下信息ProudectColorAmount产品1红123产品1蓝126产品2蓝103产品2红Null产品2红89产品1红203要求写出:...
一个简单的表Proudect有以下信息
Proudect Color Amount
产品1 红 123
产品1 蓝 126
产品2 蓝 103
产品2 红 Null
产品2 红 89
产品1 红 203
要求写出:
1.按产品分类,仅列出各类产品中红色多于蓝色产品的名称及差额。
2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色 展开
Proudect Color Amount
产品1 红 123
产品1 蓝 126
产品2 蓝 103
产品2 红 Null
产品2 红 89
产品1 红 203
要求写出:
1.按产品分类,仅列出各类产品中红色多于蓝色产品的名称及差额。
2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色 展开
3个回答
展开全部
楼上的算法应该是没什么问题,不过对于数据是null的要另行处理,这个时候需要用nvl函数
/*1.按产品分类,仅列出各类产品中红色多于蓝色产品的名称及差额。*/
select A.Proudect as 名称, (A.Amount - B.Amount) as 差额 from
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝' and A.Amount > B.Amount
/*2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色
*/
select A.Proudect as 名称, A.Amount as '红色', B.Amount as '蓝色' from
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝'
/*1.按产品分类,仅列出各类产品中红色多于蓝色产品的名称及差额。*/
select A.Proudect as 名称, (A.Amount - B.Amount) as 差额 from
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝' and A.Amount > B.Amount
/*2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色
*/
select A.Proudect as 名称, A.Amount as '红色', B.Amount as '蓝色' from
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(nvl(Amount),0) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝'
光点科技
2023-08-15 广告
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
展开全部
/*1.按产品分类,仅列出各类产品中红色多于蓝色产品的名称及差额。*/
select A.Proudect as 名称, (A.Amount - B.Amount) as 差额 from
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝' and A.Amount > B.Amount
/*2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色
*/
select A.Proudect as 名称, A.Amount as '红色', B.Amount as '蓝色' from
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝'
select A.Proudect as 名称, (A.Amount - B.Amount) as 差额 from
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝' and A.Amount > B.Amount
/*2.按产品分类,将数据按一下方式进行统计显示:
产品 红色 蓝色
*/
select A.Proudect as 名称, A.Amount as '红色', B.Amount as '蓝色' from
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) A,
(select Proudect, Color, Sum(Amount) Amount from Proudect
group by Proudect, Color) B
where A.Proudect = B.Proudect and A.Color = '红' and B.Color = '蓝'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询