SQL 语句 新人求助!
一个表中数据按日,月统计。两条语句合并。表为zjlrestmp
VarDate 为DTPicker1.value
sqla为按日统计 sqlB为按月统计
AddCboBFname 一个函数 (如果COMBO控件不是空的,就增加相应的字段,
老调试不出来!
sqlA = "select * from zjlrestmp where Left(CZDAT,8)='" & Format(VarDate, "yyyymmdd") & "' and bfname= '" & strBFname & "'and BWTAR=0 and pzflag=0 "
sqlB = "select * from zjlrestmp where Left(CZDAT,6)='" & Format(VarDate, "yyyymm") & "' and Left(CZDAT,8)<='" & Format(VarDate, "yyyymmdd") & "' and bfname= '" & strBFname & "'BWTAR=0 and pzflag=0 "
If strMaktxx <> "" Then
sqlA = "select * from (" & sqlA & ") as g where " & AddCboBFname(strMaktxx, "MAKTXX")
sqlB = "select * from (" & sqlB & ") as h where " & AddCboBFname(strMaktxx, "MAKTXX")
End If
sqlA = "select * from (" & sqlA & ")"
sqlB = "select * from (" & sqlB & ")"
m = InStr(Trim(sqlA), "from")
sqlA = Right(Trim(sqlA), Len(Trim(sqlA)) - m + 1)
n = InStr(Trim(sqlB), "from")
sqlB = Right(Trim(sqlB), Len(Trim(sqlB)) - n + 1)
sqlA = "SELECT a.MAKTXX as maktxx, Count(a.CHEHAO) AS ch, Sum(a.MZQTY)-Sum(a.PZQTY) as sz, Sum(a.jZQTY) AS jz " & sqlA & " as a"
sqlB = "SELECT b.MAKTXX as maktxx, Count(b.CHEHAO) AS ch, Sum(b.MZQTY)-Sum(b.PZQTY) as sz, Sum(b.jZQTY) AS jz " & sqlB & "as b "
sql = "select c.maktxx as maktxx,c.ch as ach,c.sz as asz,c.jz as ajz,d.maktxx as bmaktxx,d.ch as bch,d.sz as bsz,d.jz as bjz FROM (" & sqlA & ") as c right join (" & sqlB & ") as d on c.maktxx=d.maktxx"
sql = "select "", e.maktxx,e.ach ,e.asz,e.ajz,e.bch ,e.bsz ,e.bjz from (" & sql & ") as e group by e.maktxx with rollup"