左连接不会产生预期的结果

时间:2015-11-05 20:41:16

标签: sql sql-server join left-join

我有这个奇怪的问题。下面是我的代码的最佳表示,除了代替A_和B_是我的代码中的公用表表达式。有人建议外部申请可能是一个更好的主意,但我只是感到困惑,我在第一时间错了。 我理解这很多代码,但我认为这也可以帮助其他人

.emacs.customization.el

但是当我查看我在上面加入Consumer表的每个表时,我得到了这个

Select c.consumerID,
   ,CASE 
          WHEN lacp.LowestAnnualCost  IS NOT NULL THEN  
                 CASE 
                       WHEN lcp.LowestAnnualCost < lacp.LowestAnnualCost THEN 
                              lcp.LowestAnnualCost 
                       ELSE lacp.LowestAnnualCost  
                 END
          ELSE
                 lcp.LowestAnnualCost 
   END
   AS LowestAnnualCost
From Consumer c  
left join A_ lcp on c.ConsumerID = lcp.ConsumerID
left join B_ lacp on lcp.ConsumerID = lacp.ConsumerID

ConsumerID LowestAnnualCost
1               23
2               19
3               NULL
4               NULL
5               25

2 个答案:

答案 0 :(得分:1)

我不得不对你的消费者表做一些假设,但除此之外,试试这个......

CREATE TABLE #Consumer (ConsumerID int IDENTITY(1,1) NOT NULL, Consumer_Name nvarchar(max) null)
CREATE TABLE #A_ (ConsumerID int NULL, LowestAnnualCost int NULL)
CREATE TABLE #B_ (ConsumerID int NULL, LowestAnnualCost int NULL)


INSERT INTO #Consumer (Consumer_Name) VALUES ('Bob')
INSERT INTO #Consumer (Consumer_Name) VALUES ('Mike')
INSERT INTO #Consumer (Consumer_Name) VALUES ('Larry')
INSERT INTO #Consumer (Consumer_Name) VALUES ('Kevin')
INSERT INTO #Consumer (Consumer_Name) VALUES ('Brian')


INSERT INTO #A_ (ConsumerID, LowestAnnualCost) VALUES (1, 23)
INSERT INTO #A_ (ConsumerID, LowestAnnualCost) VALUES (2, 19)
INSERT INTO #A_ (ConsumerID, LowestAnnualCost) VALUES (3, 10)
INSERT INTO #A_ (ConsumerID, LowestAnnualCost) VALUES (4, 54)
INSERT INTO #A_ (ConsumerID, LowestAnnualCost) VALUES (5, 25)

INSERT INTO #B_ (ConsumerID, LowestAnnualCost) VALUES (1, 23)
INSERT INTO #B_ (ConsumerID, LowestAnnualCost) VALUES (2, 19)
INSERT INTO #B_ (ConsumerID, LowestAnnualCost) VALUES (3, 98)
INSERT INTO #B_ (ConsumerID, LowestAnnualCost) VALUES (4, NULL)
INSERT INTO #B_ (ConsumerID, LowestAnnualCost) VALUES (5, 25)



Select 
  c.consumerID,
  CASE WHEN lacp.LowestAnnualCost  IS NOT NULL THEN  
      CASE WHEN lcp.LowestAnnualCost < lacp.LowestAnnualCost THEN 
          lcp.LowestAnnualCost 
       ELSE
           lacp.LowestAnnualCost  
       END
    ELSE
        lcp.LowestAnnualCost 
     END
   AS LowestAnnualCost
From #Consumer c  
left join #A_ lcp on c.ConsumerID = lcp.ConsumerID
left join #B_ lacp on lcp.ConsumerID = lacp.ConsumerID

--DROP TABLE #Consumer
--DROP TABLE #A_
--DROP TABLE #B_

现在,你永远不会说出你想要的结果到底是什么,但这是产生的输出......

consumerID  LowestAnnualCost
----------------------------
1           23
2           19
3           10
4           54
5           25

答案 1 :(得分:0)

除非我错过了什么,否则我不确定你的代码是否完全代表了你想要做的事情。 我尝试制作一个sqlfiddle,但我不能让它在那里运行(构建工作正常):http://sqlfiddle.com/#!3/a3619/1

无论如何,当我运行时:

CREATE TABLE A (ConsumerID int PRIMARY KEY, LowestAnnualCost int)
CREATE TABLE B (ConsumerID int PRIMARY KEY, LowestAnnualCost int)
CREATE TABLE C (ConsumerID int PRIMARY KEY)

INSERT C (ConsumerID) VALUES (1)
INSERT C (ConsumerID) VALUES (2)
INSERT C (ConsumerID) VALUES (3)
INSERT C (ConsumerID) VALUES (4)
INSERT C (ConsumerID) VALUES (5)

INSERT A (ConsumerID, LowestAnnualCost) VALUES (1, 23)
INSERT A (ConsumerID, LowestAnnualCost) VALUES (2, 19)
INSERT A (ConsumerID, LowestAnnualCost) VALUES (3, 10)
INSERT A (ConsumerID, LowestAnnualCost) VALUES (4, 54)
INSERT A (ConsumerID, LowestAnnualCost) VALUES (5, 25)

INSERT B (ConsumerID, LowestAnnualCost) VALUES (1, 23)
INSERT B (ConsumerID, LowestAnnualCost) VALUES (2, 19)
INSERT B (ConsumerID, LowestAnnualCost) VALUES (3, 98)
INSERT B (ConsumerID, LowestAnnualCost) VALUES (4, NULL)
INSERT B (ConsumerID, LowestAnnualCost) VALUES (5, 25)

GO

Select c.ConsumerID,
   CASE 
          WHEN lacp.LowestAnnualCost  IS NOT NULL THEN  
                 CASE 
                       WHEN lcp.LowestAnnualCost < lacp.LowestAnnualCost THEN 
                              lcp.LowestAnnualCost 
                       ELSE lacp.LowestAnnualCost  
                 END
          ELSE
                 lcp.LowestAnnualCost 
   END
   AS LowestAnnualCost
From C c  
left join A lcp on c.ConsumerID = lcp.ConsumerID
left join B lacp on lcp.ConsumerID = lacp.ConsumerID

它让我回答:

ConsumerID  LowestAnnualCost
1   23
2   19
3   10
4   54
5   25