如何在SQLServer 2005中与主键一起写入Disctict [英] How to write disctict along with primary key in sqlserver 2005

查看:168
本文介绍了如何在SQLServer 2005中与主键一起写入Disctict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

在这里,我的表格中有相同的emailid,所以我只想获得一个email id

Dear Friends,

Here i have same emailid in my table so i want to get only one email id

ex:1,anil@gmail.com
   2,anil@gmail.com



所以我只想从数据库中获取一个电子邮件ID并绑定到gridview.

所以我写了这样的查询



so i want to fetch only one email id from database and bind to gridview.

so i have written a query like this

select distinct(Emailid),StoreUserId from StoreUserEmails



但它不来了

如果我这样使用,请从StoreUserEmails中选择与众不同的(Emailid)

当我绑定到gridview时,错误就会像这样



but its not coming

if i use like this select distinct(Emailid) from StoreUserEmails

the error is coming like this when i bind to gridview

Error:Keyfeildname is essential



请解决我的问题

问候,

Anilkumar.D



Please solve my problem

Regards,

Anilkumar.D

推荐答案

SELECT  *
FROM    (SELECT EmailId, StoreUserId,
               ROW_NUMBER() OVER (PARTITION BY EmailId ORDER BY StoreUserId) AS RowNumber
         FROM   StoreUserEmails
         WHERE  EmailId LIKE ''anil@gmail.com'') AS a
WHERE   a.RowNumber = 1





希望这对您有帮助...





hope this will help you......


卸下括号:
Remove the brackets:
SELECT DISTINCT Emailid FROM StoreUserEmails



请参阅 distinct的定义 [



See the definition of distinct[^]

[edit]Removed spurious pre tag - OriginalGriff


尝试一下.

try this.

begin

select  * into #val from val
create table #output(id int, email varchar(100))


while((select COUNT(*) from #val) > 0)
begin

 
	insert #output   select top(1) * from #val
    delete from #val where email =    (select top(1) email from #val)
    
end
select * from #output
end


这篇关于如何在SQLServer 2005中与主键一起写入Disctict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