史上最牛得SQL查询语句

select transHeader.transnumber,
transHeader.transNumberReal, -----运单流水号,公路运输单的单号
transHeader.transType,--运单类型
         
case when transHeader.TrainNumber is null then '(公路)' + transType1.typename else '(铁路)' + transType1.typename end as TransMode, ----运输类型,
trainNumber,
CarriageNumber,   -------车次,机次位
replace(convert(varchar(16),transHeader.DepartTime,120),'-0','-') as DepartTime,-----发车时间
transHeader.StartAddress,
transHeader.EndAddress, -----对应的始发机构,终到机构
typelist1.typelistdesc1 as TailOrganization, ---运单运作机构
typelist2.typelistdesc1 as SignOrganization, ----运单签收机构
round(convert(float,isNull(ShipQty,0)),0) as ShipQty,
round(convert(float,isNull(ShipWeight,0)),3) as ShipWeight,
round(convert(float,isNull(ShipVolume,0)),3) as ShipVolume, ----件数,重量,体积
StartTime,   -----开单时间

case when OperationMode='JTL' and CarrierPickup=1 and CarrierDispatch=0 then '门到站'
when OperationMode='JTL' and CarrierDispatch=1 and CarrierPickup=0 then '站到门'
when OperationMode='JTL' and CarrierDispatch=1 and CarrierPickup=1 then '门到门'
when OperationMode='JTL' and CarrierDispatch=0 and CarrierPickup=0 then '站到站'
else '非行邮门到门' end as 运作方式,

case transStatus
when 5 then isnull(typelist7.typelistdesc1,'')+'运输中'
when 3 then isnull(typelist4.typelistdesc1,'')+'已发送'
when 7 then isnull(typelist5.typelistdesc1,'')+'已接收'
when 9 then isnull(typelist6.typelistdesc1,'')+'已签收'
else '' end as transStatus,
transHeader.Remark1,-----备注
transHeader.vendorName,  ---供应商

case when replace(convert(varchar(10),transHeader.DeliverTime,120),'-0','-')='1900-1-1' then ''
else replace(convert(varchar(16),transHeader.DeliverTime,120),'-0','-') end as DeliverTime, ---派发时间

transHeader.FourgonNumber,---车厢号
transHeader.APayable,transHeader.realExpense,----应付,实付
replace(convert(varchar(16),transHeader.closeTime,120),'-0','-') as closeTime, -----签收时间

case when transHeader.APayable-transHeader.realExpense>0 then '<font color=red>'+convert(varchar(20),transHeader.APayable-transHeader.realExpense)+'</font>'
else convert(varchar(20),transHeader.APayable-transHeader.realExpense) end as notAPayable,-----未付红色显示
        
         -- //滞留类型
case when transHeader.TrainNumber is null and transType1.typename='基地提货' and transHeader.AddWho is null then '未提货'
when transHeader.TrainNumber is null and transType1.typename='基地提货' and transHeader.signWho is null then '提货未签收'
when transHeader.TrainNumber is null and transType1.typename='基地送货' and transHeader.AddWho is null then '未送货'
when transHeader.TrainNumber is null and transType1.typename='基地送货' and transHeader.signWho is null then '送货未签收'
when transHeader.TrainNumber is null and transType1.typename='公路站到站' and transHeader.AddWho is null then '未发运'
when transHeader.TrainNumber is null and transType1.typename='公路站到站' and transHeader.AddWho is not null and transHeader.sendwho is null then '公路运单未发送'
when transHeader.TrainNumber is null and transType1.typename='公路站到站' and transHeader.AddWho is not null and transHeader.sendwho is not null  and transHeader.receiptwho is null then '公路运单未接收'
when transHeader.TrainNumber is null and transType1.typename='公路站到站' and transHeader.signWho is null and transHeader.AddWho is not null  and  transHeader.sendwho is not null  and transHeader.receiptwho is not null then '未签收'
when transHeader.TrainNumber is not null and transHeader.AddWho is null then '装车单未录入'
when transHeader.TrainNumber is not null and transHeader.receiptwho is null then '装车单未接收'
when transHeader.TrainNumber is not null and transHeader.sendwho is null then '装车单未发送'
when transHeader.TrainNumber is not null and transHeader.signWho is null then '卸车单未签收' else '' end as StopType,--滞留类型,

'' as orderArea -----订单位置区域表
        
from transHeader


left join (select * from typelist where TypeCode='Organization' ) as typelist1 on Typelist1.TypelistCode=transHeader.TailOrganization
left join (select * from typelist where TypeCode='Organization' ) as typelist2 on Typelist2.TypelistCode=transHeader.SignOrganization
left join (select * from typelist where TypeCode='TransStatus') as typelist3 on Typelist3.TypelistId=transHeader.transStatus
left join transType as transType1 on transHeader.transType=transType1.typecode
left join(
select * from typelist left join [user] on typelist.typelistcode=[user].userBelong where typelist.typecode='organization'
) typelist4 on typelist4.userid=transHeader.sendwho

left join(
select * from typelist left join [user] on typelist.typelistcode=[user].userBelong where typelist.typecode='organization'
) typelist5 on typelist5.userid=transHeader.receiptwho

left join(
select * from typelist left join [user] on typelist.typelistcode=[user].userBelong where typelist.typecode='organization'
) typelist6 on typelist6.userid=transHeader.signwho

left join(
select * from typelist left join [user] on typelist.typelistcode=[user].userBelong where typelist.typecode='organization'
) typelist7 on typelist7.userid=transHeader.modifywho


--left join OrderProcess on OrderProcess.TransNumber=transHeader.TransNumber
--left join OrderHeader on OrderHeader.OrderNumber=OrderProcess.OrderNumber
inner join(
select * from OrderHeader inner join transdetail on  OrderHeader.OrderNumber=transdetail.OrderNumber
) transdetail on transdetail.TransNumber=transHeader.TransNumber
where 1=1
order by transHeader.starttime

你可能感兴趣的:(sql)