仅选择没有其他出现类型的行

Daw*_*jee 2 sql sql-server join left-join

我不确定如何解释这个问题,所以让我尝试一个简化的例子。

I have two tables Tickets and TicketEntities:

Tickets

tID | Customer |  Val
----|----------|------
1   | Paul     |  20
2   | Paul     |  10
3   | Peter    |  15
4   | Jane     |  100
5   | Doe      |  400
6   | John     |  5
Run Code Online (Sandbox Code Playgroud)

TicketEntities

EntityID |  TicketID |   Type
---------|-----------|--------
1        |   1       |   1     
2        |   1       |   2
3        |   3       |   1
4        |   4       |   1
5        |   5       |   2
6        |   6       |   2
Run Code Online (Sandbox Code Playgroud)

I want to create two views (preferably so if one view is possible):

  1. [Already solved] View to show me Tickets with TicketEntity type 1. My working solution: Desired Result:

    tID   |   Customer  | Val | EntityID |  Type
    
    Run Code Online (Sandbox Code Playgroud)

    -------|-------------|-----|----------|------- 1 | Paul | 20 | 1 | 1 3 | Peter | 15 | 3 | 1 4 | Jane | 100 | 4 | 1

    Note: Not actual query/results

    SQL

    SELECT *
    FROM Tickets AS t
    LEFT JOIN TicketEntities AS e ON t.ID=e.TicketID WHERE e.EntityTypeId = 1 
    
    Run Code Online (Sandbox Code Playgroud)
  2. View with only Tickets with TicketEntity type 2 but not 1

    Desired Result:

    tID   |   Customer  | Val | EntityID |  Type
    ------|-------------|-----|----------|-------
    5     |     Doe     | 400 |  5       |   2
    6     |     John    | 5   |  6       |   2   
    
    Run Code Online (Sandbox Code Playgroud)

    SQL

    SELECT *
      FROM Tickets AS t
      LEFT JOIN TicketEntities AS e ON t.ID=e.TicketID WHERE e.EntityTypeId = 1 
    AND 
    NOT EXISTS (SELECT * 
      FROM dbo.Tickets AS t2
        INNER JOIN dbo.TicketEntities AS e2 ON t2.Id=e2.Ticket_Id 
        WHERE e2.EntityTypeId = 2)
    
    Run Code Online (Sandbox Code Playgroud)
  3. OR more preferably, the single view:

    Desired Result:

    tID    |    Customer |  Val |  Entity_Type1 |  Entity_Type2
    -------|-------------|------|---------------|---------------
    1      |    Paul     |  20  |  1            |  2
    2      |    Paul     |  10  |               |
    3      |    Peter    |  15  |  1            |    
    4      |    Jane     |  100 |  1            |    
    5      |    Doe      |  400 |               |  2
    6      |    John     |  5   |               |  2  
    
    Run Code Online (Sandbox Code Playgroud)

    SQL

    I'm not sure how to go about it. Self join?

Finally, Improvements to the question are welcome. What's the best way to approach this problem?

Tim*_*sen 5

I think two left joins from the Tickets table to the TicketEntities table should work here:

SELECT
    t.tID,
    t.Customer,
    t.Val,
    te1.Type,
    te2.Type
FROM Tickets t
LEFT JOIN TicketEntities te1
    ON t.tID = te1.TicketID AND te1.Type = 1
LEFT JOIN TicketEntities te2
    ON t.tID = te2.TicketID AND te2.Type = 2;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Demo

Note carefully that we do not impose any WHERE restrictions on the two TicketEntities joins. This is to ensure that we don't filter off any ticket records prematurely.