我需要使用 SQL 从表中获取唯一的数据子集
我正在通过 SQL 命令使用 Dbase 表。我有以下免费表格可以使用。
import1
帐户、电话、子帐户、locationid
我需要帐户和电话的每个独特组合的第一次出现。表中没有一个字段是唯一的。我可以使用不同的限定符获取部分数据子集,但我还需要与其选择的记录相关的其他字段。
这可以做到吗? 谢谢
编辑:我发现我需要限定所选记录必须保持完整。
示例:
Import1
001 123-4567 123 0110
001 123-0001 234 0220
001 123-4567 456 0011
002 222-2222 010 0110
003 333-3333 333 0330
应返回
import1
001 123-4567 123 0110
001 123-0001 234 0220
002 222 -2222 010 0110
003 333-3333 333 0330
这就是我的意图。
I am working with a Dbase table via SQL commands. I have the following free table to work with.
import1
account, phone, subaccount, locationid
I need the first occurrence of each uinique combination of account and phone. No one field in the table is unique. I can get a partial subset of data using the distinct qualifier but i need the other fields that relate to the record its selecting also.
Can this be done?
Thanks
Edit: I found out that I need to qualify the selected records must stay intact.
Example:
Import1
001 123-4567 123 0110
001 123-0001 234 0220
001 123-4567 456 0011
002 222-2222 010 0110
003 333-3333 333 0330
should return
import1
001 123-4567 123 0110
001 123-0001 234 0220
002 222-2222 010 0110
003 333-3333 333 0330
That's my intent here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,无论你如何切片,你都需要一个唯一的密钥。它可以是列的组合,但您绝对必须有某种方法来唯一标识每一行。如果没有这个,你最好的办法就是添加这样的约束。除了该约束之外,这种类型的请求最好通过具有单个唯一列来满足。但是,如果您没有这样的列,则可以通过将构成唯一键的列连接在一起来创建它:
在本例中,
UniqueCol1
表示唯一键的列之一,UniqueCol2
代表下一个,依此类推。无论如何,这都不是一个快速查询。保证单个列是唯一的将使这个问题变得更加简单。这样,您可以执行类似于 Mike M 的解决方案的操作:这里需要指出的重要一点是,在上述两个解决方案中,“第一个”仅由找到的最低排序键值确定。如果“第一个”需要由其他内容(例如日期时间列)确定,则需要在帖子中提及。
编辑
鉴于您补充说这是导入,最简单的解决方案是向临时表添加自动增量列。在 SQL Server 中,这将是 IDENTITY 列,但其他数据库产品也有等效列。如果您这样做,那么我上面提出的最后一个解决方案就可以解决问题(只需将 UniqueCol 替换为您的 Identity 列的名称)。
First, no matter how you slice it, you need a unique key. It can be a combination of columns, but you absolutely must have some means of uniquely identifying each row. Without that, your best recourse is to add such a constraint. In addition to that constraint, this type of request is best served by also having a single unique column. However, if you do not have such a column, you can create it from the columns that do comprise the unique key by concatenating them together:
In this case
UniqueCol1
represents one of the columns of the unique key,UniqueCol2
represents the next and so on. This will not be a fast query by any means. Having a single column guaranteed to be unique would make this problem significantly simpler. With that, you can do something akin to Mike M's solution:An important point to be made here is that in both solutions above "first" is being determined simply by the lowest sorted key value found. If "first" needs to be determined by something else, like a datetime column, you need to mention that your in your post.
EDIT
Given your addition that this is an import, the simplest solution is to add an auto-incrementing column to your staging table. In SQL Server this would be an IDENTITY column but other database products have an equivalent. If you do that, then the last solution I presented above will do the trick (simply replace UniqueCol with the name of your Identity column).
尝试以下操作:
现在您已经有了主键-
编辑:我忘记了重要的部分。哦。
Try the following:
Now you've got a primary key-
EDIT: I forgot the important part. doh.
稍微优雅的解决方案:
Slightly more elegant solution:
听起来您想要一个独特的电话/帐户信息集合,然后您想要获取该独特组合的每个子帐户和位置。这是正确的吗?
如果我理解正确,您可能需要采用以下格式创建新表或视图:
如果我误解了您想要执行的操作,请告诉我...
更新:
您的此修改版本建议应该有效。只去掉了“电话”中的“分钟”并添加了一个明显的:
It sounds like you want a unique collection of Phone/Account info and THEN you want to get each Sub Account and Location for that unique combo. Is this correct?
If i'm understanding you correctly you may want to make new tables or views in the following format:
Let me know if I missunderstood what you are trying to do...
UPDATE:
This modified version of your suggestion should work. Only took the Min off of Phone and added a distinct:
似乎是我的 IDE 的最佳解决方案(Visual Fox Pro v9)
Seems the best solution for my IDE ( Visual Fox Pro v9 )