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  ( ‘00001‘, ‘1‘, ‘Robert‘, NULL )
INSERT  INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES  ( ‘00002‘, ‘2‘, ‘John‘, ‘00001‘ )
INSERT  INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES  ( ‘00003‘, ‘3‘, ‘Tom‘, ‘00001‘ )
INSERT  INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES  ( ‘00004‘, ‘4‘, ‘Jim‘, ‘00003‘ )
INSERT  INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES  ( ‘00005‘, ‘5‘, ‘Lily‘, ‘00002‘ )
INSERT  INTO T_Person ( FId, FNumber, FName, FManagerId )
VALUES  ( ‘00006‘, ‘6‘, ‘Merry‘, ‘00003‘ )
INSERT  INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES  ( ‘00001‘, ‘1‘, ‘Bacon‘, 30 )
INSERT  INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES  ( ‘00002‘, ‘2‘, ‘Cake‘, 2 )
INSERT  INTO T_Merchandise ( FId, FNumber, FName, FPrice )
VALUES  ( ‘00003‘, ‘3‘, ‘Apple‘, 6 )

--  还要向T_SaleBill和T_PurchaseBill表中插入演示数据:
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00001‘, ‘1‘, ‘00006‘, ‘2007-03-15‘, ‘2007-05-15‘ )
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00002‘, ‘2‘, NULL, ‘2006-01-25‘, ‘2006-02-03‘ )
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00003‘, ‘3‘, ‘00001‘, ‘2006-02-12‘, ‘2007-01-11‘ )
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00004‘, ‘4‘, ‘00003‘, ‘2008-05-25‘, ‘2008-06-15‘ )
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00005‘, ‘5‘, ‘00005‘, ‘2008-03-17‘, ‘2007-04-15‘ )
INSERT  INTO T_SaleBill ( FId, FNumber, FBillMakerId, FMakeDate, FConfirmDate )
VALUES  ( ‘00006‘, ‘6‘, ‘00002‘, ‘2002-02-03‘, ‘2007-11-11‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00001‘, ‘1‘, ‘00006‘, ‘2007-02-15‘, ‘2007-02-15‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00002‘, ‘2‘, ‘00004‘, ‘2003-02-25‘, ‘2006-03-03‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00003‘, ‘3‘, ‘00001‘, ‘2007-02-12‘, ‘2007-07-12‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00004‘, ‘4‘, ‘00002‘, ‘2007-05-25‘, ‘2007-06-15‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00005‘, ‘5‘, ‘00002‘, ‘2007-03-17‘, ‘2007-04-15‘ )
INSERT  INTO T_PurchaseBill ( FId, FNumber, FBillMakerId, FMakeDate,
                              FConfirmDate )
VALUES  ( ‘00006‘, ‘6‘, NULL, ‘2006-02-03‘, ‘2006-11-20‘ )

--  向T_SaleBillDetail表和T_PurchaseBillDetail表中插入演示数据:
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00001‘, ‘00001‘, ‘00003‘, 20 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00002‘, ‘00001‘, ‘00001‘, 30 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00003‘, ‘00001‘, ‘00002‘, 22 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00004‘, ‘00002‘, ‘00003‘, 12 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00005‘, ‘00002‘, ‘00002‘, 11 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00006‘, ‘00003‘, ‘00001‘, 60 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00007‘, ‘00003‘, ‘00002‘, 2 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00008‘, ‘00003‘, ‘00003‘, 5 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00009‘, ‘00004‘, ‘00001‘, 16 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00010‘, ‘00004‘, ‘00002‘, 8 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00011‘, ‘00004‘, ‘00003‘, 9 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00012‘, ‘00005‘, ‘00001‘, 6 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00013‘, ‘00005‘, ‘00003‘, 26 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00014‘, ‘00006‘, ‘00001‘, 66 )
INSERT  INTO T_SaleBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00015‘, ‘00006‘, ‘00002‘, 518 ) 

INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00001‘, ‘00001‘, ‘00002‘, 12 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00002‘, ‘00001‘, ‘00001‘, 20 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00003‘, ‘00002‘, ‘00001‘, 32 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00004‘, ‘00002‘, ‘00003‘, 18 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00005‘, ‘00002‘, ‘00002‘, 88 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00006‘, ‘00003‘, ‘00003‘, 19 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00007‘, ‘00003‘, ‘00002‘, 6 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00008‘, ‘00003‘, ‘00001‘, 2 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00009‘, ‘00004‘, ‘00001‘, 20 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00010‘, ‘00004‘, ‘00003‘, 18 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00011‘, ‘00005‘, ‘00002‘, 19 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00012‘, ‘00005‘, ‘00001‘, 26 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00013‘, ‘00006‘, ‘00003‘, 3 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00014‘, ‘00006‘, ‘00001‘, 22 )
INSERT  INTO T_PurchaseBillDetail ( FId, FBillId, FMerchandiseId, FCount )
VALUES  ( ‘00015‘, ‘00006‘, ‘00002‘, 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]
时间: 2024-12-14 12:55:12

