0

我有这个查询如下:

AS
BEGIN
SET NOCOUNT ON;
BEGIN
           SELECT *
    FROM   (SELECT [id],
                   [account],
                   [tag],
                   [location],
                   [whoAdded],
                   ROW_NUMBER() OVER (PARTITION BY whoAdded ORDER BY id DESC) AS rn
            FROM   accountLogAccounts) AS a
    WHERE  rn = 1
    FOR    XML PATH ('users'), TYPE, ELEMENTS, ROOT ('root');
END
END

我试图在此处包括用户帐户总数以及所有标签的计数。

该表如下所示:

id, account, tag, whoAdded
1, 123, dog, bob
2, 123, cat, jon
3, 125, pig, bob

预期结果:

用户 Bob 有 2 个不同的帐户和 2 个标签

用户 jon 有 1 个不同的帐户和 1 个标签

4

1 回答 1

0
Select count(distinct whoAdded), count(distinct tag), whoadded
FROM accountLogAccounts
group by whoadded

应该返回

2, 2, bob
1, 1, jon

如果您需要实际文本,请相应地应用字符串连接。

于 2013-11-12T21:00:29.813 回答