|
- SELECT "库存",* FROM (select "男" AS 性别,门店,男比率 AS 比率 FROM (SELECT C.门店,C.女数/(C.女数+D.男数) AS 女比率,D.男数/(C.女数+D.男数) AS 男比率 FROM (SELECT A.门店,B.女数 FROM
- (select distinct 门店 FROM [库存$] WHERE 门店 IS NOT NULL)A
- LEFT JOIN
- (select 门店,SUM(库存值) AS 女数 from [库存$] WHERE 性别="女" GROUP BY 门店)B
- ON A.门店=B.门店)C
- LEFT JOIN
- (select 门店,SUM(库存值) AS 男数 from [库存$] WHERE 性别="男" GROUP BY 门店)D
- ON C.门店=D.门店)
- UNION ALL
- select "女" AS 性别,门店,女比率 FROM (SELECT C.门店,C.女数/(C.女数+D.男数) AS 女比率,D.男数/(C.女数+D.男数) AS 男比率 FROM (SELECT A.门店,B.女数 FROM
- (select distinct 门店 FROM [库存$] WHERE 门店 IS NOT NULL)A
- LEFT JOIN
- (select 门店,SUM(库存值) AS 女数 from [库存$] WHERE 性别="女" GROUP BY 门店)B
- ON A.门店=B.门店)C
- LEFT JOIN
- (select 门店,SUM(库存值) AS 男数 from [库存$] WHERE 性别="男" GROUP BY 门店)D
- ON C.门店=D.门店))
- UNION ALL
- SELECT "销售",* FROM (select "男" AS 性别,门店名称,男比率 AS 比率 FROM (SELECT C.门店名称,C.女数/(C.女数+D.男数) AS 女比率,D.男数/(C.女数+D.男数) AS 男比率 FROM (SELECT A.门店名称,B.女数 FROM
- (select distinct 门店名称 FROM [销售$] WHERE 门店名称 IS NOT NULL)A
- LEFT JOIN
- (select 门店名称,SUM(金额) AS 女数 from [销售$] WHERE 性别="女" GROUP BY 门店名称)B
- ON A.门店名称=B.门店名称)C
- LEFT JOIN
- (select 门店名称,SUM(金额) AS 男数 from [销售$] WHERE 性别="男" GROUP BY 门店名称)D
- ON C.门店名称=D.门店名称)
- UNION ALL
- select "女" AS 性别,门店名称,女比率 FROM (SELECT C.门店名称,C.女数/(C.女数+D.男数) AS 女比率,D.男数/(C.女数+D.男数) AS 男比率 FROM (SELECT A.门店名称,B.女数 FROM
- (select distinct 门店名称 FROM [销售$] WHERE 门店名称 IS NOT NULL)A
- LEFT JOIN
- (select 门店名称,SUM(金额) AS 女数 from [销售$] WHERE 性别="女" GROUP BY 门店名称)B
- ON A.门店名称=B.门店名称)C
- LEFT JOIN
- (select 门店名称,SUM(金额) AS 男数 from [销售$] WHERE 性别="男" GROUP BY 门店名称)D
- ON C.门店名称=D.门店名称))
复制代码 SQL参考:请验证 |
|