sql/hive查询,为某个值计算每天的行数

lb3vh1jj  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(456)

我目前正在编写一个python脚本,它使用一个查询从我们的配置单元服务器提取数据。我期待一个输出,将过滤卡号有x或更多的交易,每天根据输入“txncount”。
输入为:datetime1、datetime2、merchantid、cardnum、terminalid和txncount。
我的代码(不工作):

Query = "SELECT TRIM(i002_number) as CardNum, i004_amt_trxn, TRIM(i042_merch_id) as MerchantID, i043a_merch_name, TRIM(i041_pos_id) as TerminalID, \
i049_cur_trxn, i062v2_trans_id, i003_proc_code, i006_amt_bill, i051_cur_bill, amt_card, cardcurrency, ltimestamp, \
i039_rsp_cd, i018_merch_type, i043b_merch_city, i043c_merch_cnt, i022_pos_entry, i032_acquirer_id, trxntype, reasoncode, \
SUBSTRING(i002_number, 1, 6) AS issuer_bin, COUNT(i002_number) as txncount\
CASE \
    WHEN SUBSTRING(i002_number,1,1) = 5 THEN 'MasterCard' \
    WHEN SUBSTRING(i002_number,1,1) = 4 THEN 'VISA' \
END AS source \
FROM tsys.ods_authorizations \
WHERE ltimestamp >= '"+DateTime1+"' AND ltimestamp <= '"+DateTime2+"' AND i042_merch_id = "+MerchantID+" \
AND i002_number = "+CardNum+" AND i041_pos_id = "+terminalID+""
HAVING txncount >= '"+TxnCount+"'

预期数据示例(截断):

CardNum         TimeStamp           TxnCount
123      2019-06-01 00:00:30.00        2   
123      2019-06-01 05:00:20.00        2
123      2019-06-03 20:00:00.00        1
456      2019-06-04 06:00:00.00        2
456      2019-06-04 00:00:10.91        2
789      2019-06-01 12:00:40.51        1

我想我这里的问题是,它不能计算每个卡号,因为我有问题,由条款组。另外,我还没有拆分日期和时间,查询还不能识别日期之间的差异。

r6vfmomb

r6vfmomb1#

您的查询格式不正确。你有一个 COUNT() 有一堆其他的专栏——你没有 GROUP BY . 这在sql中是不允许的。
我建议您也使用参数,而不是使用查询字符串。所以,你大概想要这样的东西。
您的结果似乎需要每个事务的详细信息,而不是摘要(因此您有两行计数为“2”而不是一行)。这表明您确实需要窗口函数:

SELECT a.*
FROM (SELECT a.*,
             (CASE WHEN i002_number LIKE '5%' THEN 'MasterCard'
                   WHEN i002_number LIKE '4%' THEN 'VISA'
              END) AS source
             COUNT(*) OVER (PARTITION BY i002_number, TRUNC(ltimestamp, 'DAY')) as txncount
      FROM FROM tsys.ods_authorizations a
      WHERE ltimestamp >= :timestamp1 AND
            ltimestamp <= :timestamp2 AND
            i042_merch_id = :MerchantID AND
            i002_number = :CardNum AND 
            i041_pos_id = :terminalID
     ) a
WHERE txncount >= :TxnCount

相关问题