sql利用视图实现一个数值型字段的求和问题
有如下简单需求:客户购买产品,厂商想看到收入和欠费统计。
已存在的数据库主要表结构:客户表(Customer)和销售记录表(SoldRecord),另外相关表(如产品表Product)在此略过。
1、客户表
这个表很简单:

字段说明:

字段说明:
3、视图创建(viewRecordDetail)
4、简单示例
下面向两个表里填充一些数据,测试一下。
(1)、客户表
已存在的数据库主要表结构:客户表(Customer)和销售记录表(SoldRecord),另外相关表(如产品表Product)在此略过。
1、客户表
这个表很简单:

字段说明:
tid int 自增字段 关键字
name 客户名
adddate 添加日期
address 客户所在地
2、销售记录表name 客户名
adddate 添加日期
address 客户所在地

字段说明:
tid int 自增字段 关键字
customerid 客户tid
boughtdate 客户购买产品日期
paidmoney 金额(正数代表已付款,负数代表欠费)
客户需求分析:“厂商想看到收入和欠费统计”,分析上述表结构,可以看到问题的由来就出在这个表的paidmoney字段上。收入必须将paidmoney按照正数相加,欠费则必须按负数相加。这样我们就想到将paidmoney字段“拆一为二”,便于sql函数的统计计算。 customerid 客户tid
boughtdate 客户购买产品日期
paidmoney 金额(正数代表已付款,负数代表欠费)
3、视图创建(viewRecordDetail)
SELECT dbo.Customer.tid, dbo.Customer.name, dbo.Customer.addDate, dbo.Customer.address, dbo.SoldRecord.boughtDate, dbo.SoldRecord.tid AS sid,
ABS(CASE WHEN dbo.SoldRecord.paidMoney > 0 THEN dbo.SoldRecord.paidMoney ELSE 0 END) AS income,
ABS(CASE WHEN dbo.SoldRecord.paidMoney < 0 THEN dbo.SoldRecord.paidMoney ELSE 0 END) AS outcome, dbo.SoldRecord.paidMoney
FROM dbo.Customer INNER JOIN
dbo.SoldRecord ON dbo.Customer.tid = dbo.SoldRecord.CustomerId
GROUP BY dbo.Customer.tid, dbo.SoldRecord.tid, dbo.Customer.name, dbo.Customer.addDate, dbo.Customer.address, dbo.SoldRecord.boughtDate,
dbo.SoldRecord.paidMoney
很明显,两个表的连接查询创建一个视图,重点是利用case when将paidmoney的拆分。其中income为收入,outcome为欠费,函数ABS取绝对值(我们当然可以利用其他函数如sum取值)。这样需要统计的话,直接对视图进行操作就可以了。ABS(CASE WHEN dbo.SoldRecord.paidMoney > 0 THEN dbo.SoldRecord.paidMoney ELSE 0 END) AS income,
ABS(CASE WHEN dbo.SoldRecord.paidMoney < 0 THEN dbo.SoldRecord.paidMoney ELSE 0 END) AS outcome, dbo.SoldRecord.paidMoney
FROM dbo.Customer INNER JOIN
dbo.SoldRecord ON dbo.Customer.tid = dbo.SoldRecord.CustomerId
GROUP BY dbo.Customer.tid, dbo.SoldRecord.tid, dbo.Customer.name, dbo.Customer.addDate, dbo.Customer.address, dbo.SoldRecord.boughtDate,
dbo.SoldRecord.paidMoney
4、简单示例
下面向两个表里填充一些数据,测试一下。
(1)、客户表
use testdb
insert into customer select 'jeff wong',getdate(),'beijing'
union all select 'jeffery zhao',getdate(),'shanghai'
union all select 'dudu',getdate(),'shanghai'
union all select 'terrylee',getdate(),'tianjin'
(2)、销售记录表insert into customer select 'jeff wong',getdate(),'beijing'
union all select 'jeffery zhao',getdate(),'shanghai'
union all select 'dudu',getdate(),'shanghai'
union all select 'terrylee',getdate(),'tianjin'
use testdb
insert into soldRecord select 1,getdate(),168
union all select 2 ,getdate(),223
union all select 1,getdate(),-7500
union all select 1,getdate(),268
union all select 4 ,getdate(),-113
union all select 3,getdate(),500
union all select 1,getdate(),22
union all select 4 ,getdate(),15
union all select 3,getdate(),-15000
union all select 1,getdate(),200
union all select 2 ,getdate(),111
union all select 2,getdate(),7000
union all select 2,getdate(),268
(3)、统计insert into soldRecord select 1,getdate(),168
union all select 2 ,getdate(),223
union all select 1,getdate(),-7500
union all select 1,getdate(),268
union all select 4 ,getdate(),-113
union all select 3,getdate(),500
union all select 1,getdate(),22
union all select 4 ,getdate(),15
union all select 3,getdate(),-15000
union all select 1,getdate(),200
union all select 2 ,getdate(),111
union all select 2,getdate(),7000
union all select 2,getdate(),268
select sum(income) as totalIncome,
sum(outcome) as totalOutcome from viewRecordDetail
小结:这里只是记录一下个人解决问题的思路和简单实践,并不推荐直接利用sql的dbms做这些琐碎的统计处理。sql固然强大,但是,汝之蜜糖,焉知不是我之毒药呢?其实强大的高级语言如c#,java等都可以轻松实现这些功能,更何况创建太多视图什么的不易维护。还是千方百计地创建合理的表结构,交给高级语言处理去吧。
sum(outcome) as totalOutcome from viewRecordDetail
作者:Jeff Wong
出处:http://jeffwongishandsome.cnblogs.com/
本文版权归作者和博客园共有,欢迎围观转载。转载时请您务必在文章明显位置给出原文链接,谢谢您的合作。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构