[英]How can I join these tables in postgresql?
I have a query like this: 我有这样的查询:
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
That gives me a table with account_ids, and the ie_usage_count, total_usage, and percent_ie associated with each account ID 这给了我一张带有account_ids的表,以及与每个帐户ID关联的ie_usage_count,total_usage和percent_ie
account_id | ie_usage_count | total_usage | percent_ie
I have another query 我还有另一个查询
select name, account_id
from accounts
That gives me the name of the person associated with each account. 这给了我与每个帐户关联的人的名字。
name | account_id |
I'd like to have a single query that includes name, account_id, ie_usage_count, total_usage, and percent_ie. 我想要一个包含名称,account_id,ie_usage_count,total_usage和percent_ie的查询。
name | account_id | ie_usage_count | total_usage | percent_ie
Any ideas? 有任何想法吗?
Your first query is more easily written as: 您的第一个查询更容易写为:
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;
You can get the name just by joining it in: 您只需加入即可获得名称:
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;
UNTESTED but perhaps something simple like a join, add on account_ID and then addition of a group by... but this makes several assumptions .. like acc_Logs has all accounts, or you only want percent_IE when there is a log entry... if no acc_Log or no account then no record... 未测试,但可能很简单,例如联接,添加account_ID,然后添加group by ...但这有几个假设..例如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
added alias to tables. 向表添加了别名。
Just join to it: 只需加入:
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
only for qualifying rows, division by 0 avoided elegantly. accounts
,避免了用0除。 account_id
to bigint
? account_id
为bigint
? There's something off here. integer
by 100 before you cast to float
. float
之前 ,将integer
乘以100既便宜又精确。 numeric
instead and wrap that into round(expression, 2)
to get pretty output. numeric
然后将其包装到round(expression, 2)
以获得漂亮的输出。 WHERE
condition. WHERE
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.