业务统计查询问题

eeight 2013-06-03 11:00:00
有张业务表
orderTable-----------------
id 编号(code) 客户名(custome) 贷款金额(money) 出款时间(actionDate)


出回款记录表 orderIdtable 对应上表的id 类型out 表示出款 in 表示回款
recordTable-----------------
id 类型(type) 时间(actionDate) 金额(money) 单子ID(orderId)
1 out 300 1
2 in 200 1

怎么用一条语句查出所有业务的详细情况
----------------------------------------------------
单子orderId 客户customer 贷款金额 已出款金额 未出款金额 已回款金额 未回款金额


这是个放贷款的业务表,recordTable是出款和回款的记录表
...全文
509 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
eeight 2013-06-03
  • 打赏
  • 举报
回复
如果单子还没有任务出回款记录,SUM出来的会有NULL的记录, 怎么把SUM为NULL的值显示为0??? 就差这一步了。
xiaoqi7456 2013-06-03
  • 打赏
  • 举报
回复
引用 7 楼 eeight 的回复:
不用理会时间 ,只要计算金额就行
我怎么知道你的业务逻辑? 你说一句我写一句,写完之后你再说一条业务逻辑。。。 select orderId,customer ,sum(case when type='out' and actionDate <getdate() then money end) as 未出款 ,sum(case when type='out' and actionDate>getdate() then money end) as 已出款金额, sum(case when type='in' and actionDate <getdate() then money end) as 未回款 ,sum(case when type='in' and actionDate>getdate() then money end) as 已回款金额, from orderIdtable a ,recordTable b where orderTable.id = recordTable orderId group by orderId,customer
eeight 2013-06-03
  • 打赏
  • 举报
回复
不用理会时间 ,只要计算金额就行
eeight 2013-06-03
  • 打赏
  • 举报
回复
查询中至少也要出现 sun()吧 不然怎么知道1条业务多次出款后的出款总额??
xiaoqi7456 2013-06-03
  • 打赏
  • 举报
回复
select orderId,customer, 贷款金额 ,case when type='out' and actionDate <getdate() then money end as 未出款 case when type='out' and actionDate>getdate() then money end as 已出款金额, case when type='in' and actionDate <getdate() then money end as 未回款 case when type='in' and actionDate>getdate() then money end as 已回款金额, from orderIdtable a ,recordTable b where orderTable.id = recordTable orderId
引用 4 楼 eeight 的回复:
额。。。。。。。。。。。 每个业务都可以多次出款(只要款未出完),也都可以多次回款(只要还有欠钱)
eeight 2013-06-03
  • 打赏
  • 举报
回复
额。。。。。。。。。。。 每个业务都可以多次出款(只要款未出完),也都可以多次回款(只要还有欠钱)
xiaoqi7456 2013-06-03
  • 打赏
  • 举报
回复
select orderId,customer, 贷款金额 , case when type='out' then money end as 出款金额, case when type='in ' then money end as 回款金额 from orderIdtable a ,recordTable b where orderTable.id = recordTable orderId
引用 2 楼 eeight 的回复:
orderTable.id 关联 recordTable orderId
eeight 2013-06-03
  • 打赏
  • 举报
回复
orderTable.id 关联 recordTable orderId
xiaoqi7456 2013-06-03
  • 打赏
  • 举报
回复
关联字段是什么? id吗?
Andy__Huang 2013-06-03
  • 打赏
  • 举报
回复
引用 9 楼 eeight 的回复:
如果单子还没有任务出回款记录,SUM出来的会有NULL的记录, 怎么把SUM为NULL的值显示为0??? 就差这一步了。
isnull(sum(money),0)
xiaoqi7456 2013-06-03
  • 打赏
  • 举报
回复
引用 9 楼 eeight 的回复:
如果单子还没有任务出回款记录,SUM出来的会有NULL的记录, 怎么把SUM为NULL的值显示为0??? 就差这一步了。
把money 改成 isnull(money,0)

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