有没有大神帮我改下SQL,添加[年],[月]两个字段


有没有大神帮我改下SQL,添加[年],[月]两个字段。我自己尝试了,但是报错。[当月日期]格式是(yyyy-MM-dd)。原SQL:
select [分公司],[当月日期],SUM([库存]) [库存],SUM([当月销量]) [当月销量],SUM([前一个月销量]) [前一个月销量],SUM([前二个月销量]) [前二个月销量] from(
select resultQTY.soncomp AS [分公司],resultQTY.FQTY AS [库存],resultQTY.当月日期,
isnull(out1.FQty,0) AS [当月销量],isnull(out2.FQty,0) AS [前一个月销量],isnull(out3.FQty,0) AS [前二个月销量]
from(
select result.soncomp,result.FQTY,
FDate [当月日期],
DATEADD(MONTH,-1,FDate) AS [前一个月日期],DATEADD(MONTH,-2,FDate) AS [前二个月日期]
from(
select cast(FYear AS nvarchar(5))+'-'+cast(FPeriod AS nvarchar(5))+'-01' AS FDate,soncomp,SUM(FEndQty) AS FQTY
from ICBal where soncomp in(select billName from billNameType where billType = '分公司')
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%') AND FYear>=2016
group by FYear,FPeriod,soncomp)result
)resultQTY
LEFT JOIN(select result.billName,
DATEADD(MONTH,1,DATEADD(MONTH,-1,result.FDate)) AS Fdate,result.FQty
from(
select vwICBill_8.billName,SUM(isnull(FQty,0)) FQty,
cast(YEAR(Fdate) AS nvarchar(5))+'-'+cast(MONTH(Fdate) AS nvarchar(5))+'-01' AS FDate
from vwICBill_8
LEFT JOIN billNameType ON(vwICBill_8.billName = billNameType.billName)
where FTranType = 21 AND billNameType.billType='分公司'
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%')
AND FROB !=-1 AND Fdate>= '2016-05-01'
group by vwICBill_8.billName,YEAR(Fdate),MONTH(Fdate))result
)out1
ON(out1.Fdate =resultQTY.[当月日期] AND out1.billName = resultQTY.soncomp)
LEFT JOIN(select result.billName,
DATEADD(MONTH,1,DATEADD(MONTH,-1,result.FDate)) AS Fdate,result.FQty
from(
select vwICBill_8.billName,SUM(isnull(FQty,0)) FQty,
cast(YEAR(Fdate) AS nvarchar(5))+'-'+cast(MONTH(Fdate) AS nvarchar(5))+'-01' AS FDate
from vwICBill_8
LEFT JOIN billNameType ON(vwICBill_8.billName = billNameType.billName)
where FTranType = 21 AND billNameType.billType='分公司'
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%')
AND FROB !=-1 AND Fdate>= '2016-05-01'
group by vwICBill_8.billName,YEAR(Fdate),MONTH(Fdate))result
)out2
ON(out2.Fdate =resultQTY.[前一个月日期] AND out2.billName = resultQTY.soncomp)
LEFT JOIN(select result.billName,
DATEADD(MONTH,1,DATEADD(MONTH,-1,result.FDate)) AS Fdate,result.FQty
from(
select vwICBill_8.billName,SUM(isnull(FQty,0)) FQty,
cast(YEAR(Fdate) AS nvarchar(5))+'-'+cast(MONTH(Fdate) AS nvarchar(5))+'-01' AS FDate
from vwICBill_8
LEFT JOIN billNameType ON(vwICBill_8.billName = billNameType.billName)
where FTranType = 21 AND billNameType.billType='分公司'
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%')
AND FROB !=-1 AND Fdate>= '2016-05-01'
group by vwICBill_8.billName,YEAR(Fdate),MONTH(Fdate))result)out3
ON(out3.Fdate =resultQTY.[前二个月日期] AND out3.billName = resultQTY.soncomp)
)result
group by [分公司],[当月日期]
union all
--------及时库存
select result.制单,result.FDate,result.库存,outbill.[前1个月销量],outbill.[前2个月销量],outbill.[前3个月销量] from(
select ICInventory.billName AS [制单],cast(YEAR(GETDATE()) AS nvarchar(5))+'-'+cast(MONTH(GETDATE()) AS nvarchar(5))+'-01' AS FDate,SUM(isnull(FQty,0)) AS [库存]
from ICInventory
LEFT JOIN billNameType ON(ICInventory.billName = billNameType.billName)
where DateTime = CONVERT(varchar(10),getdate(),120)
AND FStockType = '实仓' AND company='成品仓' AND billNameType.billType='分公司'
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%')
GROUP BY ICInventory.billName)result
LEFT JOIN(
------前3个月出货
SELECT billName AS [制单],
SUM([前1个月销量]) [前1个月销量] ,SUM([前2个月销量]) [前2个月销量],SUM([前3个月销量]) [前3个月销量]
FROM(
SELECT FMONTH,FYEAR,billName,
CASE WHEN FTypeName='前1个月' THEN FQty else 0 END AS [前1个月销量],
CASE WHEN FTypeName='前2个月' THEN FQty else 0 END AS [前2个月销量],
CASE WHEN FTypeName='前3个月' THEN FQty else 0 END AS [前3个月销量]
FROM(
SELECT outbill.FMONTH,outbill.FYEAR,billName,fdate.FTypeName,SUM(isnull(FQty,0)) AS FQty
FROM(
select vwICBill_8.billName,SUM(isnull(FQty,0)) FQty,YEAR(Fdate) AS FYEAR,MONTH(Fdate) AS FMONTH
from vwICBill_8
LEFT JOIN billNameType ON(vwICBill_8.billName = billNameType.billName)
where FTranType = 21 AND billNameType.billType='分公司'
AND (FNumber like 'F00%' or FNumber like 'F01%' or FNumber like 'F11%')
AND FROB !=-1 AND Fdate>= DATEADD(MONTH,-6,GETDATE())
group by vwICBill_8.billName,YEAR(Fdate),MONTH(Fdate)
)outbill LEFT JOIN(select '前'+convert(varchar,id)+'个月' AS FTypeName,Fyear,FMonth from CAIGOUJUECE_generateTime())fdate
ON(outbill.FMONTH = fdate.FMonth AND outbill.FYEAR = fdate.Fyear)
group by outbill.FMONTH,outbill.FYEAR,billName,fdate.FTypeName)result)result1
group by billName)outbill ON(result.制单 = outbill.制单)
order by [当月日期] desc
FineReport果果果 发布于 2018-8-6 14:22
悬赏:2 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
0
axing发布于2018-8-6 14:22(编辑于 2023-9-6 09:34)
555
最佳回答
0
Paul_yau发布于2018-8-6 14:36(编辑于 2023-9-6 09:34)
555
  • 0关注人数
  • 438浏览人数
  • 最后回答于:2018-8-6 14:48
    活动推荐 更多
    热门课程 更多
    返回顶部