简体   繁体   English

SQL始终为每个ID从LUT返回所有行

[英]SQL Always return all rows from LUT for each ID

I am wondering if this query can be modified to achieve what I want: 我想知道是否可以修改此查询以实现我想要的功能:

SELECT 
cv.[ID]
,cv.[CustomValue]
,cf.[SpecialInformationId]
FROM #CustomFields cf
FULL OUTER JOIN #CustomValues cv ON cf.SpecialInformationId = cv.id

This returns all cv.Id's. 这将返回所有cv.Id。 It also returns any unmatched cf.SpecialInformationId's with a NULL for the cv information. 它还会返回任何不匹配的cf.SpecialInformationId,其中简历信息为NULL。 What I actually want is for each instance of cv, I want every cf to show. 我真正想要的是cv的每个实例,我希望每个cf都能显示。 cf is a lookup table. cf是一个查找表。 In this instance there are 12 values, but that varies everytime the query runs. 在这种情况下,有12个值,但是每次查询运行时都会变化。 Here is an example: 这是一个例子:

What this query currently returns: 该查询当前返回的内容:

cv.id   cv.customvalue   cf.specialinformationid
1           003            1
1           abc            2
2           004            1
2           1/1/2010       4 
2           abc            2
3           009            1
4           003            1
4           acb            2
4           1/2/2010       4

What I want it to return: 我想要它返回什么:

cv.id   cv.customvalue   cf.specialinformationid
1          003               1
1          abc               2
1          NULL              3
1          NULL              4
1          NULL              5
2          004               1
2          abc               2
2          NULL              3
2          1/1/2010          4
2          NULL              5
3          009               1
3          NULL              2
3          NULL              3
3          NULL              4
3          NULL              5
4          003               1
4          acb               2
4          NULL              3
4          1/2/2010          4
4          NULL              5

A Left join cannot be used because there are only 12 rows in the lookup table so if a left join is used the same result will be achieved as the full outer join. 不能使用左联接,因为查找表中只有12行,因此,如果使用左联接,则将获得与完整外部联接相同的结果。

This is a spinoff of my other question: SQL 2 tables always return all rows from 1 table match existing on other 这是我其他问题的衍生产品: SQL 2表总是返回1个表中所有存在于其他表中的行的所有行

Thanks 谢谢

我相信CROSS JOIN可以达到您想要的结果。

The problems are arising because your Table2 is not really a 'vehicle' table. 由于您的Table2并不是真正的“车辆”表,因此出现了问题。 Because the VehicleId does not uniquely identify a record in that table. 因为VehicleId不会唯一地标识该表中的记录。 This is where all of the confusion is coming from. 这就是所有困惑的来源。 So to solve that and get your problem to work I did a select distinct on table2 against the values in table 1 (I also did a select distinct for clarity, but it was not necessary.) Hope this helps. 因此,为了解决该问题并使您的问题发挥作用,我在table2上针对表1中的值进行了选择(我为清楚起见也进行了select的选择,但这不是必需的。)希望这会有所帮助。

CREATE TABLE #Table1 (Id INT)
CREATE TABLE #Table2 (VehicleID INT, Value VARCHAR(50), Table1ID INT)

INSERT INTO #Table1 VALUES (1),(2),(3),(4),(5)
INSERT INTO #Table2 VALUES (1, 't', 1),(1, 'q', 2),(3, 'w', 3),(3, 'e', 4),(4, 't', 1),(5, 'e', 1),(5, 'f', 2),(5, 'g', 4)

SELECT * FROM #Table1
SELECT * FROM #Table2

SELECT  t2.VehicleID, t2.Value
FROM    ( SELECT    t2.VehicleId, t1.Id
          FROM      ( SELECT DISTINCT
                                VehicleId
                      FROM      #Table2 ) t2
                    CROSS JOIN ( SELECT Id
                                 FROM   #Table1 ) t1 ) Base
        LEFT JOIN #Table2 t2
            ON Base.VehicleId = t2.VehicleID
               AND Base.Id = t2.Table1ID
WHERE (Base.VehicleId BETWEEN 1 AND 3)

DROP TABLE #Table1
DROP TABLE #Table2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM