sql代码书写规范 及子查询分析

--sql书写规范,注意行的缩进

select f.goodscode

, f.goodsname
, f.goodsspec
, n.orgname as ownername
, h.stornum
, m.whname
, f.manufacturer
, f.place
, g.dates 

#if (dbtype == "Microsoft SQL Server"){ 
,(case when g.dates is not null then datediff(dd,g.dates,getdate()) else 999 end)  as zxday//sql server中计算日期差
#}else if (dbtype == "Oracle" || dbtype.indexof("db2") >= 0){ 
,(case when g.dates is not null then trunc(sysdate-to_date(g.dates,'yyyy-mm-dd')) else 999 end) as zxday //oracle计算日期差
#}else if (dbtype == "MySQL"){
,(case when g.dates is not null then datediff(dd,g.dates,getdate()) else 999 end)  as zxday
#}

from
goodsdoc f
left join (

select c.goodsid
 , e.dates
from
goodacctdt c
join ( select max(a.serialnum) as serialnum
, a.goodsid
  from
  goodacctdt a
  join goodacctmt b
  on a.acctpageid = b.acctpageid and a.entid = b.entid
  where
  b.ruleid in ('4szcn61oa8b0tz79', '0pfridb711yiiomu')//查询最大编号,根据goodsid分组。
  group by

  a.goodsid) d //把这个子查询里面的的数据当作一个表,然后用明细表关联主表,然后把它当作一个表用join连接起来,子查询的作用是确定过滤后的最大

//编号,用过滤后的明细管理汇总,取得汇总中的日期,这样就求的了每种商品分组收的最大编号的记录和对象汇总表中的日期字段显示。

on c.serialnum = d.serialnum and c.goodsid = d.goodsid
join goodacctmt e

on c.acctpageid = e.acctpageid and c.entid = e.entid) g//从goodsdoc中取得所有商品,然后把g里面的作为一个表来管理。用left管理,如果有如果能关联出来

//显示日期,如果没有则显示空。
on f.goodsid = g.goodsid and f.entid = f.entid
join storbalance h//取库存
on f.goodsid = h.goodsid and f.entid = h.entid
join storehouse m
on h.whid = m.whid and h.entid = m.entid
join orgdoc n
on h.ownerid = n.orgid and h.entid = n.entid

on f.goodsid = g.goodsid and f.entid = f.entid
join storbalance h//取库存
on f.goodsid = h.goodsid and f.entid = h.entid
join storehouse m
on h.whid = m.whid and h.entid = m.entid
join orgdoc n
on h.ownerid = n.orgid and h.entid = n.entid

where
1 = 1
and h.whid = :whid
#if(typeof goodsid != "undefined") {//js中给数据库中传值
#if(goodsid != ""){
#__sql += " and f.goodsid = '" + goodsid +"'"
#}
#}
order by
zxday,f.goodscode
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值