在 SQL Server Compact 中加入逗号分隔值不起作用

Join with comma separated values in SQL Server Compact not working

我有 2 个 table 如下

CREATE TABLE [Chart]
(
   [Id] BIGINT NOT NULL IDENTITY (1,1),
   [Owner] NVARCHAR(100),
   [Identifier] NVARCHAR(128) NOT NULL,
   [Title] NVARCHAR(100),
   [Type] INT NOT NULL,
   [Category] INT NOT NULL,
   [CreatedDate] DATETIME NOT NULL,
   [ModifiedDate] DATETIME,
   [ExpireDate] DATETIME,
   [VisitCount] BIGINT NOT NULL,
   [ConfigurationId] BIGINT NOT NULL,
   [Theme] NVARCHAR(50)
);

ALTER TABLE [Chart] ADD CONSTRAINT [PK_dbo.Chart] PRIMARY KEY ([Id]);


CREATE TABLE [SelectedTag]
(
   [Id] BIGINT NOT NULL IDENTITY (1,1),
   [Identifier] NVARCHAR(4000),
   [TagName] NVARCHAR(4000),
   [Description] NVARCHAR(4000),
   [EuUnit] NVARCHAR(50),
   [EuRangeLow] NVARCHAR(50),
   [EuRangeHigh] NVARCHAR(50),
   [MinStats] FLOAT,
   [MaxStats] FLOAT,
   [Min] FLOAT,
   [Max] FLOAT,
   [GroupCode] NVARCHAR(4000)
);

ALTER TABLE [SelectedTag] ADD CONSTRAINT [PK_dbo.SelectedTag] PRIMARY KEY ([Id]);

我试过这个答案查询 Tag Count 和 TagList for one Id 它在 SQL Server

中运行良好

https://dba.stackexchange.com/q/112408/172901

select c.Identifier, count(c.Identifier) as TagCount, (
       select concat(',', s.TagName)  from selectedtag  as s
       where concat(',', c.identifier, ',') like '%,'+s.identifier+',%'
       for xml path(''), type
       ).value('substring(text()[1], 2)', 'varchar(max)') as TagList
 from chart c
 group by c.Identifier

但是对于 SQL 服务器紧凑型数据库它不起作用

我使用 CompactView 运行 查询 SQL 压缩数据库

https://sourceforge.net/projects/compactview/

我的查询有什么问题或 SQL 压缩问题?谢谢

XML SQL Server Compact 不支持数据类型和函数,嵌套的 sSELECT 也不起作用。写一个普通的select,在代码中进行额外的处理

试试这个:

SELECT c.[Identifier]
      ,COUNT(c.[Identifier]) AS TagCount
      ,STRING_AGG(ST.[TagName], ',') AS TagList
FROM [Chart] C
INNER JOIN selectedtag ST
    ON C.[Identifier] = ST.[Identifier]
GROUP BY c.[Identifier];

由于您使用的是 SQL Server 2017,因此您可以使用 STRING_AGG 来连接字符串。