【问题标题】:SQL Server 2008 - field to concatenate stringsSQL Server 2008 - 连接字符串的字段
【发布时间】:2013-05-10 04:21:49
【问题描述】:

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

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

【问题讨论】:

  • 在内部查询中还要在where条件中包含Trans_Id。
  • 您好,尝试添加“and a.tran_id = b.tran_id”,但错误消息“transactions.trans_id”在选择列表中无效,因为它不包含在聚合函数或 GROUP 中BY 子句。”如果我然后按 trans_id 添加到组中,我会为每个添加一行...
  • @HL8 你能提供一些示例数据吗?我希望能帮助您解决问题
  • 嗨,Luv,提前感谢您的帮助。我已经更新了示例数据。
  • trans_id 条件应该也在查询的for xml 部分。

标签: sql-server-2008 concatenation


【解决方案1】:

我猜您的问题是您还需要在查询的for xml 部分中过滤product_items。您可以通过使用 CTE 来做到这一点,您可以在其中从 transactions 查询所需的行,然后使用 CTE 连接 Trans_ID。

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

SQL Fiddle

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)

查询 1:

-- 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

Results:

| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 | 1,2,3,4,5 |

查询 2:

-- 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

Results:

| CUSTOMER_NO | CARD_NO | NO_TRANS | TRANS_IDS |
------------------------------------------------
|        1234 |   12345 |        2 |       1,2 |

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多