内部加入+选择最近的

时间:2014-08-12 23:20:42

标签: sql sql-server-ce

我一直试图做下面的查询,但老实说它让我发疯了。

我在MS SQL CE 4.0上有2个表

表1名称:项目

  • ID
  • ITEM_CODE
  • Logged_by
  • 描述
ID | Item_Code | Logged_by | Description

1  |    A      |   Pete    |    just an A
2  |    B      |   Mary    |    Seams like a B
3  |    C      |   Joe     |    Obviously this is a C
4  |    D      |   Pete    |    This is another A

表2名称:Item_Comments

  • ID
  • ITEM_CODE
  • 注释
  • 日期
ID | Item_Code | Comment         |   Date

1  |    B      |    Done         |   2014/08/08
2  |    A      |    Nice A       |   2014/08/08
3  |    B      |    Send 1 More  |   2014/08/09
4  |    C      |    Done         |   2014/08/10
5  |    D      |    This is an A |   2014/08/10
6  |    D      |    Opps Sorry   |   2014/08/11

想要的结果:我希望将 Item_Comments 中的最新评论加入项目

ID | Item_Code | Logged_by | Description               | Comment

1  |     A    |     Pete   |    just an A              |  Nice A
2  |     B    |     Mary   |    Seams like a B         |  Send 1 More    
3  |     C    |     Joe    |    Obviously this is a C  |  Done           
4  |     D    |     Pete   |    This is another A      |  Opps Sorry     

我做了这个查询,但我得到了所有信息=(混合。

SELECT * 
FROM Items t1 
JOIN
     (SELECT Item_Code, Comment, MAX(date) as MyDate
      FROM Item_Comments
      Group By Item_Code, Comment, Date           
     ) t2
  ON Item_Code= Item_Code
ORDER BY t1.Item_Code;      

你知道怎么办?

3 个答案:

答案 0 :(得分:0)

请注意,如果您运行此内部文件,则会获得每条记录:

  SELECT Item_Code, Comment, MAX(date) as MyDate
  FROM Item_Comments
  Group By Item_Code, Comment, Date    

您只想要最近的评论。假设这是SQL Server 2008或更早版本,这就是每个Item_Code的最新日期:

  SELECT Item_Code, MAX(date) as MyDate
  FROM Item_Comments
  Group By Item_Code

现在你需要加入那个并查看该日期的评论:

SELECT C.* 
FROM Item_Comments C 
INNER JOIN 
 (SELECT Item_Code, MAX(date) as MyDate
  FROM Item_Comments
  Group By Item_Code 
 ) t2
 ON  C.Item_Code= t2.Item_Code
 AND C.date = t2.MyDate

现在您可以使用它连接回原始表:

SELECT t1.*, LatestComment.*
FROM Items t1 
INNER JOIN
(
SELECT C.* 
FROM Item_Comments C 
INNER JOIN 
 (SELECT Item_Code, MAX(date) as MyDate
  FROM Item_Comments
  Group By Item_Code 
 ) t2
 ON  C.Item_Code= t2.Item_Code
 AND C.date = t2.MyDate
 ) LatestComment
 On LatestComment.Item_Code = t1.Item_Code

根据您使用的实际数据库,这可以变得更加简单。这就是为什么你需要标记你的数据库和版本。

答案 1 :(得分:0)

尝试:

select x.*, z.comment
  from items x
  join (select item_code, max(date) as latest_dt
          from item_comments
         group by item_code) y
    on x.item_code = y.item_code
  join item_comments z
    on y.item_code = z.item_code
   and y.latest_dt = z.date

小提琴测试: http://sqlfiddle.com/#!6/d387f/8/0

你的查询很接近,但在你的内联视图中别名为t2,你按注释进行分组,让max函数实际上根本没有聚合。在t2中,您应该只选择item_code和max(日期)并仅按item_code分组,然后您可以使用它连接到item_comments(我上面的查询中的y和z)。

这是使用子查询执行此操作的第二种方法,但我会坚持上述(带内联视图的连接):

select i.*, c.comment
  from items i
  join item_comments c
    on i.item_code = c.item_code
 where c.date = (select max(x.date)
                   from item_comments x
                  where x.item_code = c.item_code)
 order by i.id

小提琴测试: http://sqlfiddle.com/#!6/d387f/11/0

答案 2 :(得分:0)

试试这个,

create table items (id int, item_code char(1), logged_by varchar(10), description varchar(30));

insert into items values (1, 'A', 'Pete', 'just an A');
insert into items values (2, 'B', 'Mary', 'Seams like a B');
insert into items values (3, 'C', 'Joe', 'Obviously this is a C');
insert into items values (4, 'D', 'Pete', 'This is another A');


create table item_comments (id int, item_code char(1), comment varchar(20), date date);

insert into item_comments values (1, 'B', 'Done', '2014/08/08');
insert into item_comments values (2, 'A', 'Nice A', '2014/08/08');
insert into item_comments values (3, 'B', 'Send 1 More', '2014/08/09');
insert into item_comments values (4, 'C', 'Done', '2014/08/10');
insert into item_comments values (5, 'D', 'This is an A', '2014/08/10');
insert into item_comments values (6, 'D', 'Opps Sorry', '2014/08/11');

select * from items;
select * from item_comments;

select * from (select i.logged_by,i.id,i.item_code,i.description,ic.comment
,row_number() over(partition by i.id order by i.id )as Rnk
from items i inner join item_comments ic
on i.item_code=ic.item_code and i.id in(1,3)) x
where x.Rnk=1
union
select * from (select i.logged_by,i.id,i.item_code,i.description,ic.comment
,row_number() over(partition by i.id order by i.id )as Rnk
from items i inner join item_comments ic
on i.item_code=ic.item_code and i.id in(2,4)
) x where x.Rnk=2 order by item_code