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

求sql语句,大家帮帮我
有1个销售表和一个pos销售表,我想做一个销售结构分析表,就是每个货品销售额占总销售额的百分比。现在问题是,每个单独的表的百分比我可以做出来,但是要把销售表和pos销售表合起来算百分比就不会了,下面是我单独每个表的语句,大家看看怎么写能把这2个表的结果合起来(通俗说就是,我要做一个销售结构分析表,但是销售分正常销售和pos销售分放在各自的表里,我要把正常销售和pos销售一起做一个销售分析表)
下面是我做的每个单独表的分析表:
正常销售
SELECT   l_goods.name   as   货品,   SUM(s_saledetail.price   *   s_saledetail.quantity)   AS   销售额,  
            SUM(s_saledetail.price   *   s_saledetail.quantity)   /
                    (SELECT   SUM(s_saledetail.price   *   s_saledetail.quantity)
                  FROM   s_sale   INNER   JOIN
                              s_saledetail   ON   s_sale.billid   =   s_saledetail.billid   INNER   JOIN
                              l_goods   ON   s_saledetail.goodsid   =   l_goods.goodsid)   *   100   AS   所占比率,  
            SUM(s_saledetail.quantity)   AS   数量
FROM   s_sale   INNER   JOIN
            s_saledetail   ON   s_sale.billid   =   s_saledetail.billid   INNER   JOIN
            l_goods   ON   s_saledetail.goodsid   =   l_goods.goodsid
GROUP   BY   l_goods.name   WITH   cube

pos销售:
SELECT   l_goods.name   AS   货品,   SUM(pos_saledetail.price   *   pos_saledetail.quantity)  
            AS   销售额,   SUM(pos_saledetail.quantity)   AS   数量,  
            SUM(pos_saledetail.price   *   pos_saledetail.quantity)   /
                    (SELECT   SUM(pos_saledetail.price   *   pos_saledetail.quantity)
                  FROM   pos_saledetail   INNER   JOIN
                              l_goods   ON   pos_saledetail.goodsid   =   l_goods.goodsid)  
            *   100   AS   所占比率
FROM   pos_saledetail   INNER   JOIN
            l_goods   ON   pos_saledetail.goodsid   =   l_goods.goodsid
GROUP   BY   l_goods.name   WITH   cube

希望大家能帮帮我


------解决方案--------------------
最好举例说明表结构和要求的报表格式

或者自己看看
union [all]
的帮助

------解决方案--------------------
try:
先把这两表用UNION ALL查询连起一个表再在这个表里查询结果按你上面的方法就行了
------解决方案--------------------
select 货品,sum(销售额) AS 销售额,sum(销售额)/sum(总销售额)*100% as 所占比率,sum(数量) AS 数量
(
SELECT l_goods.name as 货品, SUM(s_saledetail.price * s_saledetail.quantity) AS 销售额,
(SELECT SUM(s_saledetail.price * s_saledetail.quantity)
FROM s_sale INNER JOIN
s_saledetail ON s_sale.billid = s_saledetail.billid INNER JOIN
l_goods ON s_saledetail.goodsid = l_goods.goodsid) AS 总销售额,
SUM(s_saledetail.quantity) AS 数量
FROM s_sale INN