简体   繁体   English

如果满足条件,则SQL查询返回1列,否则返回0列

[英]SQL query returning a column of 1 if a condition is met and 0 otherwise

I've created a nice query to watch all the unique IPs entering my site. 我创建了一个不错的查询,以观察进入我站点的所有唯一IP。 The schema is as follows: 该架构如下:

Page_Statistic - Contains data on each page hit (one record per hit). Page_Statistic-包含每个页面命中的数据(每个命中一个记录)。

  • page_statistic_id. page_statistic_id。
  • ip - ip address of visitor. ip-访问者的IP地址。
  • tag - used to record special things, like "demo" if they tried the demo. 标记-用于录制特殊内容,例如“ demo”(如果尝试过演示的话)。
  • url - of the page they visited. url-他们访问过的页面。
  • requested_on - time of visit. request_on-访问时间。

Ip_Location_Info - Contains location data on each visitor (one record per ip). Ip_Location_Info-包含每个访问者的位置数据(每个ip一个记录)。

  • ip ip
  • country 国家
  • region 区域
  • city
  • zip 压缩

I want to get all the unique IP addresses, with locations, number of visits, time range, and whether they tried the demo on my website - and this last part is what I'm having trouble with. 我想获得所有唯一的IP地址,以及位置,访问次数,时间范围以及他们是否在我的网站上尝试了该演示-这是我遇到的麻烦。 I want a 1 if they've tried the demo, and 0 if they haven't. 如果他们尝试过演示,我想要1,如果没有尝试,我想要0。 An IP has tried the demo iff a tupple exists in Page_Statistic with tag set to 'demo'. IP已尝试演示,前提是在Page_Statistic中存在标签设置为“ demo”的tupple。

I've created the following query which solves this: 我创建了以下查询来解决此问题:

WITH uniqueips(frst, lst, ip, visits) AS
(
    SELECT MIN(requested_on), MAX(requested_on), ip, count(*) AS visits
    FROM Page_Statistic p
    GROUP BY ip
),
tried_demo(ip, tried_demo) AS
(
    SELECT DISTINCT ip.ip, ISNULL(p.page_statistic_id - p.page_statistic_id + 1, 0) 
    FROM uniqueips ip
    LEFT JOIN Page_Statistic p ON p.ip = ip.ip AND p.tag = 'demo'
)
SELECT 
i.*, ip.frst AS first_appeared_on, 
    ip.lst AS last_appeared_on, 
    ip.visits,
    d.tried_demo
FROM Ip_Location_Info i
JOIN uniqueips ip ON ip.ip = i.ip
JOIN tried_demo d ON ip.ip = d.ip
ORDER BY ip.frst

But the ugly part is where I set the 1 or 0 for tried_demo. 但是丑陋的地方是我为try_demo设置了1或0。

ISNULL(p.page_statistic_id - p.page_statistic_id + 1, 0)

This works because if p.page_statistic_id is null, adding 1 to null is null so isnull will return the second item in the list of params (0). 之所以有效,是因为如果p.page_statistic_id为null,则将1加到null上就等于null,因此isull将返回参数列表(0)中的第二项。 It's such a hack! 真是个骇客! What is the correct way to do this? 正确的方法是什么?

The way you have it works, so that's good, but if you wanted to change it, you could change it to 拥有它的方式很好用,但是如果要更改它,可以将其更改为

case when p.page_statistic_id is null then 0 else 1 end

OR 要么

change your tried_demo CTE to this 将您的try_demo CTE更改为此

tried_demo(ip, tried_demo) AS
(
    SELECT DISTINCT p.ip, cast(1 as bit) 
    FROM Page_Statistic 
    WHERE p.tag = 'demo'
)

then your select can be like this 那么您的选择可以像这样

SELECT 
i.*, ip.frst AS first_appeared_on, 
    ip.lst AS last_appeared_on, 
    ip.visits,
    coalesce(d.tried_demo,0) as tried_demo
FROM Ip_Location_Info i
JOIN uniqueips ip ON ip.ip = i.ip
LEFT JOIN tried_demo d ON ip.ip = d.ip
ORDER BY ip.frst

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM