2

我有三张桌子。

  • tax_master
  • item_master
  • item_tax

其中的值是这样的。

*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

4

3 回答 3

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:我认为奖品应该是价格;)

于 2013-08-23T10:42:32.570 回答
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;

演示在这里

于 2013-08-23T11:07:38.237 回答
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

SQL 小提琴示例

于 2013-08-23T11:17:29.993 回答