我有两个表,我需要从
计算数据登录历史记录 和 登录尝试
一个是不包含Login Attemtps的登录历史记录表。
登录历史记录的主键是REF_ID,但也包含Customer_ID。 Login Attempts表还包括Customer_ID
我正在尝试获取特定Customer_id的登录尝试次数和登录历史记录。我已经获得了一个Customer_ID列表并将它们放在一个临时表“#a”中 这是我试图用来计算的查询。
SELECT COUNT (la.customer_ID) as login_attempts FROM
LOGIN_ATTEMPTS la
JOIN LOGIN_HISTORY lh
on la.Customer_ID = lh.customer_ID
where la.customer_ID in (select Customer_ID from #a)group by la.customer_ID
我需要的结果集是Customer_ID,以及每个客户的登录尝试次数和旁边的历史记录。我尝试了几种不同的方法,并且我总是以GROUP BY或COUNT语法出错。
我也假设我可以在这里添加一个SUM函数,但我不确定如何构建它。感谢您的帮助!
答案 0 :(得分:2)
Select customer_ID
,(Select Count(*) from LOGIN_ATTEMPTS l where l.customer_ID=a#.ID) as [LOGIN_ATTEMPTS]
,(Select Count(*) from LOGIN_HISTORY h where h.customer_ID=a#.ID) as [LOGIN_HISTORY]
from a#
并且不使用#
Select customer_ID
,(Select Count(*) from LOGIN_ATTEMPTS l where l.customer_ID=a2.ID) as [LOGIN_ATTEMPTS]
,(Select Count(*) from LOGIN_HISTORY h where h.customer_ID=a2.ID) as [LOGIN_HISTORY]
(
Select Distinct customer_ID from
(
select customer_ID from LOGIN_ATTEMPTS
union
select customer_ID from LOGIN_HISTORY
) a1
)a2
答案 1 :(得分:1)
SELECT
COUNT (*) AS Attempts,
la.Customer_ID
FROM
LOGIN_ATTEMPTS la
JOIN
LOGIN_HISTORY lh
on la.Customer_ID = lh.customer_ID
where
la.customer_ID in (select Customer_ID from #a)
group by
la.customer_ID