SQL 报表 --简易进销系统的相关文章

Access-简易进销存管理系统

p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; padding: 5px; margin: 1em 0 1em 0; } .alexrootdiv>div>p:first-of-type,.alextitlep{ font-size: 18px; font-weight: bold; color: red; } .alexrootdiv spa

手机进销存系统

花了将近两个月的时间学习了一个企业级进销存项目,已经结束了两周多,现在终于有时间来对这个项目的学习做个总结了! 一.首先介绍下这个项目 (注:本人目前大三,专业为信息管理,与编程沾边不多.而我对编程很感兴趣,从大一下期开始主要通过看书和视频自学学习java编程!). 项目名称:手机进销存系统(Mobile SCM) 主要开发技术:Struts2+Spring+ibatis+jQuery 数据库:MySql5.1(Navicat客户端管理工具) 服务器:tomcat7.0 建模工具:ER/Stud

Silverlight管理系统源码(用于开发ERP、OA、CRM、HR、进销存、财务等系统之用)

Silverlight大型管理系统源代码(支持创建ERP.OA.CRM.HR.进销存.财务等系统之用) 可用于开发以下系统 SilverlightERP SilverlightCRM SilverlightHR Silverlight财务软件 Silverlight管理系统 SilverlightOA 由于产品对外保密,所以界面和试用请联系北京动点飞扬咨询,提供完整源代码和数据库.开发文档 QQ:372900288 电话:13911652504 Silverlight管理系统源码(用于开发ERP

进销存系统如何帮助企业改善库存管理的?

库存过多,造成产品的积压,这似乎不是什么新鲜事,但是,年年积压,后面还是年年有人压,那么,库存的管理真的有那么难吗?其实,库存的管理没有那么难,而这不难,就全靠进销存系统的帮助,而这其中的关键点,我们一起来看一下. 1.出入库管理 货物的入库和出库管理那是至关重要,每笔货物的出入都是要通过审批的,而审批就从源头上确保了企业产品的安全性,但是如果通过传统的审批手段,比较麻烦,还会耽误货物的入库和发货时间,进而造成企业的损失,但是通过进销存系统就简单多了,比如在智邦国际进销存系统内,审批就可以直接在

全赢会员管理进销存收银系统

全赢会员管理进销存收银系统,QQ:150470092 原文地址:https://www.cnblogs.com/Tonyyang/p/10030349.html

Eclipse超市进销存系统

当今社会己进入信息社会时代,信息己经受到社会的广泛关注,被看作社会和科学技术发展的三大支柱(材料.能源.信息)之一.信息是管理的基础,是进行决策的的基本依据.在一个组织里,信息己作为人力.物力.财力之外的第四种能源,占有重要的地位.然而,信息是一种非物质的,有别于基本资源的新形式的资源.信息也是管理的对象,必须进行管理和控制.超市进销存系统是将IT技术用于超市信息的管理, 它能够收集与存储超市的档案信息,提供更新与检索超市信息档案的接口:提高工作效率.论文主要介绍了本课题的开发背景,所要完成的功

进销存系统的数据建模设计

数据建模其实就是数据库设计,是很关键的环节,他的前提条件是:1)对需求充分消化,全局性的归纳总结:(什么功能操作什么表.表与表之间的逻辑处理:)2)对接下来的程序开发,做了充分的布局,心中已有系统:(程序如何划分为子系统,每个程序间如何工作的) 设计原则及考虑维度 1)效率 开发效率,模型简单,更少的数据库表,开发容易,维护容易:2)性能 数据要快3)成本 避免数据冗余MIS系统我更在意的是开发效率及应变性,因为数据量和硬盘存储都没问题,重要的是兼容各种需求的变更: 本仓库系统需求核心要点:1)

ERP进销存系统源码

介绍 ERP进销存管理系统 软件架构 核心框架:SpringBoot 2.0.0 持久层框架:Mybatis 1.3.2 日志管理:Log4j 2.10.0 JS框架:Jquery 1.8.0 UI框架: EasyUI 1.3.5 模板框架: AdminLTE 2.4.0 项目管理框架: Maven 3.2.3 开发环境 建议开发者使用以下环境,可以避免版本带来的问题 IDE: IntelliJ IDEA 2017+ DB: Mysql5.7.4 JDK: JDK1.8 Maven: Maven

进销存系统流程

翻出三年前学习时 自己接手第一个项目前总结的进销存流程,感觉还是很有价值的.故分享出来.