我需要使用 SQL 从表中获取唯一的数据子集

发布于 2024-09-11 09:14:43 字数 583 浏览 0 评论 0原文

我正在通过 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 技术交流群。

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

发布评论

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

评论(5

栩栩如生 2024-09-18 09:14:43

首先,无论你如何切片,你都需要一个唯一的密钥。它可以是列的组合,但您绝对必须有某种方法来唯一标识每一行。如果没有这个,你最好的办法就是添加这样的约束。除了该约束之外,这种类型的请求最好通过具有单个唯一列来满足。但是,如果您没有这样的列,则可以通过将构成唯一键的列连接在一起来创建它:

Select T1.account, T1.phone, T1.subaccount, T1.locationId....
From "Table" As T1
Where (Cast(T1.UniqueCol1 As varchar(100)) 
    + "|" + Cast(T2.UniqueCol2 As varchar(100)
    ... ) = (
            Select Min( Cast(T2.UniqueCol1 As varchar(100))
                        + "|" + Cast(T2.UniqueCol2 As varchar(100))
                        ... 
                        ) 
            From "Table" As T2
            Where T2.account = T1.account
                And T2.phone = T1.phone
            )

在本例中,UniqueCol1 表示唯一键的列之一, UniqueCol2 代表下一个,依此类推。无论如何,这都不是一个快速查询。保证单个列是唯一的将使这个问题变得更加简单。这样,您可以执行类似于 Mike M 的解决方案的操作:

Select T1.account, T1.phone, T1.subaccount, T1.locationId....
From "Table" As T1
Where UniqueCol = (
                    Select Min( T2.UniqueCol ) 
                    From "Table" As T2
                    Where T2.account = T1.account
                        And T2.phone = T1.phone
                    )

这里需要指出的重要一点是,在上述两个解决方案中,“第一个”仅由找到的最低排序键值确定。如果“第一个”需要由其他内容(例如日期时间列)确定,则需要在帖子中提及。

编辑

鉴于您补充说这是导入,最简单的解决方案是向临时表添加自动增量列。在 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:

Select T1.account, T1.phone, T1.subaccount, T1.locationId....
From "Table" As T1
Where (Cast(T1.UniqueCol1 As varchar(100)) 
    + "|" + Cast(T2.UniqueCol2 As varchar(100)
    ... ) = (
            Select Min( Cast(T2.UniqueCol1 As varchar(100))
                        + "|" + Cast(T2.UniqueCol2 As varchar(100))
                        ... 
                        ) 
            From "Table" As T2
            Where T2.account = T1.account
                And T2.phone = T1.phone
            )

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:

Select T1.account, T1.phone, T1.subaccount, T1.locationId....
From "Table" As T1
Where UniqueCol = (
                    Select Min( T2.UniqueCol ) 
                    From "Table" As T2
                    Where T2.account = T1.account
                        And T2.phone = T1.phone
                    )

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).

匿名的好友 2024-09-18 09:14:43

尝试以下操作:

SELECT *, ROW_NUMBER() OVER (Order By account) AS Id
INTO #Table FROM import1

现在您已经有了主键-

    SELECT account, phone, MAX(Id) AS Id
    FROM #Table
    GROUP BY account, phone

编辑:我忘记了重要的部分。哦。

SELECT * FROM #Table T
WHERE T.Id IN (
        SELECT MAX(Id) AS Id
        FROM #Table
        GROUP BY account, phone )

Try the following:

SELECT *, ROW_NUMBER() OVER (Order By account) AS Id
INTO #Table FROM import1

Now you've got a primary key-

    SELECT account, phone, MAX(Id) AS Id
    FROM #Table
    GROUP BY account, phone

EDIT: I forgot the important part. doh.

SELECT * FROM #Table T
WHERE T.Id IN (
        SELECT MAX(Id) AS Id
        FROM #Table
        GROUP BY account, phone )
你如我软肋 2024-09-18 09:14:43

稍微优雅的解决方案:

SELECT account, phone, subaccount, locationid
FROM import1
WHERE account IN
(SELECT DISTINCT account, phone FROM import1)

Slightly more elegant solution:

SELECT account, phone, subaccount, locationid
FROM import1
WHERE account IN
(SELECT DISTINCT account, phone FROM import1)
善良天后 2024-09-18 09:14:43

听起来您想要一个独特的电话/帐户信息集合,然后您想要获取该独特组合的每个子帐户和位置。这是正确的吗?

如果我理解正确,您可能需要采用以下格式创建新表或视图:

import1
-------
id  |Phone  | Account

SubAccount  //SubAccountId may not be needed here...
----------
SubAccountId  |ImportId  | SubAccount

ImportLocation
--------------
ImportId |LocationId

如果我误解了您想要执行的操作,请告诉我...

更新:

您的此修改版本建议应该有效。只去掉了“电话”中的“分钟”并添加了一个明显的:

Select distinct account, 
       phone, 
       min(subaccount), 
       min(locationid) 
from  import1 
group by account, phone

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:

import1
-------
id  |Phone  | Account

SubAccount  //SubAccountId may not be needed here...
----------
SubAccountId  |ImportId  | SubAccount

ImportLocation
--------------
ImportId |LocationId

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:

Select distinct account, 
       phone, 
       min(subaccount), 
       min(locationid) 
from  import1 
group by account, phone
十六岁半 2024-09-18 09:14:43
select account, min(phone), min(subaccount), min(locationid) 
from import1 
group by account, phone 

似乎是我的 IDE 的最佳解决方案(Visual Fox Pro v9)

select account, min(phone), min(subaccount), min(locationid) 
from import1 
group by account, phone 

Seems the best solution for my IDE ( Visual Fox Pro v9 )

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