Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

marciolvs

[Resolvido] não exibir registro duplicados

Recommended Posts

Olá pessoal, tenho um banco onde possui verios nomes iguais, porem gostaria de fazer um select e que ele exibia apenas 1 nome em caso do mesmo estar repetido.

 

Alguem tem uma idéia de como é este select ?

 

Ex:

 

ID - Nome

------------

1 - Marcio

2 - Fulano

3 - Beltrano

6 - Marcio

5 - Marcio

 

Irá exibir

1 - Marcio

2 - Fulano

3 - Beltrano

 

 

 

os repetido não será exibidos + de 1 vez.

Compartilhar este post


Link para o post
Compartilhar em outros sites

faz assim, para ver se tem duplicados

 

 

 

Select para localizar duplicados

select campo,campo1,count(*)
     from tabela having count(*) > 1
    group by campo,campo1

deletar duplicados

delete from tab p1
   where rowid < (select max(rowid)
                    from tab1 p2
                   where p1.primary_key = p2.primary_key);

Compartilhar este post


Link para o post
Compartilhar em outros sites

SELECT DISTINCT Nome FROM tabela

 

Deucerto

 

<%

      SQL = "select DISTINCT dataVencimento From ContasAPagar"
	 'SQL = "select * From ContasAPagar Order BY dataVencimento DESC"
	  Call abreConexao
	  Set rs = conexaodb.execute(SQL)
	 
	  If Not Rs.EOF Then
		 Do While Not Rs.EOF		
		 	
			
			data = RS("dataVencimento")
			response.write (data)&"<br>"
			
		  Rs.MoveNext
		 Loop
	  End If
	  rs.close
	%>

Banco:

dataVencimento

20/10/2009

20/10/2009

20/10/2009

15/03/2010

30/04/2010

03/05/2010

03/05/2010

30/07/2010

 

Exibição:

20/10/2009

15/03/2010

30/04/2010

03/05/2010

30/07/2010

 

Ou seja os repetidos não exibi, era isso mesmo que eu precisava.

 

Mas como faço para ele me exibir apenas o Ano ?

Sendo que os anos repetidos não podem ser exibidos.

Compartilhar este post


Link para o post
Compartilhar em outros sites

ok, parabéns

Compartilhar este post


Link para o post
Compartilhar em outros sites

Opa, então depois de um tempo variando aqui na tela lembrei de "AS data" criando assim uma coluna no SQL.

 

Ficou assim

 

<%
      SQL = "select DISTINCT Year(dataVencimento) AS data From ContasAPagar"
	 'SQL = "select * From ContasAPagar Order BY dataVencimento DESC"
	  Call abreConexao
	  Set rs = conexaodb.execute(SQL)
	 
	  If Not Rs.EOF Then
		 Do While Not Rs.EOF		
		 	
			
			data = RS("data")
			response.write (data)&"<br>"
			
		  Rs.MoveNext
		 Loop
	  End If
	  rs.close
	%>

Agora ele lista somente o ano do que se encontra na coluna, porem sem os repetidos, ou seja, 1 de cada.

 

Obrigado a todos que deu uma ajuda

Compartilhar este post


Link para o post
Compartilhar em outros sites

olha um outro exemplo:

 

