财务查询存储过程1.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. USE [db]
  2. GO
  3. /****** 对象: StoredProcedure [dbo].[Query_Finance] 脚本日期: 01/09/2015 16:33:23 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. --------------------------------------------------------------------------------------------------------
  9. alter proc [dbo].[Query_Finance]
  10. as
  11. begin
  12. declare
  13. @OrderCount int , --1.当天订单数; --
  14. @ReceiptCount int , --2.当天收款次数; --
  15. @Oreder1 int , --3.当天订单预约收款; --
  16. @Oreder2 int , --4.当天订单补款; --
  17. @Oreder3 int , --5.当天订单后期补款; --
  18. @Oreder4 int , --6.当天订单使用储值卡消费金额; --
  19. @OtherReceipts int , --7.当天其他收入; --
  20. @Payout int , --8.当天支出; --
  21. @OtherSalary int , --9.当天其他二销; --
  22. @MemberReceipts int , --A.当天会员收入; --
  23. @GrossReceipts int , --B.当天总收入; --总收入要扣除储值卡消费的;
  24. @NetReceipt int --C.当天纯收入; --;
  25. --1.获得当天的订单数目;
  26. select @OrderCount = count(distinct id) from dindanbukuan where 0=datediff(day,date,getdate())
  27. --2.当天收款次数;
  28. select @ReceiptCount = count(id) from dindanbukuan where 0=datediff(day,date,getdate())
  29. --3.当天订单预约收款;
  30. select @Oreder1 = sum(convert(int,money)) from dindanbukuan where 0=datediff(day,date,getdate()) and bz='预约收款'
  31. --4.当天订单补款;
  32. select @Oreder2 = sum(convert(int,money)) from dindanbukuan where 0=datediff(day,date,getdate()) and bz='预约补款'
  33. --5.当天订单后期补款;
  34. select @Oreder3 = sum(convert(int,money)) from dindanbukuan where 0=datediff(day,date,getdate()) and bz not in('预约收款','预约补款')
  35. --6.当天订单使用储值卡消费金额;
  36. select @Oreder4 = sum(convert(int,money)) from dindanbukuan where 0=datediff(day,date,getdate()) and paytype = '储值卡扣款'
  37. --7.当天其他收入;
  38. select @OtherReceipts = sum(convert(float,money)) from singleincome where 0=datediff(day,dat,getdate()) and sale2type is null
  39. --8.当天支出;
  40. select @Payout = sum(convert(float,money)) from gudingfeiyong where 0=datediff(day,dat,getdate())
  41. --9.当天其他二销;
  42. select @OtherSalary = sum(convert(float,money)) from singleincomemoneyview where 0=datediff(day,date,getdate())
  43. --10.当天会员收入;
  44. select @MemberReceipts = sum(convert(float,money)) from memberview where 0=datediff(day,date,getdate())
  45. --11.当天的总收入;
  46. set @GrossReceipts = 0
  47. if @Oreder1 is not null
  48. set @GrossReceipts = @GrossReceipts + @Oreder1
  49. else
  50. set @Oreder1 = 0
  51. if @Oreder2 is not null
  52. set @GrossReceipts = @GrossReceipts + @Oreder2
  53. else
  54. set @Oreder2 = 0
  55. if @Oreder3 is not null
  56. set @GrossReceipts = @GrossReceipts + @Oreder3
  57. else
  58. set @Oreder3 = 0
  59. if @Oreder4 is not null
  60. set @GrossReceipts = @GrossReceipts - @Oreder4
  61. else
  62. set @Oreder4 = 0
  63. if @MemberReceipts is not null
  64. set @GrossReceipts = @GrossReceipts + @MemberReceipts
  65. else
  66. set @MemberReceipts = 0
  67. if @OtherReceipts is not null
  68. set @GrossReceipts = @GrossReceipts + @OtherReceipts
  69. else
  70. set @OtherReceipts = 0
  71. if @OtherSalary is not null
  72. set @GrossReceipts = @GrossReceipts + @OtherSalary
  73. else
  74. set @OtherSalary = 0
  75. --12.当天的纯收入;
  76. set @NetReceipt = (@GrossReceipts - @Payout)
  77. if @NetReceipt is null set @NetReceipt = 0
  78. select
  79. @OrderCount 'OrderCount', --当天订单数;
  80. @ReceiptCount 'ReceiptCount', --当天收款次数;
  81. @Oreder1 'OrdersReceivables', --当天订单预约收款;
  82. @Oreder2 'OrdersReplenishment', --当天订单补款;
  83. @Oreder3 'LateReplenishment', --当天订单后期补款;
  84. @Oreder4 'StoredCardSpending', --当天储值卡消费;
  85. @OtherReceipts 'OtherReceipts', --当天其他收入;
  86. @Payout 'Payout', --当天支出;
  87. @OtherSalary 'OtherSalary', --当天的其他二销;
  88. @MemberReceipts 'MemberReceipts', --当天会员收入;
  89. @GrossReceipts 'GrossReceipts', --当天的总收入;
  90. @NetReceipt 'NetReceipt' --当天的净收入;
  91. end