如何在postgresql中加入这些表?

时间:2014-08-01 19:05:51

标签: sql postgresql

我有这样的查询:

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

有什么想法吗?

3 个答案:

答案 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)中以获得相当的输出。
  • 使用较短(等效)的表达式进行条件计数。 Details here.
  • 简化了您的WHERE条件。