• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 56
  • Last Modified:

Sql Server query to compare rows

Hi ,
   I have a table for Documents with following fields
      DocumentNo      
       DocRevision
      Location
      ..............      There are more fields
 
Following is the sample data .
      Doc1, Rev1, Loc1
      Doc1,Rev1, Loc1
     Doc1,Rev1,Loc2
     Doc2,Rev1,Loc2
     Doc2,Rev1,Loc2

   For Given DocumentNo and DocRevision , Location has to be same . In this case Doc1,Rev1 has wrong data .

    I am looking for a Sql server query that can bring   the  wrong data ( In the sample, the query should bring all the three rows of Doc1,Rev1)
0
Sam OZ
Asked:
Sam OZ
  • 2
1 Solution
 
Ryan ChongCommented:
try something like this:

declare @Documents table
(
	DocumentNo varchar(10),
	DocRevision varchar(10),
	Location varchar(30)
)
insert into @Documents
values
('Doc1', 'Rev1', 'Loc1'),
('Doc1', 'Rev1', 'Loc1'),
('Doc1', 'Rev1', 'Loc2'),
('Doc2', 'Rev1', 'Loc2'),
('Doc2', 'Rev1', 'Loc2');

with cte as
(
	select DocumentNo, DocRevision, Location
	from @Documents
	group by DocumentNo, DocRevision, Location
), cte2 as
(
	select DocumentNo, DocRevision
	from cte
	group by DocumentNo, DocRevision
	having count(*) > 1
)
select a.*
from @Documents a inner join cte2 b
on a.DocumentNo = b.DocumentNo and a.DocRevision = b.DocRevision

Open in new window

0
 
Sam OZAuthor Commented:
Thanks Chong . My apologies. There is some difference in the DB structure
     Documents  table
         DocumentNo      
        DocRevision
        Location
         ItemNo

  Items Table
       ItemName
       ItemNo

        ItemNo  joins the two tables

      For a given  ItemName,  DocRevision  the Location should be same

      I need to get entries where there are same ItemName ,Docrevision combination  but with different location
0
 
Ryan ChongCommented:
without your sample data it could only based on my imagination.

try this and see if this provide the expected output?

declare @Documents table
(
	DocumentNo varchar(10),
	DocRevision varchar(10),
	Location varchar(30),
	ItemNo varchar(20)
)
insert into @Documents
values
('Doc1', 'Rev1', 'Loc1', '001'),
('Doc1', 'Rev1', 'Loc1', '001'),
('Doc1', 'Rev1', 'Loc2', '001'),
('Doc2', 'Rev1', 'Loc2', '002'),
('Doc2', 'Rev1', 'Loc2', '002');

declare @Items table
(
	ItemName varchar(30),
	ItemNo varchar(20)
)
insert into @Items
values
('Item Name 1', '001'),
('Item Name 2', '002');

with cte as
(
	select a.DocumentNo, a.DocRevision, a.Location, b.ItemName
	from @Documents a inner join @Items b
	on a.ItemNo = b.ItemNo
	group by a.DocumentNo, a.DocRevision, a.Location, b.ItemName
), cte2 as
(
	select ItemName, DocRevision
	from cte
	group by ItemName, DocRevision
	having count(*) > 1
)
select a.*
from cte a inner join cte2 b
on a.ItemName = b.ItemName and a.DocRevision = b.DocRevision

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now