我有这样的查询:
select *, (CAST (ie_usage_count as float)/total_count)*100 as percent_ie from(
SELECT DISTINCT CAST (account_id AS bigint),
count(case when
user_agent LIKE '%MSIE 7%'
AND user_agent NOT LIKE '%Trident%'
then 1 end) as ie_usage_count,
count(*) as total_usage
FROM acc_logs
WHERE account_id NOT LIKE 'Account ID '
group by account_id
ORDER BY account_id )
where not ie_usage_count = 0
这为我提供了一个包含account_ids的表格,以及与每个帐户ID相关联的ie_usage_count,total_usage和percent_ie
account_id | ie_usage_count | total_usage | percent_ie
我有另一个查询
select name, account_id
from accounts
这给了我与每个帐户相关联的人的姓名。
name | account_id |
我想要一个包含name,account_id,ie_usage_count,total_usage和percent_ie的查询。
name | account_id | ie_usage_count | total_usage | percent_ie
有什么想法吗?
答案 0 :(得分:3)
您的第一个查询更容易编写为:
select CAST(account_id AS bigint),
SUM(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 1 else 0
end) as ie_usage_count,
count(*) as total_usage,
AVG(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 100.0 else 0
end) as percent_ie
from acc_logs
where account_id NOT LIKE 'Account ID '
group by account_id
having SUM(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 1 else 0
end) <> 0;
您可以通过以下方式获取名称:
select CAST(al.account_id AS bigint), a.name,
SUM(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 1 else 0
end) as ie_usage_count,
count(*) as total_usage,
AVG(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 100.0 else 0
end) as percent_ie
from acc_logs al join
accounts a
on al.account_id = a.account_id
where al.account_id NOT LIKE 'Account ID '
group by al.account_id, a.name
having SUM(case when user_agent LIKE '%MSIE 7%' AND user_agent NOT LIKE '%Trident%'
then 1 else 0
end) <> 0;
答案 1 :(得分:0)
UNTESTED但也许像连接一样简单,添加了account_ID然后添加了一个组...但是这会产生一些假设..比如acc_Logs有所有帐户,或者你只想在有日志条目时想要percent_IE。 ..如果没有acc_Log或没有帐户则没有记录...
select *, (CAST (ie_usage_count as float)/total_count)*100 as percent_ie from(
SELECT DISTINCT CAST (B.account_id AS bigint),
count(case when
user_agent LIKE '%MSIE 7%'
AND user_agent NOT LIKE '%Trident%'
then 1 end) as ie_usage_count,
count(*) as total_usage,
A.name
FROM acc_logs B
INNER JOIN Accounts A
on A.Account_ID = B.account_ID
WHERE B.account_id NOT LIKE 'Account ID '
group by B.account_id, A.Name
ORDER BY B.account_id )
where not ie_usage_count = 0
为表添加了别名。
答案 2 :(得分:0)
加入吧:
SELECT a.name, l.*, (l.ie_usage_count * 100)::float / l.total_count AS percent_ie
FROM (
SELECT account_id::bigint -- Why cast to bigint?
, count(user_agent LIKE '%MSIE 7%'
AND user_agent NOT LIKE '%Trident%'
OR NULL) AS ie_usage_count
, count(*) AS total_usage
FROM acc_logs
WHERE account_id NOT LIKE 'Account ID ' -- trailing blank? typo?
GROUP BY account_id
ORDER BY account_id
) l
JOIN accounts a USING (account_id)
WHERE ie_usage_count <> 0;
accounts
仅限于符合条件的行,除以0优雅地避免。account_id
投射到bigint
?这里有一些东西。您是否将数字存储为文本?我需要看看你的表格定义...... integer
之前,将float
乘以100 会更便宜,更准确。numeric
并将其包装到round(expression, 2)
中以获得相当的输出。WHERE
条件。