t-sql 随记

本文详细阐述了如何使用SQL查询语句进行库存数据分析,包括计算平均销售价格、成本统计和库存状态分类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

with _t as(select i.id,i.name,
case
when (i.InAmountTotal - i.OutAmountTotal) > 0 and (i.InCostTotal - i.OutCostTotal) > 0
then convert(numeric(18,4),(i.InCostTotal - i.OutCostTotal) / (i.InAmountTotal - i.OutAmountTotal+1))
else 0 end as salepriceavg,

case when i.incost<=0 or i.incost is null then 0 else i.incost end as incost,count(s.id) as totalcount
from dbo.ExtendCDKItem as i
left join [ExtendCDKItemStore] as s
on s.itemid=i.id
where 1=1
 and 1&i.flag<>1 group by i.id,i.name,i.incost,i.incosttotal,i.outcosttotal,i.inamounttotal,i.outamounttotal)
select *,(select count(id) from ExtendCDKItemStore as s where s.itemid=_t.id and
1&s.flag<>1 and 2&s.flag<>2 and
(s.saleorderid is null or s.saleorderid='')) as normal,(select sum(oi.amount)
from dbo.ExtendCDKSaleOrderItem as oi left join dbo.ExtendCDKSaleOrder as o on o.id=oi.orderid
where oi.itemid=_t.id and 1&o.flag<>1 and 2&o.flag<>2
and 1&oi.flag<>1) as waitingsend,(select count(id) from ExtendCDKItemStore as
s where s.itemid=_t.id and 1&s.flag<>1 and 2&s.flag=2
and (s.saleorderid is null or s.saleorderid='')) as problem,(select count(id) from ExtendCDKItemStore as s
where s.itemid=_t.id and 1&s.flag=1) as invalid,(select count(id)
from ExtendCDKItemStore as s where s.itemid=_t.id and 1&s.flag<>1
and 2&s.flag<>2 and (s.saleorderid is not null and s.saleorderid<>''))
as saled from _t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值