Sql Group by 对于除一列外所有列都相同的行

发布于 2024-12-13 21:05:57 字数 617 浏览 0 评论 0原文

我有一个包含许多列的表,将用于保存用户存储的搜索。

每天需要发送一封包含最新搜索结果的电子邮件。 可能有数千个已保存的搜索。

我保存的搜索表中的列类似于以下内容:

Id 
userEmail 
SearchParam1 
SearchParam2 
SearchParam3

由于会有很多搜索,我预计会有很多相同的搜索,因此我想获取 param1,2 和 3 相同但仍然有的每一行访问用户的电子邮件,这样我就可以向每个用户发送相同的结果,而不必多次运行相同的搜索。

理想情况下,我想要这样的东西:

SearchParam1, SearchParam2, SearchParam3
|
|____Email1
|
|____Email2
|
|____Email3

按搜索参数分组,但以某种方式链接到电子邮件。

我什至不知道从哪里开始,因此为什么我没有添加任何代码。 有人可以给我一些建议吗?

编辑 只是为了澄清。一封电子邮件不会在表中出现多次。因此从技术上讲,他们每人只能保存 1 次搜索。

没有这样的用户,只有电子邮件地址。

贝克斯

I have a table with a number of columns that will be used to save searches stored by a user.

Every day an email needs to be sent out containing the latest search results.
There could be thousands of saved searches.

My columns in my saved search table are similar to as follows:

Id 
userEmail 
SearchParam1 
SearchParam2 
SearchParam3

As there will be so many searches I anticipate there will be many that are the same so I would like to get every row where param1,2 and 3 are the same but still have access to the users email so I can send the same results to each without having to run the same search multiple times.

Ideally I'd like something like this back:

SearchParam1, SearchParam2, SearchParam3
|
|____Email1
|
|____Email2
|
|____Email3

Kind of grouped by the search params but linked to the emails some how.

I don't even know where to start, hence why I have not added any code.
Can anyone give me some suggestions?

EDIT
Just to clarify. One email would not exist more than once in the table. So technically they are only allowed to save 1 search each.

There are no users as such, just email addresses.

Bex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-12-20 21:05:58

在 SQL 中,起点是:(

select SearchParam1, SearchParam2, SearchParam3, userEmail, count(*) query_count
from MyTable
group by SearchParam1, SearchParam2, SearchParam3, userEmail
order by 1,2,3,4

如果您不想查看同一用户运行相同查询的次数,可以省略 group by 子句和 count( *) 项,只需包含 select unique 即可。)

这将在每个用户的电子邮件旁边重复搜索参数。

如果您只想在每次更改其中之一时将搜索参数作为标题包含在内,我建议在报告工具(例如 SQLServer 报告服务)中使用上述查询 - 对搜索参数进行分组,在组中包含参数字段仅标题并仅在详细信息行中包含用户电子邮件。

编辑:如果 userEmail 唯一标识一条记录,则查询可以简化为:

select SearchParam1, SearchParam2, SearchParam3, userEmail
from MyTable
order by 1,2,3,4

In SQL, a starting point would be:

select SearchParam1, SearchParam2, SearchParam3, userEmail, count(*) query_count
from MyTable
group by SearchParam1, SearchParam2, SearchParam3, userEmail
order by 1,2,3,4

(If you don't want to see how many times the same user ran the same query, you can omit the group by clause and the count(*) item, and just include select distinct instead.)

This will repeat the search parameters next to each user's e-mail.

If you want to only include the search parameters as a heading each time one of them changes, I suggest using the above query in a reporting tool (such as SQLServer reporting services) - group on the search parameters, include the parameter fields in the group header only and include the user eMail in the detail line only.

EDIT: if userEmail uniquely identifies a record, the query can be simplified to:

select SearchParam1, SearchParam2, SearchParam3, userEmail
from MyTable
order by 1,2,3,4
居里长安 2024-12-20 21:05:57

您真正应该考虑的是数据标准化。这看起来像多对多关系,因此这样的结构是个好主意:

create table yourUsersTable
(
    id int identity(1, 1) primary key clustered not null,
    name varchar(1000) not null,
    email varchar(1000) not null
)
go

create table searchParams
(
    id int identity(1, 1) primary key clustered not null,
    searchText varchar(1000) not null
)
go

create table userSearchParams
(
    fkYourUsersTable int not null references yourUsersTable(id),
    fkSearchParams int not null references searchParams(id)
)
go

这样,如果您想获取订阅搜索参数的用户的所有电子邮件地址,那么这将是一个简单的查询:

select u.email
from yourUsersTable u
inner join userSearchParams up
on u.id = up.fkYourUsersTable
inner join searchParams p
on p.id = up.fkSearchParams
where p.searchText = 'your search parameter here'

这是使用多对多关系实现数据规范化的基本示例。它利用连接表来创建用户和搜索参数之间的关系。通过上述设计,数据检索变得更加简单,并且您不会受到原始非规范化数据(具有 searchParam1searchParam2 等列)的限制。

请告诉我这是否有意义。

What you really should consider is data normalization. This looks like a many-to-many relationship, so a structure like this would be idea:

create table yourUsersTable
(
    id int identity(1, 1) primary key clustered not null,
    name varchar(1000) not null,
    email varchar(1000) not null
)
go

create table searchParams
(
    id int identity(1, 1) primary key clustered not null,
    searchText varchar(1000) not null
)
go

create table userSearchParams
(
    fkYourUsersTable int not null references yourUsersTable(id),
    fkSearchParams int not null references searchParams(id)
)
go

This way, if you wanted to get all the email addresses for users subscribed to a search parameters, it'd be a simple query:

select u.email
from yourUsersTable u
inner join userSearchParams up
on u.id = up.fkYourUsersTable
inner join searchParams p
on p.id = up.fkSearchParams
where p.searchText = 'your search parameter here'

This is a basic example of implementing data normalization with a many-to-many relationship. It utilizes a join table to create the relationship between users and search parameters. Data retrieval has become much simpler with the above design, and you aren't constricted by your original denormalized data (with columns like searchParam1, searchParam2, etc.).

Please let me know if that made sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文