似乎我创建的查询有点乱。 还有其他方法可以让它更具可读性吗?
这是我创建的查询。
Select nullif((select count(*) from [casino_game].[dbo].[group_user] gu
where gu.group_id = (select pt.group_id from [casino_game].[dbo].[promo_trigger] pt
join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id
where pt.name = f.promo_name
group by pt.group_id)),0) as target_group, -- Number of customer uploaded to a free game program.
(cast(count(*) as decimal(11,2)) / nullif((select count(*) from [casino_game].[dbo].[group_user] gu
where gu.group_id = (select pt.group_id from [casino_game].[dbo].[promo_trigger] pt
join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id
where pt.name = f.promo_name
group by pt.group_id)), 0)) * 100 as claim_rate from [data].[dbo].[testgame]
这个查询正在运行,但是我需要一些建议,如果有另一种方法可以使这个工作更具可读性。
答案 0 :(得分:2)
您可以使用In [628]: %timeit df.loc[np.in1d(df['id'], ['0','1']),'id'].map({'0': False, '1': True})
100 loops, best of 3: 2.19 ms per loop
来避免使用相同的OUTER APPLY
两次
correlated sub-query
答案 1 :(得分:1)
我认为目标表是group_user
而不是testgame
,它必须是一行,不需要连接到testgame
的多行
select
COUNT(*) as target_group, -- Number of customer uploaded to a free game program.
-- It may cause the Divide by zero error if count(*) = 0
CASE WHEN COUNT(*) > 0 THEN (cast((SELECT COUNT(*) as value FROM [data].[dbo].[testgame]) as decimal(11,2)) / COUNT(*)) * 100 END as claim_rate
from
[casino_game].[dbo].[group_user] gu
inner join [casino_game].[dbo].[promo_trigger] pt on gu.group_id = pt.group_id
inner join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id