Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server T-SQL选择与条件连接_Sql Server_Tsql_Join_Max - Fatal编程技术网

Sql server T-SQL选择与条件连接

Sql server T-SQL选择与条件连接,sql-server,tsql,join,max,Sql Server,Tsql,Join,Max,假设我有3张桌子: 汽车 身份证 CarColorHistory 身份证 龋齿 彩色 修改日期 颜色: 身份证 颜色名称 我想选择所有汽车及其颜色,但重要的是,汽车的颜色是CarColorHistory表中最后修改的颜色 我需要使用join来完成此操作 例如: 汽车: 汽车颜色历史记录: 1 1 1 26/03/2012 -> (actual color, can be take by date or id) 2 1 2 25/03/2012 3 2 2 25/03/2012

假设我有3张桌子:

  • 汽车

    • 身份证
  • CarColorHistory

    • 身份证
    • 龋齿
    • 彩色
    • 修改日期
  • 颜色

    • 身份证
    • 颜色名称
  • 我想选择所有汽车及其颜色,但重要的是,汽车的颜色是
    CarColorHistory
    表中最后修改的颜色

    我需要使用join来完成此操作

    例如:

    汽车:

    汽车颜色历史记录:

    1 1 1 26/03/2012  -> (actual color, can be take by date or id)
    2 1 2 25/03/2012
    3 2 2 25/03/2012
    
    颜色:

    1 Blue
    2 Red
    
    我需要得到结果:(汽车id,颜色名称)

    我试图通过加入Cars表和CarColorHistory表来实现,但我得到了所有颜色的汽车。我只需要实际的颜色(最后添加)


    请帮助

    这应该能帮到你:

    SELECT c.id, (
        SELECT co.ColorName FROM Color co
        WHERE co.id = (
            SELECT TOP 1 ColorID FROM CarColorHistory
            WHERE CarID = c.id
            ORDER BY ModificationDate DESC
        )
     ) AS ColorName
    
    试试这个:

    select c.id, colorname
    from cars c
    inner join CarColorHistory h on c.id = h.CarID
    inner join Color c2 on h.colorid = c2.id
    where h.ModificationDate = (select max(ModificationDate)
                                from CarColorHistory x where c.id = x.CarId)
    

    有几种方法可以得到结果。您可以使用子查询获取
    max(modificationdate)

    或者,由于您使用的是SQL Server,因此可以使用:


    请参见

    实现这一点的一种方法可能是只使用子查询,就像以前发布的一样,因为您使用的是t-sql,所以您还应该能够使用apply:

    SELECT
            Cars.Id, LatestColors.ColorID, LatestColors.ModificationDate
        FROM Cars
        CROSS APPLY (
            SELECT TOP 1
                    ColorID, ModificationDate
                FROM CarColorHistory
                WHERE CarID = Cars.ID
                ORDER BY ModificationDate DESC
        ) AS LatestColors
    

    如果您有
    Sql Server 2005
    或更高版本,则可以尝试此操作:

    您可以在此处尝试
    通用表表达式的工作原理:


    我不确定这是不是最好的方式,但这是我做这件事的方式。首先使用MAX从表中获取所需的值,然后将该结果作为表使用,以
    联接
    ,以消除不需要的值

    SELECT c.ID, Color.Color
    From Cars c JOIN CarColorHistory h on c.id = h.CarID
         JOIN Color on h.ColorID = Color.ID
         JOIN
    
        --Create a table with only the latest value and the car ID
        ( 
        SELECT c.ID, Max(h.TimeStamp) as time
        FROM Cars c JOIN CarColorHistory h on c.id = h.CarID
             JOIN Color on h.ColorID = Color.ID
        Group by c.ID --Join the table on latest time to get rid of old timestamps
        ) Max on Max.ID = c.ID and h.TimeStamp=max.time
    

    谢谢,但你能帮我加入声明吗?我怎样才能用你的浓咖啡?这对我的老师很重要。为什么您需要颜色历史记录的ID?谢谢,这正是我需要的:*
    select c.id, r.colorname
    from cars c
    inner join CarColorhistory h1
      on c.id = h1.carid
    inner join
    (
      select max(modificationdate) MaxDate,
        carid
      from CarColorhistory
      group by carid
    ) h2
      on h1.carid = h2.carid
      and h1.modificationdate = h2.maxdate
    inner join color r
      on h1.colorid = r.id
    
    select id, colorname
    from
    (
      select c.id, r.colorname,
        row_number() over(partition by c.id order by modificationdate desc) rn
      from cars c
      inner join CarColorhistory h1
        on c.id = h1.carid
      inner join color r
        on h1.colorid = r.id
    ) src
    where rn = 1;
    
    SELECT
            Cars.Id, LatestColors.ColorID, LatestColors.ModificationDate
        FROM Cars
        CROSS APPLY (
            SELECT TOP 1
                    ColorID, ModificationDate
                FROM CarColorHistory
                WHERE CarID = Cars.ID
                ORDER BY ModificationDate DESC
        ) AS LatestColors
    
      ;WITH CTE_Cars(CarID, MaxDate)
        AS
        (
        SELECT CarID, MAX(ModificataionDate) AS MaxDate
        FROM CarColorHistory
        GROUP BY CarID
        )
        SELECT CTE_Cars.CarID, Color.ColorName
        FROM 
        CTE_Cars
        INNER JOIN CarColorHistory ON CarColorHistory.CarID = CTE_Cars.CarID AND
        CarColorHistory.ModificataionDate = CTE_Cars.MaxDate
        INNER JOIN Color ON Color.id = CarColorHistory.ColorId
    
    SELECT c.ID, Color.Color
    From Cars c JOIN CarColorHistory h on c.id = h.CarID
         JOIN Color on h.ColorID = Color.ID
         JOIN
    
        --Create a table with only the latest value and the car ID
        ( 
        SELECT c.ID, Max(h.TimeStamp) as time
        FROM Cars c JOIN CarColorHistory h on c.id = h.CarID
             JOIN Color on h.ColorID = Color.ID
        Group by c.ID --Join the table on latest time to get rid of old timestamps
        ) Max on Max.ID = c.ID and h.TimeStamp=max.time