在Firebird 2.5服务器DB上有表:商品,收入,销售
货物表有列:goods_id,名称
收入:Income_id,Goods_id,incomeqty(数量),recdate
销售:sales_id,income_id,Goods_id,saleqty,recdate
我有查询在日期间隔中选择它们之间的收入,销售和差异,并且它可以正常工作:
select income.goods_id,
sum(case when which = 'income' then sum_amt else 0 end) as sum_income,
sum(case when which = 'sale' then sum_amt else 0 end) as sum_sale,
sum(case when which = 'income' then sum_amt else 0 end)
- sum(case when which = 'sale' then sum_amt else 0 end) as differ_between
from (select goods_id, sum(incomeqty) as sum_amt, 'income' as which
from income
where income.recdate betwwen :d1 and :d2
group by pr_k
union all
select goods_id, sum(salesqty), 'sale'
from sales
where sale.recdate between :d1 and :d2
group by goods_id) x
group by goods_id
但我想从商品表中另外选择商品名称。如何包括" name"此查询中的column和if子句(" where goods_id = income.goods_id")?
答案 0 :(得分:1)
简单添加联接
<!-- Add mousewheel plugin (this is optional) -->
<script type="text/javascript" src="/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
<!-- Add fancyBox -->
<link rel="stylesheet" href="/fancybox/source/jquery.fancybox.css" type="text/css" media="screen" />
<script type="text/javascript" src="/fancybox/source/jquery.fancybox.pack.js"></script>
<!-- Optionally add helpers - button, thumbnail and/or media -->
<link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-buttons.css" type="text/css" media="screen" />
<script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-buttons.js"></script>
<script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-media.js"></script>
<link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-thumbs.css" type="text/css" media="screen" />
<script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-thumbs.js"></script>>
但我不知道如果不包括收入,您发布的查询如何工作“select income.goods_id”?
修改后的版本以支持您的评论
select x.goods_id, G.name,
sum(case when which = 'income' then sum_amt else 0 end) as sum_income,
sum(case when which = 'sale' then sum_amt else 0 end) as sum_sale,
sum(case when which = 'income' then sum_amt else 0 end)
- sum(case when which = 'sale' then sum_amt else 0 end) as differ_between
from (select goods_id, sum(incomeqty) as sum_amt, 'income' as which
from income
where income.recdate betwwen :d1 and :d2
group by pr_k
union all
select goods_id, sum(salesqty), 'sale'
from sales
where sale.recdate between :d1 and :d2
group by goods_id) x
INNER JOIN GOODS G ON G.goods_id=x.goods_id
group by x.goods_id, G.name
但我想更好的是将“group by”和“select”中的字段顺序更改为:
select x.goods_id, G.name,
sum(case when which = 'income' then sum_amt else 0 end) as sum_income,
sum(case when which = 'sale' then sum_amt else 0 end) as sum_sale,
sum(case when which = 'income' then sum_amt else 0 end)
- sum(case when which = 'sale' then sum_amt else 0 end) as differ_between
from
GOODS G LEFT JOIN
(select goods_id, sum(incomeqty) as sum_amt, 'income' as which
from income
where income.recdate betwwen :d1 and :d2
group by pr_k
union all
select goods_id, sum(salesqty), 'sale'
from sales
where sale.recdate between :d1 and :d2
group by goods_id) x ON G.goods_id=x.goods_id
group by x.goods_id, G.name