日期:2014-05-18  浏览次数:20563 次

关于行列转换数据出错

SELECT   DISTINCT   DAY(tb1.RiQi),  
SUM(CASE   WHEN   tb1.Name_BuMen   =   '厨部 '   AND   tb2.FangShi   =   '现金 '   THEN   tb1.JinE_Zhe   END)   AS   XJ1,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '海鲜 '   AND   tb2.FangShi   =   '现金 '   THEN   tb1.JinE_Zhe   END)   AS   XJ2,  
SUM(CASE   WHEN   tb1.Name_BuMen   =   '酒水 '   AND   tb2.FangShi   =   '现金 '   THEN   tb1.JinE_Zhe   END)   AS   XJ3,  
SUM(CASE   WHEN   tb1.Name_BuMen   =   '其他 '   AND   tb2.FangShi   =   '现金 '   THEN   tb1.JinE_Zhe   END)   AS   XJ4,  
SUM(CASE   WHEN   tb1.Name_BuMen   =   '厨部 '   AND   tb2.FangShi   =   '签单 '   THEN   tb1.JinE_Zhe   END)   AS   QD1,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '海鲜 '   AND   tb2.FangShi   =   '签单 '   THEN   tb1.JinE_Zhe   END)   AS   QD2,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '酒水 '   AND   tb2.FangShi   =   '签单 '   THEN   tb1.JinE_Zhe   END)   AS   QD3,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '其他 '   AND   tb2.FangShi   =   '签单 '   THEN   tb1.JinE_Zhe   END)   AS   QD4,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '厨部 '   AND   tb2.FangShi   =   '免单 '   THEN   tb1.JinE_Zhe   END)   AS   MD1,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '海鲜 '   AND   tb2.FangShi   =   '免单 '   THEN   tb1.JinE_Zhe   END)   AS   MD2,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '酒水 '   AND   tb2.FangShi   =   '免单 '   THEN   tb1.JinE_Zhe   END)   AS   MD3,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '其他 '   AND   tb2.FangShi   =   '免单 '   THEN   tb1.JinE_Zhe   END)   AS   MD4,
SUM(CASE   WHEN   tb2.Fangshi= '现金 '   THEN   tb2.JINE   END)   AS   FS1,
SUM(CASE   WHEN   tb2.Fangshi= '签单 '   THEN   tb2.JINE   END)   AS   FS2,
SUM(CASE   WHEN   tb2.Fangshi= '免单 '   THEN   tb2.JINE   END)   AS   FS3,
SUM(tb2.JINE)   AS   FSHJ,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '厨部 '   THEN   tb1.JinE_Zhe   END)   AS   FL1,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '海鲜 '   THEN   tb1.JinE_Zhe   END)   AS   FL2,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '酒水 '   THEN   tb1.JinE_Zhe   END)   AS   FL3,
SUM(CASE   WHEN   tb1.Name_BuMen   =   '其他 '   THEN   tb1.JinE_Zhe   END)   AS   FL4,
SUM(CASE   WHEN   tb2.Fangshi= '现金 '   OR   tb2.FANGSHI= '签单 '   THEN   tb2.JINE   END)   AS   FLHJ
FROM   tb1   INNER   JOIN   tb2   ON   tb2.BH   =   tb1.BH
WHERE   tb1.RIQI   > =@STDATE   AND   tb1.RIQI   <=   @ENDATE