如何重新定义Distinct
我的老板给了我一项任务,但我不确定是否可行,因为大约两周后,我无法找到解决方案,所以我将其扔掉,向 SO 小组寻求任何形式的帮助。如果这让你伤脑筋,我深表歉意。
首先介绍一点背景知识:我们开发了一个数据库查询应用程序,允许用户从数据库中获取他们想要的任何内容,而无需了解任何 SQL。
我的问题:我们想要自定义“不同”选择的使用方式。我们希望它仅应用于某些字段,而不是将 unique 关键字应用于所选的行,以便使用与字段匹配的第一行。例如:
Bob Jones,122 Main Street,波特兰,俄勒冈州
苏·琼斯,俄勒冈州波特兰大街 122 号
玛丽·史密斯,俄勒冈州波特兰第 32 街 458 号
Ralph Smith, 458 32nd Street, Portland, OR
我们希望此功能能够正常工作,以便仅返回每个地址的第一行,如下所示:
Bob Jones, 122 Main Street, Portland, OR
Mary Smith, 458 32nd Street, Portland, OR
使用正常的不同行,将返回所有四行,这超出了我们的预期。有人对我们如何做到这一点有任何想法吗?谢谢。
My boss has given me an assignment that I'm not sure is possible since after about two weeks, I can't figure out a solution, so I'm throwing this out to ask for any sort of help from the SO group. If this breaks your brain, I apologize.
A little background first: We develop a database querying application that allows users to get back anything they want from the database without having to know any SQL.
My Problem: We want to customize way a "Distinct" selection is used. Instead of having the distinct keyword apply to the rows selected, we want it to apply to only certain fields, so that the first rows that match the fields will get used. For an example:
Bob Jones, 122 Main Street, Portland, OR
Sue Jones, 122 Main Street, Portland, OR
Mary Smith, 458 32nd Street, Portland, OR
Ralph Smith, 458 32nd Street, Portland, OR
we want this to work so that only the first rows for each address is returned as follows:
Bob Jones, 122 Main Street, Portland, OR
Mary Smith, 458 32nd Street, Portland, OR
With the normal distinct, all four rows would be returned, which is more than we want. Does anyone have any ideas as to how we can go about doing this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PARTITION BY Address
指定要分组的列列表,ORDER BY ID
指示您定义“第一个”项目的列列表。(确保前面的语句以分号终止。)
PARTITION BY Address
specifies the column list you want to group by, andORDER BY ID
indicates the column list you define the "first" item with.(Make sure the previous statement is terminated by a semicolon.)
您必须运行常规的“SELECT DISTINCT”sql 语句,该语句仅查询实际需要不同的字段。然后,一旦取回这些数据,就运行另一个查询,该查询采用具有匹配数据的 TOP 1 记录来获取非不同字段。目前我还没有找到在单个 SQL 语句中执行此操作的方法...
You have to run a regular "SELECT DISTINCT" sql statement that only queries for the fields the actually do need to be distinct. Then once you get those back, run another query that takes the TOP 1 record that has matching data to get the non-distinct fields. At the moment I don't see a way to do this in a single SQL statement...
SELECT MIN (a.firstname + a.lastname) ,来自 x Group By 地址的地址。
这是一个过于简单化的答案,但它表达了你的观点。它只会返回每个不同地址的“较小”名称。
您还需要使用字段串联来进行区分,但它会满足您的需要。
SELECT MIN (a.firstname + a.lastname) , address from x Group By address.
That's an overly simplistic answer, but it gets your point accross. it will return only the "lesser" name for each distinct address.
You'll need to use field concatenation to do the distinctions as well, but it will give you what you need.