表结构如下:只把主要字段列出

表一:付款记录表  Gather

        字段:GatherID ,

       AccountID,

                PayMents 金额,

                PayWay  付款方式 1 现金 2 刷卡 

 表2:销售记录表 Account

  字段:AccountID

     仓库:StorageID

     时间:BeginTime   销售时间

 表3:仓库表 Storage

  字段 StorageID

    StorageName

现要求查出某个仓库某给时间段 总营业收入,现金收入,刷卡收入

SQL语句如下:

SELECT S.StorageName,S.StoreageID SUM(G.PayMents) as Total ,SUM(T.PayMents) as Cash,SUM(H.PayMents) as Card

FROM Storage S,Account A, Gather G

LEFT JOIN Gather T ON T.GatherID=G.GatherID AND T.PayWay='1'

LEFT JOIN Gather H ON H.GatherID=G.GatherID AND H.PayWay='2'

WHERE  S.StorageID=A.StorageID AND A.AccountID=G.AccountID AND A.BeginTime>'2016-01-01' AND A.BeginTime<'2016-01-10' AND S.StorageID=1123