Group By Query To Find Duplicate Field Value
Jul 23, 2001
Hello,
Not sure how to do this. I think I need to use a Group By query. I have a "Products" table with the following fields:
Program#
Number
UPC
Item
Item#
Size
Dept
Everything is specific to the Program#. In other words, for every instance of a Program# there can be more than one Product, which is specified by the "Number" field. So: SELECT * FROM Product WHERE [Program#] = '12345' should return this:
12345 | 1 | 000012345678 | Cookies | 98765 | 12ct | Retail |
12345 | 2 | 000012345678 | Cake | 98765 | 12ct | Retail |
12345 | 3 | 000012345678 | Ice Cream | 98765 | 12ct | Retail |
However, some recordsets are returning like this:
12345 | 1 | 000012345678 | Cookies | 98765 | 12ct | Retail
12345 | 1 | 000012345678 | Cake | 98765 | 12ct | Retail
12345 | 2 | 000012345678 | Ice Cream | 98765 | 12ct | Retail
In which case I have to fix them (the "Number" field) with an update query sp they are numbered sequentially. Luckily, this doesn't happen very often.
Can someone help me with a query that will return only those records with duplicate values in the "Number" field? I am not sure how to construct this query which I will use as a stored procedure.
TIA,
Bruce Wexler
Programmer/Analyst
View 1 Replies
ADVERTISEMENT
Aug 26, 2015
I've inherited a table of members that has the following structure:
CREATE TABLE [dbo].[dimMember](
[dimMemberId] [int] IDENTITY(1,1) NOT NULL,
[dimSourceSystemId] [int] NOT NULL CONSTRAINT [DF_dimMember_dimSourceSystemId] DEFAULT ((-1)),
[MemberCode] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](250) NOT NULL,
[LastName] [nvarchar](250) NOT NULL,
[Code] ....
Based on the way the data loads into the table there's a possibility of some records being near duplicates of each other. For example, we can have a member that has records that have the same first name, last name, SSN, but different addresses, membercodes, subscribercode etc... This can happen in pretty much any variation thereof.
What I want to do, is add a new column and use that to group the similar records under based on comparing on several columns. By this I mean, if a member matches 4 of the 7 values below with another member, we would group these:
First Name (1st 3 characters)
Last Name
DOB
CurrentAddress1
MemberCode
SSN
SubscriberCode
I'm at a loss of how to structure the SQL to update the new column in the table.
View 9 Replies
View Related
May 28, 2012
I have the following table:
f_namef_countryf_ID
ABCUS123
DEFGB123
ABCUS456
GHIGB789
etc.
I need to run a query to discover all instances where a f_name and f_country pair exists for more than one f_ID. ABC/US is one such example; IDs 123 and 456 have this pair.
View 7 Replies
View Related
Jul 20, 2005
I have a .NET program that can connect to either an Access 97 database or anSQL Server 7 database. In the database I have two tables which have a fieldcalled ID. When I run a query like "SELECT A.*, B.* FROM A, B", the queryreturns those fields as "A.ID" and "B.ID" when connected to Access 97, butas "ID" and "ID" in SQL Server 7. Is there anyway to get SQL Server toprepend the table name to the field name in a case like this and not returnduplicate field names like that without having to specify aliases for thefields?- Don
View 1 Replies
View Related
Aug 25, 2015
I have a scenario as below for one ID -
+------+--------+----------------------------+-------+
| id | amount | date | descr|
+------+--------+-----------------------------+------+
| 5689 | 10.00 | 2015-08-25 12:10:57.107 | 4 |
| 5689 | 10.00 | 2015-08-24 12:07:57.107 | 3 |
| 5689 | 10.00 | 2015-08-25 12:05:57.107 | 3 |
| 5689 | 130.00 | 2015-08-24 12:07:57.107 | 4 |
| 5689 | 130.00 | 2015-08-25 12:07:57.107 | 3 |
+------+--------+-----------------------------+-----+
I want to fetch below 3 records from the above scenario i.e. latest record of each amount (Latest is determined using "descr" column i.e. 4 is greater then 3 -
+------+--------+----------------------------+-------+
| id | amount | date | descr|
+------+--------+-----------------------------+------+
| 5689 | 10.00 | 2015-08-25 12:10:57.107 | 4 |
| 5689 | 10.00 | 2015-08-24 12:07:57.107 | 3 |
| 5689 | 130.00 | 2015-08-24 12:07:57.107 | 4 |
+------+--------+-----------------------------+-----+
But in case of same amounts I am unable to fetch the latest status as even using partitioning will treat them as one.
CREATE TABLE #TMP
(
ID INT,
AMOUNT DECIMAL,
[DATE] DATETIME,
DESCR VARCHAR(10)
)
INSERT INTO #TMP VALUES
(5689,10.00,'2015-08-25 12:10:57.107','4')
,(5689,10.00,'2015-08-24 12:07:57.107','3')
,(5689,10.00,'2015-08-25 12:05:57.107','3')
,(5689,130.00,'2015-08-24 12:07:57.107','4')
,(5689,130.00,'2015-08-25 12:07:57.107','3')
View 8 Replies
View Related
Oct 2, 2007
Hello Everyone:
I am using the Import/Export wizard to import data from an ODBC data source. This can only be done from a query to specify the data to transfer.
When I try to create the tables, for the query, I am getting the following error:
Msg 2714, Level 16, State 4, Line 12
There is already an object named 'UserID' in the database.
Msg 1750, Level 16, State 0, Line 12
Could not create constraint. See previous errors.
I have duplicated this error with the following script:
USE [testing]
IF OBJECT_ID ('[testing].[dbo].[users1]', 'U') IS NOT NULL
DROP TABLE [testing].[dbo].[users1]
CREATE TABLE [testing].[dbo].[users1] (
[UserID] bigint NOT NULL,
[Name] nvarchar(25) NULL,
CONSTRAINT [UserID] PRIMARY KEY (UserID)
)
IF OBJECT_ID ('[testing].[dbo].[users2]', 'U') IS NOT NULL
DROP TABLE [testing].[dbo].[users2]
CREATE TABLE [testing].[dbo].[users2] (
[UserID] bigint NOT NULL,
[Name] nvarchar(25) NULL,
CONSTRAINT [UserID] PRIMARY KEY (UserID)
)
IF OBJECT_ID ('[testing].[dbo].[users3]', 'U') IS NOT NULL
DROP TABLE [testing].[dbo].[users3]
CREATE TABLE [testing].[dbo].[users3] (
[UserID] bigint NOT NULL,
[Name] nvarchar(25) NULL,
CONSTRAINT [UserID] PRIMARY KEY (UserID)
)
I have searched the "2714 duplicate error msg," but have found references to duplicate table names, rather than multiple field names or column name duplicate errors, within a database.
I think that the schema is only allowing a single UserID primary key.
How do I fix this?
TIA
View 4 Replies
View Related
May 7, 2006
This part is solved, please see my question at the bottom of this topic
Hi, my problem is the following, doesn't seem too hard but it's been puzzling me for hours now, and haven't solved it yet, so I need your help!
I'm creating an application to read from an existing db running on SQL-server 2000. This db contains the tables: tClient, tCommunication and tAddress. All of the three tables share a field called nClient which is the unique key in tClient, but each client can have multiple adresses and communication (phone/email etc), so several nClient can occur with the same value in tCommunication and tAddress.
I want to run a query which searches for a text (%blah%) in several fields in any of the tables, but I don't want the query to return the same client more than once, which now does happen if a client has more than one address. Help me! I want to to either return the clients with only the address that contained the %blah% or only the first address it finds for the client. Please help!
Simplified version of my query is like this:
SELECT sLastname, sStreet, sCommunication FROM tClient
LEFT JOIN tCommunication ON tClient.nClient = tCommunication.nClient
LEFT JOIN tAddress ON tClient.nClient = tAddress.nClient
WHERE tClient.sLastname LIKE '%blah%'
OR tClient.sCompanyName LIKE '%blah%'
OR tClient.sClient LIKE '%blah%'
OR tClient.sFirstname LIKE '%blah%'
OR tClient.mMemo LIKE '%blah%'
OR tCommunication.sCommunication LIKE '%blah%'
OR tAddress.sStreet LIKE '%blah%'
OR tAddress.sNumber LIKE '%blah%'
OR tAddress.sZip LIKE '%blah%'
OR tAddress.sCity LIKE '%blah%';
View 8 Replies
View Related
Mar 9, 2008
I have to write some reports for a database I am not familiar with. Is there a query I can use to find a table name if I know the field name?
example: Select table_name from database where field_name = 'my_field'
Mike
View 1 Replies
View Related
Dec 4, 2006
Derek writes "I'm totally new to SQL and need some help.
I need to write a query that will return rows where a specific field begins with a selected string.
Example:
My table is called rf_log
I want to see all rows where the PACKSLIP begins with 'ORD000888'
I tried:
Select *
from rf-log
where PACKSLIP like 'ORD000888'
This returns only rows where the packslip is = ORD000888
I need to see all rows where the packslip begins with ORD000888.
So packslip ORD000888-0, ORD000888-1 and ORD000888-2 should also be returned.
Many thanks."
View 11 Replies
View Related
Jan 12, 2000
Hi,
Does anybody know the SQL query to find the duplicate records?
Many Thanks in advance!
View 2 Replies
View Related
Feb 4, 2003
Hello board,
I was wondering if anyone can tell me an easy way to find duplicate records on sql. The thing is this, at work we have a database (table) which includes tracking numbers, I need a easy way to be able to search this table for duplicate tracking numbers and print them out. I currently access this table to edit some data by using the following path “Start > Programs > Microsoft SQL Server > Enterprise Manager” then work my down the tree to “Databases > Master > Tables” on tables I do a right click and “open table/query”. Any help would be most appreciated. Believe me I’m very “SQL illiterate”
Bill
:confused:
View 2 Replies
View Related
Mar 29, 2004
I have column A,B and C. I need a query to find the duplicates among these column.
Thanks,
Ravi
View 2 Replies
View Related
May 13, 1999
Hi,
As far as I know in SQL Server 6.5 there is no concept called rowid. How can I find duplicate records in a table and delete them.
Thanks,
Srini
View 2 Replies
View Related
May 28, 2008
I have written this script: What I want to do now is, with the new JOINED table find and display all the duplicates custID WHERE the price is either 100 OR 200. Can anyone help? I am very new to all this and can't see how to do it. Thanks!
SELECT
*
FROM
table_customer T1
INNER JOIN
table_itemsBought T2
ON
T2.custID = T1.custID
WHERE
price = '100'
OR price = '200';
View 2 Replies
View Related
Apr 17, 2014
I have this query that I have been using to find duplicate records works great except for now. The logic I am adding is pcs_rreas <> 'NG'. When I add this it does take out the NG, but it also excludes the reocords that have NULL data in this field. I don't want that to happen. How can I fix this? I tried adding (pcs_rreas <> 'NG' or pcs_rreas is null) but nothing is pulling now.
SELECT pcs_id1, pcs_rreas, pcs_lname, pcs_fname, pcs_minit, pcs_degree, pcs_xtyp, pcs_office, pcs_dba, pcs_dob, pcs_sex, pcs_eff, pcs_trm, pcs_spec1, pcs_spec2, pcs_spec3,
pcs_spec4, pcs_tax1, pcs_ssn, pcs_altid, pcs_upin, pcs_medic, pcs_mcaid, pcs_ecs, pcs_npi, pcs_status, pcs_dir, pcs_den, pcs_www, pcs_hold, pcs_email,
pcs_misc1, pcs_misc2, pcs_newdt, pcs_newby, pcs_chgdt, pcs_chgby, pcs_malp, pcs_pf, pcs_ctl, pcs_sys, pcs_pay, pcs_ann, pcs_bcert, pcs_pass, pcs_w9,
pcs_taxex, pcs_tax2, pcs_type, pcs_rreas, pcs_routo, pcs_rtime, pcs_rdate, pcs_force, pcs_flag, pcs_v419, pcs_bcity, pcs_bstate,
[code]...
View 3 Replies
View Related
Oct 5, 2007
Hello friends,
I have a one problem, i have a table in that some reocrds are duplicate.i want to find which records are duplicate.
for exp. my table is as follows
emp_id emp_name
1 aa
2 bb
3 cc
1 aa
3 cc
3 cc
and i want the result is like
emp_id emp_name
1 aa
1 aa
3 cc
3 cc
3 cc
View 6 Replies
View Related
Apr 30, 2004
I would like to locate duplicate rows within a specific table. This table has 12 diffrent rows.
BASENO - POSITION - SEQ - PROD -STYL - DESCR - FIELD01 THRU FIELD05 - VALUE01 THRU VALUE05 - FORMTYPE - ANSWER
I have been playing with the following query but can't seem to get it perfect to locate my dups within sql. Can someone help me with the querry?
I'm playing with the following querry.
SELECT
<list of all columns>
FROM
tablename
GROUP BY
<list of all columns>
HAVING
Count(*) > 1
Can somone possible input my column names into this querry that would possibly get it to locate my dups? I'm missing somehting and not sure what.
Thanks for any help
SQL Newbie
View 9 Replies
View Related
Oct 17, 2013
I'm using this to find duplicates where a person has the same email but varying firstname and lastnames:
select distinct t1.booking_id, t1.first_name, t1.last_name, t1.email_add, t1.booking_status_id
from [aren1002].[BOOKING]
as t1 inner join [aren1002].[BOOKING]
as t2
on t1.last_name=t2.last_name and t1.booking_id<>t2.booking_id
where t1.booking_status_id = 330
order by last_name asc
Sample data:
3927 Greg Smith greg@emailno1.com 303
5012 John Smith greg@emailno1.com 303
6233 John Smith greg@emailno1.com 303
4880 Dulcie Abuud dulcie@theiremail.com 303
However it is listing the non duplicate rows, For example: The record with Abuud as the last name, doesn't have any duplicates in the table, so I don't want it listed.
The data should be like this:
3927 Greg Smith greg@emailno1.com 303
5012 John Smith greg@emailno1.com 303
6233 John Smith greg@emailno1.com 303
View 4 Replies
View Related
Dec 10, 2013
Is there a way to find duplicate values and get the timediff from the start and end of each duplicate.
datetimeTagname value MachineValueTime Diff Minutes
12/9/13 8:55machine_1 14,423 Machine_1 14,423 5
12/9/13 9:00machine_1 14,423 Machine_1 14,428 9
12/9/13 9:05machine_1 14,428 Machine_1 42,743 4
12/9/13 9:10machine_1 14,428
12/9/13 9:14machine_1 14,428
12/9/13 11:32machine_1 42,743
12/9/13 11:36machine_1 42,743
View 6 Replies
View Related
Jul 28, 2014
How can I pull out duplicate records based on certain fields?
Table called Bank
I want to pull out records that have duplicate inv_no, cus_no, amount,ordernum
Not all the fields are the same in each record. But I want the records that have these fields that are the same.
View 1 Replies
View Related
Sep 6, 2007
In the FLAT FILE source, I have to find the duplicate rows based on the two fields say, "bill number" & "invoice date".
The rows within flat file has like "bill number" which is duplicated on the same "invoice date".
If duplicate rows found then move the duplicate rows into another Flat File.
If not found then move the rows into Sql Server Table.
Pls provide the solution. Thank you
View 9 Replies
View Related
Jun 11, 2015
Someone ran an update statement multiple times so their are multiple entries in the table. Â What is the quickest way to track down the multiple entries? Â I would only want to see where timein and timeoff exist in the table multiple times for the same id. Â So this would be a duplicate
EntryID -- ID Â -- timein -- timeoff
1487 Â 11 Â Â 2015-05-05 16:33:23 Â 2015-05-05 18:45:26
1623 Â 11 Â Â 2015-05-05 16:33:23 Â 2015-05-05 18:45:26
View 7 Replies
View Related
Oct 18, 2013
I've got 2 tables of towns. I'm using outer join because i need all the town from both tables. However I'm sometimes getting duplicates.
My query
select a.town, b.town
from a
outer join b on a.town = b.town
group by a.town, b.town
How to stop getting null values?
portsmouth null
portsmouth portsmouth
southampton southampton
null southampton
TownA null
null TownB
I'm looking for distinct values like this:
portsmouth portsmouth
southampton southampton
TownA null
null TownB
etc...
View 2 Replies
View Related
Oct 1, 2015
My data is like below:Â
Â
ClassId ClassName  StudentId  Subject    SubjectIdÂ
1        ESL       12        English     20Â
1        ESL       13        Science     30Â
1        ESL       12        Social      40Â
1        ESL       12        Maths       50Â
Â
Required output: parameters are Subject column valuesÂ
ClassId ClassName  TotalStudents  SubjectIds Â
Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 20, 40, 50Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30Â
Â
When one student takes multiple subjects then count student only once, so in the above data 12 is one student id takes multiple subjects so counted only once. TotalStudents value is 1
Â
I did write below query:Â
Â
Declare @subjectids stringÂ
set @subjectids = '20,30,40,50'Â
-- will split @subjectids and store in temp table   Â
select classname, classid, Count(Distinct StudentId)Â
from mytableÂ
where SubjectsIds in @subjectIdsÂ
group by ClassId, ClassName, SubjectId,Â
Â
but it gives me below output:Â Â Â
ClassId ClassName  TotalStudents  SubjectIdsÂ
Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 20Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 30Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 40Â
1Â Â Â Â Â Â Â ESLÂ Â Â Â Â Â Â Â 1Â Â Â Â Â Â Â Â Â Â Â Â Â Â 50Â
View 9 Replies
View Related
Jun 30, 2015
In my asp.net project there are about 100 drop down list.I created a table to store data for drop down list in which including [DropdownID],[Order Sequence] and [Description] three columns. The sample like below. Data was input manually by a user. How to code to find out duplicate [OrderSequence]?
DropdownID--OrderSequence--Description
1-------------0--------------AAA
1-------------1--------------BBB
2-------------0--------------YYY
2-------------1--------------XXX
2-------------2--------------QQQ 'DUPLICATE OrderSequence
2-------------2--------------WWW 'DUPLICATE OrderSequence
2-------------3--------------RRR
View 2 Replies
View Related
Jul 8, 2010
I'm doing a group by in an aggregate transformation. I have say 6 columns in the output and I'm grouping on all of them - how can I get duplicate rows in the output? If I do the same select and group by in SQL on the source data I don't get any duplicate rows. In fact out of 6000+ rows I only get 2 duplicates.
View 7 Replies
View Related
Sep 11, 2014
i have a table like below
create table staff_attendance
(
attendance_id int,
attendace_date datetime,
staff_id int,
working_year int,
hours int
)
values like
1 2014-06-30 00:00:00.0ST10121
2 2014-06-30 00:00:00.0ST10122
3 2014-06-30 00:00:00.0ST10122 ----same entry like previous one
4 2014-07-01 00:00:00.0ST10121
5 2014-07-01 00:00:00.0ST10122
6 2014-07-02 00:00:00.0ST10121
7 2014-07-02 00:00:00.0ST10122
8 2014-06-30 00:00:00.0ST10221
9 2014-06-30 00:00:00.0ST10222
10 2014-07-01 00:00:00.0ST1022 1
11 2014-07-01 00:00:00.0ST102 22
12 2014-07-02 00:00:00.0ST102 21
13 2014-07-02 00:00:00.0ST102 22
I Need to find the duplicate rows like same entries which is having more than 1 rows.... how do i find?
View 3 Replies
View Related
Apr 21, 2007
Hi,
in table aspnet_membership of ASPNETDB.mdb file, i saw a kind of duplicate field like "LoweredEmail" containing the same value as field "Email" (also in table users field "LoweredUsername" ..).
I guess it's for containing email in lowercase, but why those fields?
Thanks
Tartuffe
View 2 Replies
View Related
Feb 29, 2000
I have a field called RegId. RegId is of datatype NVARCHAR (20).
RegId
-----
12322
2122111
23423
etc
etc
I want to run a query to find out if there are duplicate entries in this field.
Any ideas on how I can achieve this?
Thanks in advance,
Anthony
View 1 Replies
View Related
Jan 9, 2004
I deliberately intend to add some duplicates to one of my tables. For eg
Job User IsAdmin
JobID 235User ID 1
JobID 235User ID 5
JobID 235User ID 9
JobID 235User ID 5
JobID 235User ID 2
JobID 235User ID 9
JobID 235User ID 10
JobID 235User ID 1
I know its bad practice to do such a thing but there is a genuine reason. What I need to do is to be able to have a SQL statement that appends true to the IsAdmin field whenever it encounters the next UserID thats happens to be a duplicate. Hence the above would look like:
Job User IsAdmin
JobID 235User ID 1
JobID 235User ID 5
JobID 235User ID 9
JobID 235User ID 5 True
JobID 235User ID 2
JobID 235User ID 9 True
JobID 235User ID 10
JobID 235User ID 1 True
Thanks
View 1 Replies
View Related
Aug 20, 2007
Hi,
I have several row records in which
name email
--------- ---------
name1 abc@abc.com
name2 abc@abc.com
name3 abc@abc.com
name4 123@123.com
name6 123@123.com
How should I write the query which can return records with unique email address with any name attached?
The expected record set will be
name email
--------- ---------
<any> abc@abc.com
<any> 123@123.com
I was thinking to select the distinct of email and just stuck at here. I encountered this problem few times and still cannot solve it. Any help will great appreciated. Thanks in advance.
View 7 Replies
View Related
Jul 10, 2007
Hi everybody couldn't get through with saving my data on the table with two primary keys...
my table structure is this
pubidintUnchecked (primary key)
pubchar(1)Unchecked
publchar(1)Unchecked
pubcodechar(2)Unchecked (primary key)
a sample data is here
pubid pub publ pubcode
1 a b ab
1 b b bb
2 a b ab
2 b b bb
when i save this table modifying the pubid and pubcode as primary keys the following error displays...
Unable to create index 'PK_PUBS3'.
CREATE UNIQUE INDEX terminated because a duplicate key was found for index ID 1. Most significant primary key is '51'.
Could not create constraint. See previous errors.
The statement has been terminated.
what i understand is that on the primary key duplicates are not allowed how could i allow it?
thanks
View 7 Replies
View Related
Mar 15, 2004
I am looking for some help in pulling certain people out of a table
This is the basic setup of my table
PK ID NAME MEETING
1 11111 Joe CLASS98
2 22222 Jane CLASS98
3 33333 Bob CLASS98
4 11111 Joe CLASS04
5 22222 Jane CLASS04
6 44444 Sally CLASS04
What I am wanting to do is Select only the people who attended CLASS98 but didn't attend CLASS04
I could just remove the CLASS04 people by sorting if I could just return the rows that do not duplicate the ID and meeting is either CLASS04 or CLASS98
Either way would get me to my goal.
Any help is GREATLY appreciated.
Thanks
Jimmyjoe
View 11 Replies
View Related