使用双重嵌套 sql 查询 sybase 选择特定卡片
selecting specific cards in with a double nested sql query sybase
SELECT card_num,
COUNT(merchant_code)
from e_transaction
where trans_date between '20161017 00:00' and '20161017 23:59'
and channelid='02'
and trans_code='P'
and card_num in (select card_num,
sum(trans_amount)
from e_transaction
where trans_date between '20161017 00:00' and '20161017 23:59'
and channelid='02'
and trans_code='T'
and card_num not in (select card_num
from e_transaction
where trans_date between '20160724 00:00' and '20161016 23:59')
group by card_num
having sum(trans_amount) > 100000)
group by card_num
having count (merchant_code) > 1
我一直收到此查询的语法错误,但似乎无法找到错误所在。我试图在今天使用相同的 merchant_code 假脱机多个 card_num,trans_amount 大于 100000,并且在 20160724 和昨天
之间找不到
错误信息
[Incorrect syntax near ','] [Incorrect syntax near the keyword
'group']
正确格式化查询后,很容易发现问题。在您的第一个嵌套 select 中,您写道:
and card_num in (select card_num,
sum(trans_amount)
from e_transaction
...)
in
需要单列,但您的嵌套 select 给出了两列。您需要做的就是从嵌套的 select 中删除 sum(trans_amount)
,如下所示:
and card_num in (select card_num
from e_transaction
...)
SELECT card_num,
COUNT(merchant_code)
from e_transaction
where trans_date between '20161017 00:00' and '20161017 23:59'
and channelid='02'
and trans_code='P'
and card_num in (select card_num,
sum(trans_amount)
from e_transaction
where trans_date between '20161017 00:00' and '20161017 23:59'
and channelid='02'
and trans_code='T'
and card_num not in (select card_num
from e_transaction
where trans_date between '20160724 00:00' and '20161016 23:59')
group by card_num
having sum(trans_amount) > 100000)
group by card_num
having count (merchant_code) > 1
我一直收到此查询的语法错误,但似乎无法找到错误所在。我试图在今天使用相同的 merchant_code 假脱机多个 card_num,trans_amount 大于 100000,并且在 20160724 和昨天
之间找不到错误信息
[Incorrect syntax near ','] [Incorrect syntax near the keyword 'group']
正确格式化查询后,很容易发现问题。在您的第一个嵌套 select 中,您写道:
and card_num in (select card_num,
sum(trans_amount)
from e_transaction
...)
in
需要单列,但您的嵌套 select 给出了两列。您需要做的就是从嵌套的 select 中删除 sum(trans_amount)
,如下所示:
and card_num in (select card_num
from e_transaction
...)