简体   繁体   English

如何在Postgresql中加入这些表?

[英]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;
  • Your original query was invalid, because the subquery had no alias. 您的原始查询无效,因为子查询没有别名。
    Otherwise, the subquery is just fine, don't flatten it. 否则,子查询就可以了,不要弄平它。 Short syntax, percentage calculation and join to accounts only for qualifying rows, division by 0 avoided elegantly. 简短的语法,百分比计算和仅对符合条件的行加入accounts ,避免了用0除。
  • Why do you cast account_id to bigint ? 为什么将account_idbigint There's something off here. 这里有些事 Are you storing numbers as text? 您是否将数字存储为文本? I would need to see your table definitions ... 我将需要查看您的表定义...
  • Reformulated the calculation. 重新计算。 It's cheaper and more precise to multiply the integer by 100 before you cast to float . 在转换float 之前 ,将integer乘以100既便宜又精确。
    You may want to cast to numeric instead and wrap that into round(expression, 2) to get pretty output. 您可能需要转换为numeric然后将其包装到round(expression, 2)以获得漂亮的输出。
  • Using a shorter (equivalent) expression for the conditional count. 对条件计数使用较短(等效)的表达式。 Details here. 详细信息在这里。
  • Simplified your WHERE condition. 简化了您的WHERE条件。

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

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