为每个查询选择最新更新

时间:2013-10-28 10:30:14

标签: sql

我有两个表,需要创建一个查询,使用最新的日记更新带回所有当前打开的查询

表 - sv_inquiry

inquiry_id | status  | state       | owner_grp |
------------------------------------------------
1            OPEN     Acknowledged    DEV
2            OPEN     Acknowledged    DEV
3            OPEN     Reassigned      DEV
4            OPEN     Reassigned      DEV

表 - sv_diary

diary_id| inquiry_id | change_dt| change_uid |
------------------------------------------------
1          1           2/1/2013    JBLOGGS
2          1           3/1/2013    JBLOGGS
3          1           4/1/2013    JSMITH
1          2           2/1/2013    JBLOGGS
2          2           4/1/2013    JSMITH
1          3           4/1/2013    JSMITH
2          3           6/1/2013    JBLOGGS
1          4           5/1/2013    JBLOGGS
2          4           8/1/2013    JBLOGGS

我目前使用的代码是

SELECT 
  t1.inquiry_id, 
  t1.state, 
  t1.status, 
  t1.business_impact, 
  t1.priority, 
  t1.owner_rep, 
  t1.owner_grp, 
  t2.change_dt as diary_updated, 
  t2.change_uid as diary_updated_by  
FROM sv_inquiry t1
JOIN sv_diary t2 
  on t1.inquiry_id = t2.inquiry_id
WHERE 
  owner_grp = 'DEV' 
  AND state <> 'Closed' 
  AND status is not null

但是当我只想要基于sv_diary表中日期的最新日记条目时,这会带回每个查询的所有日记条目。我该怎么做?

对表格的垃圾格式表示歉意,但我不知道如何在这个网站上创建一个像样的表结构。

由于

5 个答案:

答案 0 :(得分:0)

SELECT t1.inquiry_id, t1.state, t1.status, t1.business_impact, t1.priority, t1.owner_rep, t1.owner_grp, 
 t2.change_dt as diary_updated, t2.change_uid as diary_updated_by  
FROM sv_inquiry t1
JOIN sv_diary t2 on t1.inquiry_id = t2.inquiry_id
WHERE owner_grp = 'DEV' 
AND state <> 'Closed' 
AND status is not null
AND HAVING (t2.change_dt = (Select MAX(t2.change_dt) as Expr1
                                        FROM sv_diary t2 AS data_feed_file_1
                                        where t2.change_dt is not null))

答案 1 :(得分:0)

尝试分组:

SELECT tt.*,
        t2.change_dt,
        t2.change_uid   
FROM
(
    SELECT  t1.inquiry_id, 
            t1.state, 
            t1.status, 
            t1.business_impact, 
            t1.priority, 
            t1.owner_rep, 
            t1.owner_grp, 
            MAX(t2.diary_id) as diary_id
    FROM sv_inquiry t1
    JOIN sv_diary t2 on t1.inquiry_id = t2.inquiry_id
    WHERE owner_grp = 'DEV' 
    AND state <> 'Closed' 
    AND status is not null
    GROUP BY t1.inquiry_id, t1.state, t1.status, t1.business_impact, t1.priority, t1.owner_rep, t1.owner_grp, 
) tt
INNER JOIN sv_diary td on t1.inquiry_id = t2.inquiry_id

答案 2 :(得分:0)

您可以尝试以下查询: -

SELECT t1.inquiry_id, t1.state, t1.status, t1.business_impact, t1.priority, t1.owner_rep, t1.owner_grp, 
 t2.change_dt as diary_updated, t2.change_uid as diary_updated_by  
FROM sv_inquiry t1
JOIN (select * from (select d.*,row_number(over partition by inquiry_id order by dairy_updated desc) rn from sv_diary d) where rn =1) t2 on t1.inquiry_id = t2.inquiry_id
WHERE owner_grp = 'DEV' 
AND state <> 'Closed' 
AND status is not null

我没有测试相同,所以如果您发现任何问题,请告诉我。 我在这里使用过分析查询,它比子查询

更快

答案 3 :(得分:0)

您必须按日期列排序并将其限制为第一个元素:

MYSQL:

SELECT 
  t1.inquiry_id, 
  t1.state, 
  t1.status, 
  t1.business_impact, 
  t1.priority, 
  t1.owner_rep, 
  t1.owner_grp, 
  t2.change_dt as diary_updated, 
  t2.change_uid as diary_updated_by  
FROM sv_inquiry t1
JOIN sv_diary t2 
  on t1.inquiry_id = t2.inquiry_id
WHERE 
  owner_grp = 'DEV' 
  AND state <> 'Closed' 
  AND status is not null
ORDER BY t2.change_dt DESC
LIMIT 1;

SQL Server

SELECT TOP 1
  t1.inquiry_id, 
  t1.state, 
  t1.status, 
  t1.business_impact, 
  t1.priority, 
  t1.owner_rep, 
  t1.owner_grp, 
  t2.change_dt as diary_updated, 
  t2.change_uid as diary_updated_by  
FROM sv_inquiry t1
JOIN sv_diary t2 
  on t1.inquiry_id = t2.inquiry_id
WHERE 
  owner_grp = 'DEV' 
  AND state <> 'Closed' 
  AND status is not null
ORDER BY t2.change_dt DESC;

如果您使用的是不同的dbms,则可以执行类似的工作来完成此操作。

答案 4 :(得分:0)

试试这个:

SELECT t1.inquiry_id, t1.state, t1.status, t1.business_impact, t1.priority,
    t1.owner_rep, t1.owner_grp, t2.change_dt as diary_updated,
    t2.change_uid as diary_updated_by
FROM sv_inquiry t1
    LEFT JOIN (
        SELECT t1.*
        FROM sv_diary t1 JOIN(
            SELECT inquery_id, MAX(change_dt)
            FROM sv_diary
            GROUP BY inquery_id
        ) t2 ON t1.inquery_id = t2.inquery_id AND t1.change_dt = t2.change_dt 
    ) t2 on t1.inquiry_id = t2.inquiry_id
WHERE t1.owner_grp = 'DEV' 
    AND t1.state <> 'Closed' 
    AND t1.status is not null