有没有大神帮我改下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.,outbill.,outbill. 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() ,SUM() ,SUM()
FROM(
SELECT FMONTH,FYEAR,billName,
CASE WHEN FTypeName='前1个月' THEN FQty else 0 END AS ,
CASE WHEN FTypeName='前2个月' THEN FQty else 0 END AS ,
CASE WHEN FTypeName='前3个月' THEN FQty else 0 END AS
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