我正在运行以下脚本,但连接字段返回的值不正确。
select customer_no, card_no, count(*) as no_trans,
stuff((select ',' + CAST(trans_id as varchar(20))
from transactions a (nolock)
where a.customer_no = b.customer_no and a.card_no = b.card_no
for xml path('')),1,1,'') AS trans_ids
from transactions b (nolock)
where date>= '01 apr 2013'
and date < '30 apr 2013'
and trans_id in (select trans_id
from product_items (nolock)
where product_item in ('298029'))
group by customer_no, card_no
我期望得到的是没有。trans (count(*)) 其中包含 product_item 并将 trans_id 列表作为连接字段返回。
例如。
customer_No card_no no_trans trans_ids
1234 12345 2 1, 2
但我得到的是;
customer_No card_no no_trans trans_ids
1234 12345 2 1, 2, 3, 5, 6
有人可以告诉我我做错了什么吗?提前致谢。
样本数据
交易表
Customer_No Card_No Trans_ID
1234 12345 1
1234 12345 2
产品项目表
Trans_ID Product_item
1 298029
2 298029