从左外连接中选择最上面的一个

发布于 2024-07-30 20:46:39 字数 1672 浏览 2 评论 0原文

伙计们,我有一个查询,基本上选择我们的用户使用的最新浏览器。

这是我们的(简化的)表结构

HITS_TABLE
----------
USERID
BROWSER
HITSDATE

USER_TABLE
----------
USERID
USERNAME

,这是我查询用户使用的最新浏览器的方式

SELECT U.*, H.BROWSER

FROM USER_TABLE U

CROSS APPLY 
  (SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC
  )as H

HITS_TABLE 是几天前刚刚添加的。

因此,该查询只是在我们添加 HITS_TABLE 后访问我们网站的用户,并消除其他查询。

这是示例案例

USER_TABLE
-------------------
USERID     USERNAME
-------------------
1          'Spolski'
2          'Atwoord
3          'Dixon'


HITS_TABLE
------------------------------
USERID     HITSDATE     BROWSER
------------------------------
2          15/8/2009    'Firefox 3.5'
1          16/8/2009    'IE 6'
2          16/8/2009    'Chrome'

这是示例结果

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'

但是,我想使用“未知”浏览器添加其他用户。 这是我想要的结果,

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
3          'Dixon'      'Unknown'

我相信它可以通过 LEFT OUTER JOIN 来实现。 但我总是这样:(我不想要这个结果)

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
2          'Atwoord'    'Firefox 3.5'
3          'Dixon'      'Unknown'

我希望我的问题很清楚。

Guys, I have a query where basically select the latest browser that our user used.

here is our (simplified) table structure

HITS_TABLE
----------
USERID
BROWSER
HITSDATE

USER_TABLE
----------
USERID
USERNAME

and here is how I query the latest browser that our user used

SELECT U.*, H.BROWSER

FROM USER_TABLE U

CROSS APPLY 
  (SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC
  )as H

The HITS_TABLE is just added several days ago.

So, that query is just resulting users that visited our website after we added the HITS_TABLE, and eliminate the others.

Here is the sample case

USER_TABLE
-------------------
USERID     USERNAME
-------------------
1          'Spolski'
2          'Atwoord
3          'Dixon'


HITS_TABLE
------------------------------
USERID     HITSDATE     BROWSER
------------------------------
2          15/8/2009    'Firefox 3.5'
1          16/8/2009    'IE 6'
2          16/8/2009    'Chrome'

Here is the sample result

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'

But, I want to add other users with 'unknown' browser.
Here is my desired result

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
3          'Dixon'      'Unknown'

I believe it could be achieved by LEFT OUTER JOIN.
But I always had this: (I DO NOT want this result)

------------------------------
USERID     USERNAME     BROWSER
------------------------------
1          'Spolsky'    'IE 6'
2          'Atwoord'    'Chrome'
2          'Atwoord'    'Firefox 3.5'
3          'Dixon'      'Unknown'

I hope my question is clear.

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

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

发布评论

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

评论(3

小姐丶请自重 2024-08-06 20:46:39

你不能子选择吗,不漂亮但应该可以..

SELECT U.*,

ISNULL((SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC),'UnKnown') AS Browser

FROM USER_TABLE U

Couldn't you sub select, not pretty but should work ..

SELECT U.*,

ISNULL((SELECT TOP 1 BROWSER 
   FROM HITS_TABLE 
   WHERE HITS_TABLE.USERID = U.USERID
   ORDER BY HITS_TABLE.HITSDATE DESC),'UnKnown') AS Browser

FROM USER_TABLE U
醉南桥 2024-08-06 20:46:39

对 hit_table 使用 userid 上的 group by 可以让您获取每个用户 ID 的 max() 命中日期。 我在下面的代码中将其称为“最新热门”。

在 USER TABLE 上选择并左连接到 LATEST HITS 允许您提取每个用户的记录。

返回到点击表,然后您可以提取与该日期关联的浏览器记录,或者对于其中没有记录的用户,提取空记录。

select
   user_table.userid,
   user_table.username,
   isnull(hitstable.browser, 'unknown') as browser
from
  user_table
left join
(
  select
    userid,
    max(hitsdate) hitsdate
  from
    hits_table
  group by  
    userid
) latest_hits
on
  user_table.userid = latest_hits.userid    
left join
  hits_table
on hits.table.userid = latest_hits.userid
and hits_table.hitsdate = latest_hits.hitsdate

using a group by on userid against the hits_table allows you to get the max() hitsdate for each userid. I've called this LATEST HITS in the code below.

Selecting on the USER TABLE with a left join to LATEST HITS allows you to pull records for every user.

joining back onto the HITS TABLE then allwos you to pull the browser record associated with that date, or a null for users with no record in there.

select
   user_table.userid,
   user_table.username,
   isnull(hitstable.browser, 'unknown') as browser
from
  user_table
left join
(
  select
    userid,
    max(hitsdate) hitsdate
  from
    hits_table
  group by  
    userid
) latest_hits
on
  user_table.userid = latest_hits.userid    
left join
  hits_table
on hits.table.userid = latest_hits.userid
and hits_table.hitsdate = latest_hits.hitsdate
瀟灑尐姊 2024-08-06 20:46:39
SELECT U.*,'BROWSER' = 
    case 
     when (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC) is  null then 'Unknown'
else (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC)
    end
FROM USER_TABLE U
SELECT U.*,'BROWSER' = 
    case 
     when (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC) is  null then 'Unknown'
else (SELECT TOP 1 BROWSER FROM HITS_TABLE WHERE HITS_TABLE.USERID = U.USERID ORDER BY HITS_TABLE.HITSDATE DESC)
    end
FROM USER_TABLE U
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文