gpt4 book ai didi

sql-server - SQL 服务器 : Dynamic columns returned from query

转载 作者:行者123 更新时间:2023-12-04 01:54:01 25 4
gpt4 key购买 nike

我有一个针对 data.stackexchange.com 运行的相当复杂的 SQL 查询。查询可以在this link找到。 (并粘贴在下面)。

DECLARE @Location varchar(128) = ##Location:string##
DECLARE @RepLimit int = ##RepLimit:int##

SELECT Users.DisplayName,
Users.Id,
Users.WebsiteUrl,
Users.Reputation,
(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id = 3 -- Javascript
AND Posts.OwnerUserId = Users.Id
) AS JavascriptCount,

(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id = 5 -- PHP
AND Posts.OwnerUserId = Users.Id
) AS PhpCount,

(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id = 820 -- jQuery
AND Posts.OwnerUserId = Users.Id
) AS jQueryCount,

(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id = 21 -- MySQL
AND Posts.OwnerUserId = Users.Id
) AS MySqlCount,

(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id = 1386 -- Android
AND Posts.OwnerUserId = Users.Id
) AS AndroidCount,

(
SELECT COUNT(*)
FROM Posts
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id
WHERE Posts.PostTypeId = 2
AND Tags.Id IN (58338, 81106, 92809, 7003) -- IOS
AND Posts.OwnerUserId = Users.Id
) AS IosCount

FROM Users

WHERE Users.Reputation > @RepLimit
AND Users.Location = @Location

在上面的查询中,发生了一些事情:

  1. 在 data.stackexchange.com 上,他们为我生成了一个表单字段,用于输入我在顶部DECLAREd 的数据(在本例中为 Location 和 RepLimit)。
  2. 我要搜索的标签(Javascript、iOS、Android、PHP 等)是硬编码的,每个标签都使用子选择,效率不如我想象的那么高。

我想改变两件事,但我对 SQL Server 不够熟悉,不知道它们是否可行(也不知道究竟要查询什么才能找到我需要的结果)。这两个变化是:

  1. 我想优化查询。现在,我觉得进行六个几乎相同的子选择并不是实现最终结果的理想方式。
  2. 我希望标签列表在新的表单元素中以逗号分隔的列表形式提供(在顶部作为 DECLAREd,如 Location 和 RepLimit),或者作为 5单独的表单字段(将查询限制为最多 5 个不同的标签)

有没有人有过类似查询的经验(或类似的问题,让查询返回的实际列是动态的?)。感谢您的帮助。

最佳答案

关于您的第一个问题,这里是一种优化查询的方法:

DECLARE @Location varchar(128) = ##Location:string##
DECLARE @RepLimit int = ##RepLimit:int##

SELECT Users.DisplayName,
Users.Id,
Users.WebsiteUrl,
Users.Reputation,
sum(case when Tags.Id = 3 then 1 else 0 end) as JavascriptCount,
sum(case when Tags.Id = 5 then 1 else 0 end) as PhpCount,
sum(case when Tags.Id = 820 then 1 else 0 end) as jQueryCount,
sum(case when Tags.Id = 21 then 1 else 0 end) as MySqlCount,
sum(case when Tags.Id = 1386 then 1 else 0 end) as AndroidCount,
sum(case when Tags.Id IN (58338, 81106, 92809, 7003) then 1 else 0 end) as IosCount

FROM Users
JOIN Posts ON Posts.OwnerUserId = Users.Id
JOIN PostTags ON Posts.ParentId = PostTags.PostId
JOIN Tags ON PostTags.TagId = Tags.Id

WHERE Users.Reputation > @RepLimit AND Posts.PostTypeId = 2
AND Users.Location = @Location

GROUP BY
Users.DisplayName,
Users.Id,
Users.WebsiteUrl,
Users.Reputation

至于你的第二个问题,我对那些 stackexchange 表格一无所知 - 恐怕我无能为力。但是由于您提到的是逗号分隔列表,您可以利用 SQL 的 CHARINDEX() 函数来查找参数中的 Tags.Id。像这样:

case when CHARINDEX(','+convert(varchar,Tags.Id)+',',','+@List1+',') <> 0 then 1 else 0 end 

关于sql-server - SQL 服务器 : Dynamic columns returned from query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com