选择与 usercolumn 不同的值以及 usercolumn 中每个字段的日期最小值

发布于 2024-07-25 06:05:40 字数 955 浏览 10 评论 0原文

我有一些像这样的数据,但超过 1500000 条记录和超过 700 个用户:

usercolumn   ,       datecolumn\     
a1            ,      1998/2/11\
a2            ,      1998/3/11\
a1            ,      1998/2/15\
a4            ,      1998/4/14\
a3            ,      1999/1/15\
a2            ,      1998/11/12\
a2            ,      1999/2/11\
a3            ,      2000/2/9\
a1            ,      1998/6/5\
a3            ,      1998/7/7\
a1            ,      1998/3/11\
a5            ,      1998/3/18\
a2            ,      1998/2/8\
a1            ,      1998/12/11\
a4            ,      1998/12/1\
a5            ,      1998/2/11\
....

我想从 usercolumn 中获得不同的数据以及每个用户的日期最小值,如下所示:

usercolumn      ,    datecolumn \        
a1              ,    1998/2/11\
a2              ,    1998/2/8\
a3              ,    1998/7/7\
a4              ,    1998/4/14\
a5              ,    1998/2/11\
....

请帮助我编写一个 SQL 命令来为 oledb 执行此操作C# 中的适配器,谢谢。

I have some data like this but more than 1500000 records and more than 700 users:

usercolumn   ,       datecolumn\     
a1            ,      1998/2/11\
a2            ,      1998/3/11\
a1            ,      1998/2/15\
a4            ,      1998/4/14\
a3            ,      1999/1/15\
a2            ,      1998/11/12\
a2            ,      1999/2/11\
a3            ,      2000/2/9\
a1            ,      1998/6/5\
a3            ,      1998/7/7\
a1            ,      1998/3/11\
a5            ,      1998/3/18\
a2            ,      1998/2/8\
a1            ,      1998/12/11\
a4            ,      1998/12/1\
a5            ,      1998/2/11\
....

I would like to have distinct data from usercolumn and minimum value of date for each user like this:

usercolumn      ,    datecolumn \        
a1              ,    1998/2/11\
a2              ,    1998/2/8\
a3              ,    1998/7/7\
a4              ,    1998/4/14\
a5              ,    1998/2/11\
....

please help me to write an SQL command to do this for oledb adapter in c#, thanks.

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

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

发布评论

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

评论(6

空气里的味道 2024-08-01 06:05:40
SELECT usercolumn, MIN(datecolumn) FROM tablename GROUP BY usercolumn;

请注意,如果您需要其他列,它们必须出现在 GROUP BY 子句中或者在各行中保持不变。 否则结果将是不确定的。

SELECT usercolumn, MIN(datecolumn) FROM tablename GROUP BY usercolumn;

Note that if you want other columns they must either appear in the GROUP BY clause or be constant across rows. Otherwise the result will be non-deterministic.

演出会有结束 2024-08-01 06:05:40

这适用于 SQLServer 2008 和 DB2:

with temp as (
    select *, row_number() over (partition by usercolumn order by datecolumn) as rownum
    from table)
select * from temp 
where rownum = 1

即使您需要在选择中包含多个列,它也会给出正确的结果。

This will work for SQLServer 2008 and DB2:

with temp as (
    select *, row_number() over (partition by usercolumn order by datecolumn) as rownum
    from table)
select * from temp 
where rownum = 1

It will give proper results even if you need to include multiple columns in the select.

冷夜 2024-08-01 06:05:40

像这样的事情应该做的

    SELECT usercolumn
         , MIN(datecolumn)
      FROM YouTable
     GROUP BY usercolumn
         , MIN(datecolumn)

Something like this should do the tick

    SELECT usercolumn
         , MIN(datecolumn)
      FROM YouTable
     GROUP BY usercolumn
         , MIN(datecolumn)
本宫微胖 2024-08-01 06:05:40

如果您不仅仅有这两列,那么要使用的最佳 SQL 有点取决于 OleDB 适配器另一端的服务器,但这里有一些可以很好地与许多(唉,不是全部!)这样的可能一起工作的东西服务器:

SELECT t.*
FROM thetable t
LEFT JOIN thetable taux
  ON(t.usercolumn=taux.usercolumn
     AND t.datecolumn>taux.datecolumn)
WHERE taux.usecolumn IS NULL

您可以将其读作“发出表中的那些行,以便表中没有其他行具有相同的用户和严格较少的日期”。 如果给定用户的最小日期可以出现多次,这将为该用户提供尽可能多的行 - 如果这对您来说是个问题,也有解决方案......但我会等您澄清您的问题在我继续做这件事之前,请先了解更多!-)

If you have more than just those two columns, the best SQL to use depends a bit on what server you have at the other end of that OleDB adapter, but here's something that will work well with many (alas, not all!) such possible servers:

SELECT t.*
FROM thetable t
LEFT JOIN thetable taux
  ON(t.usercolumn=taux.usercolumn
     AND t.datecolumn>taux.datecolumn)
WHERE taux.usecolumn IS NULL

which you could read as "emit those rows of thetable such that there is no other row of the table with the same user and a strictly-less date". If minimum date for a given user can occur multiple times this will give as many rows for that user -- if that's a problem for you, there are solutions to it, too... but I'll wait for you to clarify your question more before I work any more on this!-)

纵山崖 2024-08-01 06:05:40

你可以尝试这个:

SELECT DISTINCT a.username, a.date
FROM tablename AS a INNER JOIN tablename AS b
ON(a.username = b.username AND a.date < b.date)

至于C#,无法帮助你

you could try this:

SELECT DISTINCT a.username, a.date
FROM tablename AS a INNER JOIN tablename AS b
ON(a.username = b.username AND a.date < b.date)

As for C#, cant help you there

佼人 2024-08-01 06:05:40

从表名中选择不同的用户名和日期作为 A
WHERE A.DATE=(SELECT MIN(DATE) FROM TABLENAME WHERE USERNAME=A.USERNAME)

SELECT DISTINCT USERNAME, DATE FROM TABLENAME AS A
WHERE A.DATE=(SELECT MIN(DATE) FROM TABLENAME WHERE USERNAME=A.USERNAME)

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