Sql server 2008 SQL Server 2008-用于连接字符串的字段

Sql server 2008 SQL Server 2008-用于连接字符串的字段,sql-server-2008,concatenation,Sql Server 2008,Concatenation,我正在运行以下脚本,但连接字段返回的值不正确 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,

我正在运行以下脚本,但连接字段返回的值不正确

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
但我得到的是

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

我猜您的问题是,您还需要针对查询的
for xml
部分中的
产品项进行过滤。您可以通过使用CTE来实现这一点,在CTE中,您可以从
事务中查询所需的行,然后使用CTE连接
事务ID

下面是一个SQL,其中包含一些示例数据,这些数据显示了我认为是您的问题,以及一个使用CTE的查询,该查询应该满足您的需要

MS SQL Server 2008架构设置

create table transactions
(
  Customer_No int,
  Card_No int,
  Trans_ID int
)

create table product_items
(
  Trans_ID int,
  Product_item int
)

insert into transactions values
(1234, 12345, 1),
(1234, 12345, 2),
(1234, 12345, 3),
(1234, 12345, 4),
(1234, 12345, 5)

insert into product_items values
(1, 298029),
(2, 298029),
(3, 298020),
(4, 298020),
(5, 298020)
-- Your query
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 trans_id in (select trans_id
                   from product_items (nolock)
                   where product_item in ('298029'))
group by customer_no, card_no
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |
-- Rewritten to use a CTE
with C as
(
  select T.Customer_No, 
         T.Card_No,
         T.Trans_ID
  from transactions as T
  where T.Trans_ID in (select P.Trans_ID
                       from product_items as P
                       where P.Product_Item in ('298029'))
)
select C1.Customer_No,
       C1.Card_No,
       count(*) as No_Trans,
       stuff((select ',' + cast(C2.Trans_ID as varchar(20))
              from C as C2 
              where C1.Card_No = C2.Card_No and
                    C1.Customer_No = C2.Customer_No
              for xml path('')), 1, 1, '') as Trans_IDs
from C as C1
group by C1.Customer_No, 
         C1.Card_No
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |
查询1

create table transactions
(
  Customer_No int,
  Card_No int,
  Trans_ID int
)

create table product_items
(
  Trans_ID int,
  Product_item int
)

insert into transactions values
(1234, 12345, 1),
(1234, 12345, 2),
(1234, 12345, 3),
(1234, 12345, 4),
(1234, 12345, 5)

insert into product_items values
(1, 298029),
(2, 298029),
(3, 298020),
(4, 298020),
(5, 298020)
-- Your query
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 trans_id in (select trans_id
                   from product_items (nolock)
                   where product_item in ('298029'))
group by customer_no, card_no
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |
-- Rewritten to use a CTE
with C as
(
  select T.Customer_No, 
         T.Card_No,
         T.Trans_ID
  from transactions as T
  where T.Trans_ID in (select P.Trans_ID
                       from product_items as P
                       where P.Product_Item in ('298029'))
)
select C1.Customer_No,
       C1.Card_No,
       count(*) as No_Trans,
       stuff((select ',' + cast(C2.Trans_ID as varchar(20))
              from C as C2 
              where C1.Card_No = C2.Card_No and
                    C1.Customer_No = C2.Customer_No
              for xml path('')), 1, 1, '') as Trans_IDs
from C as C1
group by C1.Customer_No, 
         C1.Card_No
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |

create table transactions
(
  Customer_No int,
  Card_No int,
  Trans_ID int
)

create table product_items
(
  Trans_ID int,
  Product_item int
)

insert into transactions values
(1234, 12345, 1),
(1234, 12345, 2),
(1234, 12345, 3),
(1234, 12345, 4),
(1234, 12345, 5)

insert into product_items values
(1, 298029),
(2, 298029),
(3, 298020),
(4, 298020),
(5, 298020)
-- Your query
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 trans_id in (select trans_id
                   from product_items (nolock)
                   where product_item in ('298029'))
