Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在多种条件下计数记录_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 在多种条件下计数记录

Sql 在多种条件下计数记录,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我的查询是统计表中发生的事务数。以下是我的疑问: select tt.transaction_key, tt.description, t.mode, count(t.TransactionType) as Frequency from transaction_names tt left join transaction_report_data t on tt.transaction_key = t.TransactionType and t.created

我的查询是统计表中发生的事务数。以下是我的疑问:

select tt.transaction_key, tt.description, t.mode, count(t.TransactionType) as Frequency
from transaction_names tt left join
     transaction_report_data t
     on tt.transaction_key = t.TransactionType and
        t.created >='2017-04-11' and t.created <= '2018-04-13'
group by tt.transaction_key, tt.description, t.mode;
选择tt.transaction\u键、tt.description、t.mode、count(t.TransactionType)作为频率
从事务_名称tt左连接
交易报告数据
在tt.transaction_key=t.TransactionType和
t、 创建>='2017-04-11'和t.created试试这个

SELECT
    tt.transaction_key,
    tt.[Description],
    t.mode,
    COUNT(t.TransactionType) AS Frequency,
    Success = SUM(CASE WHEN T.[Status]='Success' THEN 1 ELSE 0 END)
    FROM transaction_names tt
       LEFT JOIN transaction_report_data t 
          ON tt.transaction_key = t.TransactionType
             AND t.created >= '2017-04-11'
             AND t.created <= '2018-04-13'
       GROUP BY 
          tt.transaction_key,
          tt.[Description],
          t.mode;
选择
tt.transaction_键,
tt.[说明],
t、 模式,
计数(t.TransactionType)作为频率,
Success=SUM(当T.[Status]=“Success”时,则为1,否则为0结束)
从事务_name tt
左连接事务\u报告\u数据t
在tt.transaction_key=t.TransactionType上
和t.created>=“2017-04-11”

和t.created你可以用下面这样的集合做一个例子

select 
tt.transaction_key, 
tt.description, 
t.mode, 
count(t.TransactionType) as Frequency,
Sum(case when t.status='success' then 1 else 0 end) as SuccessfulTransactions
from transaction_names tt left join
     transaction_report_data t
     on tt.transaction_key = t.TransactionType and
        t.created >='2017-04-11' and t.created <= '2018-04-13'
group by tt.transaction_key, tt.description, t.mode;
选择
tt.transaction_键,
tt.description,
t、 模式,
计数(t.TransactionType)作为频率,
将(t.status='success'然后1或0结束时的情况)合计为SuccessfulTransactions
从事务_名称tt左连接
交易报告数据
在tt.transaction_key=t.TransactionType和
t、 创建>='2017-04-11'和t.created
选择tt.transaction\u键、tt.description、t.mode、count(t.TransactionType)作为频率
从事务_名称tt左连接
交易报告数据
在tt.transaction_key=t.TransactionType和

t、 创建>='2017-04-11'和t。创建欢迎使用StackOverflow!我已经编辑了您的答案,以便为代码提供更好的格式。将来,您可以使用四个空格缩进代码以创建代码块。此外,如果您能对您的答案提供一些解释,那将是非常好的,因为只有代码的答案通常会被删除/否决。谢谢您的建议!!
select tt.transaction_key, tt.description, t.mode, count(t.TransactionType) as Frequency
from transaction_names tt left join
     transaction_report_data t
     on tt.transaction_key = t.TransactionType and
        t.created >='2017-04-11' and t.created <= '2018-04-13'
group by tt.transaction_key, tt.description, t.mode, t.status
having t.status = 'success'