<%@Language="vbscript" %>
    <%
    Const adOpenForwardOnly = 0
    Const adLockReadOnly = 1
    Const adCmdText = &H0001
    Const adUseClient = 3
    %>
    <!DOCTYPE HTML Public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>Remover registros duplicados</TITLE>
    <STYLE TYPE="text/css">
    BODY { background-color: #336699; font-family: Verdana; font-size: 10pt; color: #FFFFFF }
    TD { font-family: Verdana; font-size: 10pt; color: #000000 }
    </STYLE>
    </HEAD>
    <BODY>
    <%
    sPassword = "admin" ' password 
    if Request("password") <> "" Then
    	Session("Password") = Request("password")
    End if
    if Session("Password") <> sPassword Then
    	Response.Write "<HTML><BODY><FORM ACTION=""removeduplicates.asp"" METHOD=""post"">Password: (CaSe SeNsiTivE) <INPUT TYPE=""password"" NAME=""password""><INPUT TYPE=""submit""></FORM>"
    Else
    	delDub = False
    	File = Request.ServerVariables("SCRIPT_NAME")
    	File = StrReverse(File)
    	Posi = Instr(File, "/")
    	File = Left(File, Posi - 1)
    	File = StrReverse(File)
    	if Request.Form("db") <> "" Then
    		On Error Resume Next
    		DSNName = Request.Form("db")
    		sdbTable = Request.Form("tb")
    		spriKey = Request.Form("pk")
    		sdbField = Request.Form("fn")
    		sdbPath = "DBQ=" & Server.Mappath(DSNName) & ";Driver={Microsoft Access Driver (*.mdb)};"
    		strSQL = "SELECT COUNT(*) AS ttCount FROM " & sdbTable
    		Set strConn = Server.CreateObject("ADODB.Connection")
    		strConn.Open sdbPath
    		Set rs = Server.CreateObject("ADODB.Recordset")
    		rs.CursorLocation = adUseClient
    		rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    		Set rs = strConn.Execute(strSQL)
    		tempArr = ""
    		totalFound = rs("ttCount")
    		rs.Close
    		Set rs = nothing
    		totalFound = CInt(totalFound)
    		Response.Write totalFound
    		Response.Write " records" & vbcrlf
    		Response.Write "<HR>" & vbcrlf
    		strSQL = "SELECT " & sdbField & "," & spriKey & " FROM " & sdbTable & " ORDER BY " & sdbField
    		Set rs = Server.CreateObject("ADODB.Recordset")
    		rs.CursorLocation = adUseClient
    		rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    		if Err = 0 Then
    			For i = 0 To (totalFound - 1)
    				rs.MoveFirst
    				rs.Move(i)
    				lookFor = rs.fields(sdbField)
    				if lookFor = tempVal Then
    					foundDups = rs.fields(spriKey)
    					tempArr = tempArr & foundDups & ","
    				End if
    				tempVal = lookFor
    			Next
    			rs.Close
    			Set rs = nothing
    			if tempArr <> "" Then
    				Response.Write "Primary Key ID contendo dados duplicados no campo selecionado:" & vbcrlf
    				Response.Write "<BR>" & vbcrlf
    			End if
    			if tempArr = "" Then
    				Response.Write "Não há registros duplicados encontrados." & vbcrlf
    			Else
    				tempArr = Left(tempArr, Len(tempArr) - 1)
    				Response.Write tempArr
    				tempArr = Split(tempArr, ",", -1, 1)
    				prCount = UBound(tempArr)
    				prCount = prCount + 1
    				Response.Write "<BR>" & vbcrlf
    				For i = 0 To (prCount - 1)
    					strSQL = "DELETE FROM " & sdbTable & " WHERE " & spriKey & " = " & tempArr(i)
    					strConn.Execute(strSQL)
    				Next
    			End if
    			delDub = True
    		Else
    			Response.Write Err.Description
    		End if
    		strConn.Close
    		Set strConn = nothing
    		if Err = 0 And delDub = True Then
    			if tempArr <> "" Then
    				Response.Write "<BR><B>" & prCount & "</B> registro duplicado"
    				if prCount <> 1 Then
    					Response.Write "s"
    				End if
    				Response.Write " excluída com sucesso." & vbcrlf
    			End if
    		End if
    	End if
    %>
    <HR><BR><DIV ALIGN="center"><SPAN id="result_box"><SPAN title="Remove duplicate database records" closure_hashCode_1phoep="158">Remover   duplicado registros de banco de dados</SPAN></SPAN></DIV>
    <FORM ACTION="<%=File %>" METHOD="post">
    <TABLE CELLPADDING="4" CELLSPACING="1" ALIGN="center" WIDTH="50%" STYLE="border: 1px outset" BGCOLOR="#FFFFFF">
    <TR>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">Database:</TD>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">
    <INPUT TYPE="text" SIZE="25" NAME="db" STYLE="text-align: right"><B>.mdb</B></TD></TR>
    <TR>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9"> Nome da Tabela:</TD>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">
    <INPUT TYPE="text" SIZE="25" NAME="tb"></TD></TR>
    <TR>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">Nome do Campo:</TD>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">
    <INPUT TYPE="text" SIZE="25" NAME="fn"></TD></TR>
    <TR>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">Primary Key:</TD>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">
    <INPUT TYPE="text" SIZE="25" NAME="pk"></TD></TR>
    <TR>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9"> </TD>
    <TD WIDTH="50%" BGCOLOR="#E9E9E9">
    <INPUT TYPE="submit" VALUE="Remove Duplicates"></TD></TR></TABLE></FORM>
    <HR>
    <%
    End if
    %>
    </BODY>
    </HTML>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.