group by customer_no, card_no
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |
-- Rewritten to use a CTE
with C as
(
  select T.Customer_No, 
         T.Card_No,
         T.Trans_ID
  from transactions as T
  where T.Trans_ID in (select P.Trans_ID
                       from product_items as P
                       where P.Product_Item in ('298029'))
)
select C1.Customer_No,
       C1.Card_No,
       count(*) as No_Trans,
       stuff((select ',' + cast(C2.Trans_ID as varchar(20))
              from C as C2 
              where C1.Card_No = C2.Card_No and
                    C1.Customer_No = C2.Customer_No
              for xml path('')), 1, 1, '') as Trans_IDs
from C as C1
group by C1.Customer_No, 
         C1.Card_No
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |
查询2

create table transactions
(
  Customer_No int,
  Card_No int,
  Trans_ID int
)

create table product_items
(
  Trans_ID int,
  Product_item int
)

insert into transactions values
(1234, 12345, 1),
(1234, 12345, 2),
(1234, 12345, 3),
(1234, 12345, 4),
(1234, 12345, 5)

insert into product_items values
(1, 298029),
(2, 298029),
(3, 298020),
(4, 298020),
(5, 298020)
-- Your query
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 trans_id in (select trans_id
                   from product_items (nolock)
                   where product_item in ('298029'))
group by customer_no, card_no
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |
-- Rewritten to use a CTE
with C as
(
  select T.Customer_No, 
         T.Card_No,
         T.Trans_ID
  from transactions as T
  where T.Trans_ID in (select P.Trans_ID
                       from product_items as P
                       where P.Product_Item in ('298029'))
)
select C1.Customer_No,
       C1.Card_No,
       count(*) as No_Trans,
       stuff((select ',' + cast(C2.Trans_ID as varchar(20))
              from C as C2 
              where C1.Card_No = C2.Card_No and
                    C1.Customer_No = C2.Customer_No
              for xml path('')), 1, 1, '') as Trans_IDs
from C as C1
group by C1.Customer_No, 
         C1.Card_No
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |

create table transactions
(
  Customer_No int,
  Card_No int,
  Trans_ID int
)

create table product_items
(
  Trans_ID int,
  Product_item int
)

insert into transactions values
(1234, 12345, 1),
(1234, 12345, 2),
(1234, 12345, 3),
(1234, 12345, 4),
(1234, 12345, 5)

insert into product_items values
(1, 298029),
(2, 298029),
(3, 298020),
(4, 298020),
(5, 298020)
-- Your query
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 trans_id in (select trans_id
                   from product_items (nolock)
                   where product_item in ('298029'))
group by customer_no, card_no
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |
-- Rewritten to use a CTE
with C as
(
  select T.Customer_No, 
         T.Card_No,
         T.Trans_ID
  from transactions as T
  where T.Trans_ID in (select P.Trans_ID
                       from product_items as P
                       where P.Product_Item in ('298029'))
)
select C1.Customer_No,
       C1.Card_No,
       count(*) as No_Trans,
       stuff((select ',' + cast(C2.Trans_ID as varchar(20))
              from C as C2 
              where C1.Card_No = C2.Card_No and
                    C1.Customer_No = C2.Customer_No
              for xml path('')), 1, 1, '') as Trans_IDs
from C as C1
group by C1.Customer_No, 
         C1.Card_No
| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |

在内部查询中,where条件中还包含Trans_Id。Hi尝试添加“and a.tran_Id=b.tran_Id”,但错误消息“transactions.Trans_Id”在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中。”如果我按trans_id添加到group中,我会得到每个…@HL8的一行,你能给出一些示例数据来处理吗。我希望能帮助你解决问题,亲爱的,提前谢谢你的帮助。我已经用示例数据进行了更新。
trans\u id
条件也应该在查询的
forxml
部分。