我有三张桌子。
其中的值是这样的。
*tax_master*
tax_id tax_name tax_value
--------------------------
1 Vat 5
2 LBT 8
*item_master*
item_id Prise
---------------
1 30
2 100
*item_tax*
item_tax_id item_id tax_id
------------------------------
1 1 1
2 2 2
3 1 2
现在我想要这样的输出。
item_id prise VAT LBT Total_prise
---------------------------------------
1 30 1.5 2.4 33.9
2 100 - 8 108
增值税值的计算方式与5/30*100
上的5%
类似,30=1.5
答案 0 :(得分:0)
以下语句应按您希望的方式返回数据:
SELECT DISTINCT
item_master.item_id,
(SELECT
tax_master.tax_value * item_master.prise / 100
FROM
item_tax, tax_master
WHERE
item_tax.item_id = item_master.item_id and
tax_master.tax_id = item_tax.tax_id and
tax_master.tax_name = 'VAT') as VAT,
(SELECT
tax_master.tax_value * item_master.prise / 100
FROM
item_tax, tax_master
WHERE
item_tax.item_id = item_master.item_id and
tax_master.tax_id = item_tax.tax_id and
tax_master.tax_name = 'LBT') as LBT
FROM
item_tax, item_master, tax_master
WHERE
item_master.item_id = item_tax.item_id and
tax_master.tax_id = item_tax.tax_id
bwt:我认为奖应该是价格;)
答案 1 :(得分:0)
select item_id, price,
(min(case when tax_name = 'VAT' then tax end)) vat,
(min(case when tax_name = 'LBT' then tax end)) lbt,
coalesce(min(case when tax_name = 'VAT' then tax end),0) +
coalesce(min(case when tax_name = 'LBT' then tax end),0) +
price total
from
(select a.item_id item_id,
c.tax_name tax_name,
(c.tax_value * b.price / 100) tax,
b.price price
from item_tax a inner join item_master b on a.item_id = b.item_id
inner join tax_master c on a.tax_id = c.tax_id) as calc
group by item_id, price;
演示here。
答案 2 :(得分:0)
编辑:我尝试了一个小提琴,你需要一个MAX(或MIN)来组合它们在左边连接的顶部:
SELECT item_master.item_id,
MAX(item_master.price * tax_master_vat.tax_value / 100) as VAT,
MAX(item_master.price * tax_master_lbt.tax_value / 100) as LBT
FROM item_master
LEFT JOIN item_tax ON item_master.item_id = item_tax.item_id
LEFT JOIN tax_master tax_master_vat ON item_tax.tax_id = tax_master_vat.tax_id
AND tax_master_vat.tax_name = 'VAT'
LEFT JOIN tax_master tax_master_lbt ON item_tax.tax_id = tax_master_lbt.tax_id
AND tax_master_lbt.tax_name = 'LBT'
GROUP BY item_master.item_id
如果没有为某个项链接tax_value,它应该返回NULL(乘以NULL为NULL)。如果你想显示与'null'不同的东西(或什么都不是),你可以使用COALESCE。如果您想包含总价:
如果您想要添加价格或总数,可以在item_master表中加入:
SELECT item_master.item_id,
tax.VAT,
tax.LBT,
item_master.price + COALESCE(tax.VAT, 0) + COALESCE(tax.LBT, 0) AS Total
FROM item_master
LEFT JOIN ( SELECT item_master.item_id,
MAX(item_master.price * tax_master_vat.tax_value / 100) as VAT,
MAX(item_master.price * tax_master_lbt.tax_value / 100) as LBT
FROM item_master
LEFT JOIN item_tax ON item_master.item_id = item_tax.item_id
LEFT JOIN tax_master tax_master_vat ON item_tax.tax_id = tax_master_vat.tax_id
AND tax_master_vat.tax_name = 'VAT'
LEFT JOIN tax_master tax_master_lbt ON item_tax.tax_id = tax_master_lbt.tax_id
AND tax_master_lbt.tax_name = 'LBT'
GROUP BY item_master.item_id ) tax ON item_master.item_id = tax.item_id
或者,您可以使用OVER PARTITION BY子句(仅允许您对item_id进行分组,但仍然包含价格字段):
SELECT item_id,
price,
VAT,
LBT,
price + COALESCE(VAT, 0) + COALESCE(LBT, 0) AS Total
FROM ( SELECT DISTINCT
item_master.item_id,
item_master.price,
MAX(item_master.price * tax_master_vat.tax_value / 100) OVER (PARTITION BY item_master.item_id) as VAT,
MAX(item_master.price * tax_master_lbt.tax_value / 100) OVER (PARTITION BY item_master.item_id) as LBT
FROM item_master
LEFT JOIN item_tax ON item_master.item_id = item_tax.item_id
LEFT JOIN tax_master tax_master_vat ON item_tax.tax_id = tax_master_vat.tax_id
AND tax_master_vat.tax_name = 'VAT'
LEFT JOIN tax_master tax_master_lbt ON item_tax.tax_id = tax_master_lbt.tax_id
AND tax_master_lbt.tax_name = 'LBT'
) price_and_tax