【问题标题】:SQL query to fetch values of parent to childSQL查询以获取父级到子级的值
【发布时间】:2019-03-26 15:19:52
【问题描述】:

我有 2 张桌子,ITEMTARIF。在关税表中,我有父项目和一些孩子的价格。现在我必须通过将TARIF 表加入ITEM 表来获取父级的价格。在 tarif 表中,一些父级将不存在,但在加入时,它正在为父级和子级创建一个具有“NULL”值的行。如果 TARIF 表中不存在其父项,我不想导出项目。

Item

status  item_code ga_article
-----------------------------
Parent1 1234      1234   X
child1  1234      1234 01 x
child2  1234      1234 02 x
parent2 2345      2345   X
child21 2345      2345 01 X
child22 2345      2345 02 x
parent3 3456      3456  X
child31 3456      3456 01 X

tarif

item_code gf_article  price
----------------------------
1234      1234  X     100
2345      2345  X     150
2345      2345 01 X   200

现在当我将TARIF 加入Item 表以获取价格时

select 
    ga_article,
    case 
       when t.price is null and i.ga_article like i.item_code +'X%' 
          then (select top(1) price from tarif 
                where GF_ARTICLE like item-code + '%' ) 
          else price
    end as amount
from 
    article
left join 
    TARIF on gf_article = ga_article 

我的输出是:

ga_article  amount
-------------------
1234  X      100
1234 01 x    100
1234 02 x    100
2345  X      150
2345 01 x    200
2345 02 x    150
3456  X      null
3456 01 x    null

我不想看到最后两行的值为空

SELECT
    CASE 
       WHEN GF_PRIXUNITAIRE IS NULL
            AND ga_article LIKE ga_Codearticle + '%X' 
          THEN (SELECT TOP(1) GF_PRIXUNITAIRE FROM tarif 
                WHERE GF_ARTICLE LIKE ga_Codearticle + '%' 
                  AND GF_DEVISE='QAR' ) 
          ELSE GF_PRIXUNITAIRE
    END AS price
FROM  
    Article A 
LEFT JOIN 
    tarif L ON gf_article = GA_ARTICLE 
            AND GF_DEVISE = 'QAR' 
            AND GF_REGIMEPRIX = 'TTC' 
WHERE
    GA_STATUTART <> 'UNI' AND GA_CODEARTICLE <> ''

我的预期输出是:

ga_article  amount
---------------------
1234  X      100
1234 01 x    100
1234 02 x    100
2345  X      150
2345 01 x    200
2345 02 x    150

【问题讨论】:

  • 也许使用inner join 而不是left join
  • 在 where 子句中再包含一个条件 -> where amount 不为空
  • 如果我使用 Inner join ,我无法将父级价格传递给子级
  • 谢谢 Marc_s ,Sahi 回答我

标签: sql sql-server tsql


【解决方案1】:

请试试这个

SELECT * FROM
(select ga_article,
case when t.price is null and i.ga_article like i.item_code +'X%' then(SELECT TOP(1) price FROM tarif WHERE GF_ARTICLE like item-code+ '%' ) 
else price
end as amount
from article
left join TARIF on gf_article = ga_article ) AS A WHERE A.amount IS NOT NULL

【讨论】:

    【解决方案2】:

    你可以结束你的查询

    from article a
    join tarif t on t.item_code = a.item_code
    

    因此,使用下面作为通过item_code 列进行内部连接的连接

    select ga_article,
           case
             when t.price is null and i.ga_article like i.item_code + 'X%' then
              (SELECT TOP(1) price
                 FROM tarif
                WHERE GF_ARTICLE like item - code + '%')
             else
              price
           end as amount
      from article a
      join tarif t on t.item_code = a.item_code
    

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      相关资源
      最近更新 更多