1

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

表 - 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 表中日期的最新条目时,这会带回每个查询的所有日记条目。我该怎么做呢?

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

谢谢

4

5 回答 5

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))
于 2013-10-28T10:39:03.350 回答
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
于 2013-10-28T10:45:15.277 回答
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
于 2013-10-28T10:41:00.707 回答
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

我还没有测试过,所以如果你发现任何问题,请告诉我。我在这里使用了分析查询,它比子查询工作得更快

于 2013-10-28T10:41:16.857 回答
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 服务器

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,您可以做类似的工作来完成这项工作。

于 2013-10-28T10:42:32.407 回答