SQL 报表 --简易进销系统
模型图:
-- ============================================
-- Author: lifu
-- Create Date: 2017-06-18
-- Descrpition: 简化进销系统 报表制作
-- ============================================ -- Tables And Data Start ======================
CREATE DATABASE SimpleEntrySystem
GO
USE SimpleEntrySystem
GO
--创建T_Person表 人员表
CREATE TABLE T_Person
(
FId VARCHAR(20) NOT NULL ,
FNumber VARCHAR(20) , --人员工号
FName VARCHAR(20) , --人员姓名
FManagerId VARCHAR(20) , --上级主管主键(指向T_Person表的 FId字段的外键)
PRIMARY KEY ( FId ) ,
FOREIGN KEY ( FManagerId ) REFERENCES T_Person ( Fid )
)
--创建T_Merchandise表 商品表
CREATE TABLE T_Merchandise
(
FId VARCHAR(20) NOT NULL ,
FNumber VARCHAR(20) , --商品编号
FName VARCHAR(20) , --商品名
FPrice INT , --商品价格
PRIMARY KEY ( fid )
)
--创建T_SaleBill表 销售单主表
CREATE TABLE T_SaleBill
(
FId VARCHAR(20) NOT NULL ,
FNumber VARCHAR(20) , --销售单编号
FBillMakerId VARCHAR(20) ,--开单人主键(指向T_Person表的 FId字段的外键)
FMakeDate DATETIME , --制单日期
FConfirmDate DATETIME , --确认日期
PRIMARY KEY ( fid ) ,
FOREIGN KEY ( Fbillmakerid ) REFERENCES T_Person ( fid )
)
--创建T_SaleBillDetail表 销售单明细记录
CREATE TABLE T_SaleBillDetail
(
FId VARCHAR(20) ,
FBillId VARCHAR(20) , --主表主键(指向 T_SaleBill 表的 FId 字段的外键)
FMerchandiseId VARCHAR(20) , --商品主键(指向T_Merchandise表的FId字段的外键)
FCount INT , --销售数量
PRIMARY KEY ( fid ) ,
FOREIGN KEY ( Fbillid ) REFERENCES T_SaleBill ( fid ) ,
FOREIGN KEY ( Fmerchandiseid ) REFERENCES T_Merchandise ( fid )
)
--创建T_PurchaseBill表 采购单主表
CREATE TABLE T_PurchaseBill
(
Fid VARCHAR(20) NOT NULL ,
FNumber VARCHAR(20) , --采购单编号
FBillMakerId VARCHAR(20) ,--开单人主键(指向T_Person表的FId字段的外键)
FMakeDate DATETIME , --制单日期
FConfirmDate DATETIME , --确认日期
PRIMARY KEY ( fid ) ,
FOREIGN KEY ( FBillMakerId ) REFERENCES T_Person ( fid )
)
--创建T_PurchaseBillDetail表 采购单明细记录
CREATE TABLE T_PurchaseBillDetail
(
FId VARCHAR(20) NOT NULL ,
FBillId VARCHAR(20) , --主表主键(指向T_PurchaseBill表的FId字段的外键)
FMerchandiseId VARCHAR(20) ,--商品主键(指向T_Merchandise表的FId字段的外键)
FCount INT , --采购数量
PRIMARY KEY ( FId ) ,
FOREIGN KEY ( FBillId ) REFERENCES T_PurchaseBill ( FId ) ,
FOREIGN KEY ( FMerchandiseId ) REFERENCES T_Merchandise ( FId )
)
--首先向T_Person、T_Merchandise两张表中插入演示数据:
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'Robert', NULL )
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'John', '' )
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'Tom', '' )
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'Jim', '' )
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'Lily', '' )
INSERT INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES ( '', '', 'Merry', '' )
INSERT INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES ( '', '', 'Bacon', 30 )
INSERT INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES ( '', '', 'Cake', 2 )
INSERT INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES ( '', '', 'Apple', 6 ) -- 还要向T_SaleBill和T_PurchaseBill表中插入演示数据:
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', '', '2007-03-15', '2007-05-15' )
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', NULL, '2006-01-25', '2006-02-03' )
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', '', '2006-02-12', '2007-01-11' )
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', '', '2008-05-25', '2008-06-15' )
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', '', '2008-03-17', '2007-04-15' )
INSERT INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES ( '', '', '', '2002-02-03', '2007-11-11' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', '', '2007-02-15', '2007-02-15' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', '', '2003-02-25', '2006-03-03' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', '', '2007-02-12', '2007-07-12' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', '', '2007-05-25', '2007-06-15' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', '', '2007-03-17', '2007-04-15' )
INSERT INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
FConfirmDate )
VALUES ( '', '', NULL, '2006-02-03', '2006-11-20' ) -- 向T_SaleBillDetail表和T_PurchaseBillDetail表中插入演示数据:
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 20 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 30 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 22 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 12 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 11 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 60 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 2 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 5 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 16 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 8 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 9 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 6 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 26 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 66 )
INSERT INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 518 ) INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 12 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 20 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 32 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 18 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 88 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 19 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 6 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 2 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 20 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 18 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 19 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 26 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 3 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 22 )
INSERT INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES ( '', '', '', 168 ) -- Tables And Data End ======================= -- 报表===================================================================
USE [SimpleEntrySystem]
GO
-- 显示制单人详细信息
SELECT [FNumber], [FBillMakerId], [FMakeDate]
FROM [dbo].[T_SaleBill]
--inner join 需要知道是哪个人开的单
SELECT [s].[FNumber], [p].[FName], [s].[FMakeDate]
FROM [dbo].[T_SaleBill] AS [s]
INNER JOIN [dbo].[T_Person] AS [p] ON [s].[FBillMakerId] = [p].[FId]
--left outer join 需要将空的开单人标识
SELECT [s].[FNumber], COALESCE([p].[FName], '没有开单人'), [s].[FMakeDate]
FROM [dbo].[T_SaleBill] AS [s]
LEFT OUTER JOIN [dbo].[T_Person] AS [p] ON [s].[FBillMakerId] = [p].[FId] -- 显示销售单的信息
/*
要求列出所有销售单的详细信息,每行显示销售单的每一条销售记录,同时每行头部要
显示此行所属的销售单的信息,比如单号、开单人、开单日期等。T_SaleBillDetail表保存的
是销售单的每一条销售记录,T_SaleBill表保存的是销售单的头信息,T_SaleBillDetail表的
FMerchandiseId字段保存的是销售的商品主键,而 T_SaleBill表的 FBillMakerId字段保存的
是开单人的主键,只要对这四张表做连接查询即可。由于 T_SaleBill表的 FBillMakerId字段
有可能为空,所以在 T_SaleBill 表和 T_Person 表进行连接的时候要使用左外连接,而为了
提高查询效率其他连接都使用内连接
*/
SELECT [saleBill].[FNumber] AS '销售单编号',
COALESCE([person].[FName], '没有开单人') AS '开单人',
[saleBill].[FMakeDate] AS '销售时间', [merchandise].[FName] AS '商品名称',
[saleBillDetail].[FCount] AS '销售数量'
FROM [dbo].[T_SaleBill] AS [saleBill]
LEFT OUTER JOIN [dbo].[T_Person] AS [person] ON [saleBill].[FBillMakerId] = [person].[FId]
INNER JOIN [dbo].[T_SaleBillDetail] AS [saleBillDetail] ON saleBillDetail.[FBillId] = [saleBill].[FId]
INNER JOIN [dbo].[T_Merchandise] AS [merchandise] ON [merchandise].[FId] = [saleBillDetail].[FMerchandiseId]
ORDER BY [saleBill].[FMakeDate] DESC --收益计算
/*
要求计算每种商品的总收益, 受收益的定义为所有的销售单中该商品的销售总额减去所
有的采购单中该商品的购买总额。
*/
----------------------------------------------------------------------------------
--销售额
SELECT [m].[FName] AS '商品名称', [m].[FPrice] * [sbd].[FCount] AS '销售额'
FROM [dbo].[T_Merchandise] AS [m]
INNER JOIN [dbo].[T_SaleBillDetail] AS [sbd] ON [m].[FId] = [sbd].[FMerchandiseId] UNION ALL --采购额
SELECT [m].[FName] AS '商品名称', [m].[FPrice] * [pbd].[FCount] * ( -1 ) AS '采购额'
FROM [dbo].[T_Merchandise] AS [m]
INNER JOIN [dbo].[T_PurchaseBillDetail] AS [pbd] ON [m].[FId] = [pbd].[FMerchandiseId]
---------------------------------------------------------------------------------- --将采购和销售计算合并
SELECT [detail].[FName], SUM([detail].[总额])
FROM ( SELECT [m].[FName] ,
[m].[FPrice] * [sbd].[FCount] AS '总额'
FROM [dbo].[T_Merchandise] AS [m]
INNER JOIN [dbo].[T_SaleBillDetail] AS [sbd] ON [m].[FId] = [sbd].[FMerchandiseId]
UNION ALL
SELECT [m].[FName] ,
[m].[FPrice] * [pbd].[FCount] * ( -1 ) AS '总额'
FROM [dbo].[T_Merchandise] AS [m]
INNER JOIN [dbo].[T_PurchaseBillDetail] AS [pbd] ON [m].[FId] = [pbd].[FMerchandiseId]
) AS detail
GROUP BY [detail].[FName]
SQL 报表 --简易进销系统的更多相关文章
- SpringBoot终章(整合小型进销系统)
在前面的章节中我们学习Spring的时候可以看到配置文件比较多,所以我们有了SpringBoot 1. 引入依赖 <dependencies> <dependency> < ...
- Access-简易进销存管理系统
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- openerp 经典收藏 Openerp开发进销存系统完毕总结(转载)
原文地址:http://blog.csdn.net/heartrude/article/details/9142463 Openerp开发进销存系统完毕总结 分类: 代码历程 OpenERP 工程思想 ...
- spring boot的一个小项目小型进销存系统
项目所需的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- C# WINFORM进销存系统开发(内涵免费源码+部分实操视频讲解)
互联网的时代,电商火爆,大家都开始进行线上销售货品,那你是如何管理你的商品库存和进销问题?软积木--小敏用的是C# WINFORM进销存系统来管理我的数据,给我带来了很多便利. 它是高频需求项目,很多 ...
- 浩瀚技术团队... 安卓智能POS移动PDA开单器 开单器 进销存系统 进销存系统
浩瀚技术团队... 智能POS移动PDA开单器 开单器 进销存系统 进销存系统 点餐 会员管理 会员管理 深度解读 手机APP移动办公到底是什么? 快速打单POS·不仅仅是快那么简单!
- PDA手持机 移动开单进销存系统 现场出打印凭据和扫码 新的亮点
传统车销模式弊端:1.手写开单,效率低,动作慢2.现场手写开单明细不能打印,产品明细不规范3.电脑办公人员及车销人员对车上的库存情况掌握不清楚,销售人员对每种产品销售价格不清楚4.老板对员工工作的管控 ...
- Silverlight管理系统源码(用于开发ERP、OA、CRM、HR、进销存、财务等系统之用)
Silverlight大型管理系统源代码(支持创建ERP.OA.CRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM Silverli ...
- [系统开发] FileMaker进销存系统
一.简介 这是我用 FileMaker 编写的进销存系统: FileMaker 是一种在欧美流行的桌面型数据库:它使用非常方便,功能也很强大,用户可以在它上面开发自己的系统: 开发时间:2008年 二 ...
随机推荐
- Jmeter与Jenkins结合进行Web接口测试
纯通过Jmeter的界面进行Web的接口测试,效率低下.为此将Jmeter的接口测试与Jenkins联合,实现持续集成.配置完成后,只需修改运行的Jmeter脚本即可,运行结束后测试结果发送到指定邮箱 ...
- HTTP与HTTPS对访问速度(性能)的影响【转】
1 前言 HTTPS 在保护用户隐私,防止流量劫持方面发挥着非常关键的作用,但与此同时,HTTPS 也会降低用户访问速度,增加网站服务器的计算资源消耗. 本文主要介绍 https 对用户体验的影响. ...
- 2018C语言第三次作业
要求一 2.struct sk{int a; char *str)}*p; p->str++ 中的++ 加向? ++加向srt的地址. 要求二 题目1-计算平均成绩 1.设计思路 (1)主要 ...
- poj3417Network【LCA】【树形DP】
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...
- HDU 2544 - 最短路 - [堆优化dijkstra][最短路模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Time Limit: 5000/1000 MS (Java/Others) Memory Li ...
- SQL Fundamentals || Single-Row Functions || 转换函数 Conversion function
SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使 ...
- MyBatis学习(一)一个简单的例子
mybatis入门例子 开发步骤: 1.创建java工程 2.加入jar包(依赖包.驱动包) 3.创建sqlMapConfig.xml 4.创建数据库,数据库表USER_C,插入测试记录 5.创建PO ...
- MongoDB-3.4安装文档
1.建立目录 2.将解压文件cp到步骤1建立目录下 mongodb-win32-x86_64-enterprise-windows-64-3.4.1.zip 3.配置环境变量 4.启动mongodb服 ...
- svn冲突的解决
svn文件冲突的解决 冲突后,会产生三个多余的文件. ①文件名.扩展名.mine 这是你的文件,在你更新你的工作副本之前存在于你的工作副本中--也就是说,没有冲突标志.这个文件 除了你的最新修改外没有 ...
- 洛谷P4168 蒲公英 [Violet] 分块
题解:分块+离散化 解题报告: 一个分块典型题呢qwq还是挺妙的毕竟是道黑题 然,然后发现忘记放链接了先放链接QAQ 有两三种解法,都港下qwq 第一个是O(n5/3)的复杂度,谢总说不够优秀没有港, ...