简体   繁体   English

在SQL中选择多个变量

[英]Select multiple variables in SQL

So I have the following problem. 所以我有以下问题。

I have a receipts table with several columns, among others I have 2 columns 'receipt_id' and other 'status' . 我有一个包含几列的receipts table ,除其他外,我有2列'receipt_id'和其他'status'

'Status' can take the following values: 'active' and 'unpaid' . 'Status'可以采用以下值: 'active''unpaid'

I would like to run a query to have an output of the following ratio (lets call it 'RR') RR=active/(active+unpaid) 我想运行查询以得到以下比率的输出(lets call it 'RR') RR=active/(active+unpaid)

I've tried to make 'RR' as a variable and then input the arguments with their conditions using 'AS' function. 我试图将“ RR”作为变量,然后使用“ AS”功能输入参数及其条件。

Also I've tried to make the following: 我也尝试做以下事情:

select count(distinct r.user_id) from receipts as r where r.status='active' as active, count(distinct r.user_id) from receipts as r where r.status='unpaid' as unpaid 从r的收据中选择count(distinct r.user_id),其中r.status ='active'为活动,从r的收据中选择count(distinct r.user_id)为r,其中r.status ='unpaid'为未付款。

(in order to get two different columns and the make the ratio 'RR' with a simple formula). (为了得到两个不同的列,并使用一个简单的公式使比率“ RR”)。

None of this is working... 这些都不起作用...

Could someone please help me? 有人可以帮我吗? Thanks in advance 提前致谢

Just use conditional aggregation. 只需使用条件聚合。 I think this is the simplest method: 我认为这是最简单的方法:

select avg(r.status = 'active')
from receipts r
where r.status in ('active', 'unpaid');

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

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