如何在我的表中找到重复的ContactNo

时间:2016-07-19 06:59:02

标签: sql

如何在sql查询中找到我的表是否有针对以下给定表的ContactNo重复记录?

CREATE TABLE [dbo].[tblInquiry](
[Pk_InquiryID] [int] IDENTITY(1,1) NOT NULL,
[Source] [varchar](50) NOT NULL,
[Purpose] [varchar](50) NULL,
[InterestedIn] [varchar](50) NULL,
[OccupCat] [varchar](50) NULL,
[Possession] [varchar](50) NULL,
[ModeOfFinance] [varchar](50) NULL,
[Budget] [varchar](50) NULL,
[FK_InquiryForSite] [int] NOT NULL,
[PartyFirstName] [varchar](50) NOT NULL,
[PartyLastName] [varchar](50) NOT NULL,
[ContactNo] [varchar](50) NOT NULL,
[EmailID] [varchar](255) NULL,
[Address] [varchar](250) NULL,
[LastAddendedBY] [int] NOT NULL,
[DateOfInquiry] [datetime] NOT NULL,
[NextFollowedUpDate] [datetime] NULL,
[LastRemark] [varchar](250) NOT NULL,
[ImagePath] [nvarchar](250) NULL,
[Status] [varchar](50) NOT NULL,
[IsSiteVisited] [bit] NULL,
[Reference] [varchar](100) NULL,
[SourceDetail] [varchar](50) NULL,
[AttendedDate] [datetime] NULL,
[Rating] [tinyint] NULL,
[FK_SiteStructureId] [int] NULL,
[Fk_SourceId] [int] NULL,
[ImagePath2] [nvarchar](500) NULL,
[IsFromVisitForm] [tinyint] NULL)

我在下表中也有外键

CREATE TABLE [dbo].[tblInquiryRemarks](
    [Pk_InqRemarks] [int] IDENTITY(1,1) NOT NULL,
    [FollowedUpDate] [datetime] NOT NULL,
    [FollowedUpBy] [int] NOT NULL,
    [Fk_SiteId] [int] NULL,
    [Fk_InquiryId] [int] NOT NULL references tblInquiry(Pk_InquiryID),
    [Remarks] [varchar](250) NOT NULL)

如果存在如何删除一个查询中所有重复条目的第一条记录?

2 个答案:

答案 0 :(得分:2)

要了解将来如何执行此操作,请参阅this:)

select firstname as ClientName, u.userid ,count(d.Id) documents_count
from tbl_userinfo u
left join tbl_documents d
on u.Id = d.UserId
where d.ReviewStatus = 1
group by name, u.userid

(无法真正看到所有字段,因此请查看是否需要更改)

答案 1 :(得分:1)

select a.ClientName , count(b.Id) , a.UserId 
from       tbl_userinfo  as a 
left join  tbl_documents as b on a.UserId = b.UserId 
group by a.ClientName , a.UserId