选择与 usercolumn 不同的值以及 usercolumn 中每个字段的日期最小值
我有一些像这样的数据,但超过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请注意,如果您需要其他列,它们必须出现在 GROUP BY 子句中或者在各行中保持不变。 否则结果将是不确定的。
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.
这适用于 SQLServer 2008 和 DB2:
即使您需要在选择中包含多个列,它也会给出正确的结果。
This will work for SQLServer 2008 and DB2:
It will give proper results even if you need to include multiple columns in the select.
像这样的事情应该做的
Something like this should do the tick
如果您不仅仅有这两列,那么要使用的最佳 SQL 有点取决于 OleDB 适配器另一端的服务器,但这里有一些可以很好地与许多(唉,不是全部!)这样的可能一起工作的东西服务器:
您可以将其读作“发出表中的那些行,以便表中没有其他行具有相同的用户和严格较少的日期”。 如果给定用户的最小日期可以出现多次,这将为该用户提供尽可能多的行 - 如果这对您来说是个问题,也有解决方案......但我会等您澄清您的问题在我继续做这件事之前,请先了解更多!-)
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:
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!-)
你可以尝试这个:
至于C#,无法帮助你
you could try this:
As for C#, cant help you there
从表名中选择不同的用户名和日期作为 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)