SQL选择ID最大的记录

发布于 2024-12-06 18:04:56 字数 1000 浏览 0 评论 0原文

我遇到了一些 SQL 问题,但无法找到解决方案。

现在我运行的查询基本上是:

SELECT Q.ID, Q.STATUS, C.LASTNAME, C.FIRSTNAME, 
       C.POSTAL, C.PHONE 
FROM QUEUE Q 
LEFT OUTER JOIN CUSTOMER C ON Q.ID = C.APPID 
WHERE C.LASTNAME LIKE 'SMITH%'

我从这个查询中得到了大约 200 条记录。我的问题是同一个人多次出现。

Q.ID  Q.STATUS   C.LASTNAME   C.FIRSTNAME   ETC...
 1        A         SMITH        JOHN
 2        A         SMITH        RYAN
 3        B         SMITH        BRIAN
 100      A         SMITH        RYAN
 200      A         SMITH        RYAN

我需要返回的是

Q.ID   Q.STATUS   C.LASTNAME   C.FIRSTNAME   ETC...
 1        A          SMITH         JOHN
 3        B          SMITH         BRIAN
 200      A          SMITH         RYAN

任何人都可以指出我正确的方向吗?我尝试过

SELECT WHATEVER FROM TABLE WHERE Q.ID IN (SELECT MAX(ID) FROM TABLE WHERE BLAH BLAH) 

专门搜索“RYAN SMITH”时有效的方法。但我需要显示 ID 最高的 SMITH 的所有结果。

任何帮助表示赞赏。

干杯

I have a issue with some SQL that I can't wrap my head around a solution.

Right now the query I am running basically is:

SELECT Q.ID, Q.STATUS, C.LASTNAME, C.FIRSTNAME, 
       C.POSTAL, C.PHONE 
FROM QUEUE Q 
LEFT OUTER JOIN CUSTOMER C ON Q.ID = C.APPID 
WHERE C.LASTNAME LIKE 'SMITH%'

I have about 200 records from this query. My issue is the same person has multiple occurances.

Q.ID  Q.STATUS   C.LASTNAME   C.FIRSTNAME   ETC...
 1        A         SMITH        JOHN
 2        A         SMITH        RYAN
 3        B         SMITH        BRIAN
 100      A         SMITH        RYAN
 200      A         SMITH        RYAN

What I need returned instead is

Q.ID   Q.STATUS   C.LASTNAME   C.FIRSTNAME   ETC...
 1        A          SMITH         JOHN
 3        B          SMITH         BRIAN
 200      A          SMITH         RYAN

Can anyone point me in the right direction please. I have tried

SELECT WHATEVER FROM TABLE WHERE Q.ID IN (SELECT MAX(ID) FROM TABLE WHERE BLAH BLAH) 

which worked when searching for "RYAN SMITH" specifically. But I need to show all results for SMITH with the highest IDs.

Any help is appreciated.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑾夏年华 2024-12-13 18:04:56

我想你可以做如下的事情

SELECT WHATEVER FROM TABLE 
WHERE Q.ID IN 
    (SELECT MAX(ID) FROM TABLE 
     WHERE BLAH...BLAH 
     GROUP BY C.FIRSTNAME, C.LASTNAME)

希望这有帮助!

I guess you could do something like below

SELECT WHATEVER FROM TABLE 
WHERE Q.ID IN 
    (SELECT MAX(ID) FROM TABLE 
     WHERE BLAH...BLAH 
     GROUP BY C.FIRSTNAME, C.LASTNAME)

Hope this helps!!

妄断弥空 2024-12-13 18:04:56

你尝试过类似的事情吗?好吧,这并不是一个真正的 SQL 语句,只是展示了这个想法

select * from Table where id in (
         select max(q.id) from Table group by c.lastname, c,firstname
)

have you tried something like that? well it is not really a SQL statement, just showing the idea

select * from Table where id in (
         select max(q.id) from Table group by c.lastname, c,firstname
)
野の 2024-12-13 18:04:56

如果我理解得很好,这应该可行:

SELECT Q.ID
     , Q.STATUS
     , C.LASTNAME
     , C.FIRSTNAME, 
     , C.POSTAL
     , C.PHONE
  FROM QUEUE Q 
  join CUSTOMER C ON Q.ID = C.APPID 
 WHERE C.LASTNAME like 'SMITH%'
   and not exists (SELECT *
                     FROM CUSTOMER innerCustomer
                    WHERE innerCustomer.LASTNAME like 'SMITH%'
                      and innerCustomer.APPID > C.APPID
                  )

注意:我已将“左连接”更改为“内连接”,因为您正在按 C.LASTNAME 进行过滤。所以我认为左连接没有多大意义。

If I understand well, this should work:

SELECT Q.ID
     , Q.STATUS
     , C.LASTNAME
     , C.FIRSTNAME, 
     , C.POSTAL
     , C.PHONE
  FROM QUEUE Q 
  join CUSTOMER C ON Q.ID = C.APPID 
 WHERE C.LASTNAME like 'SMITH%'
   and not exists (SELECT *
                     FROM CUSTOMER innerCustomer
                    WHERE innerCustomer.LASTNAME like 'SMITH%'
                      and innerCustomer.APPID > C.APPID
                  )

Note: I have change "left join" by "inner join" because you are filtering per C.LASTNAME. So I think left join doesn't have many sense.

满栀 2024-12-13 18:04:56

假设有一个 CUSTOMER.ID,并且我认为我是对的,如下所示:

SELECT Q.ID, Q.STATUS, M.LASTNAME, M.FIRSTNAME, 
       M.POSTAL, M.PHONE 
FROM QUEUE Q 
LEFT OUTER JOIN (
  SELECT C2.ID CID, 
         MAX(C2.FIRSTNAME) FIRSTNAME, 
         MAX(C2.LASTNAME) LASTNAME, 
         MAX(C2.POSTAL) POSTAL,
         MAX(C2.PHONE) PHONE,
         MAX(Q2.ID) QID
  FROM QUEUE Q2 
  LEFT OUTER JOIN CUSTOMER C2 ON Q2.ID = C.APPID 
  WHERE C2.LASTNAME LIKE 'SMITH%'
  GROUP BY C2.ID
) M ON (M.QID = Q.ID)

Assuming there is a CUSTOMER.ID, and I think I'm right, here it goes:

SELECT Q.ID, Q.STATUS, M.LASTNAME, M.FIRSTNAME, 
       M.POSTAL, M.PHONE 
FROM QUEUE Q 
LEFT OUTER JOIN (
  SELECT C2.ID CID, 
         MAX(C2.FIRSTNAME) FIRSTNAME, 
         MAX(C2.LASTNAME) LASTNAME, 
         MAX(C2.POSTAL) POSTAL,
         MAX(C2.PHONE) PHONE,
         MAX(Q2.ID) QID
  FROM QUEUE Q2 
  LEFT OUTER JOIN CUSTOMER C2 ON Q2.ID = C.APPID 
  WHERE C2.LASTNAME LIKE 'SMITH%'
  GROUP BY C2.ID
) M ON (M.QID = Q.ID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文