SQL join queries

Hi,

i need help writting a sql query that will select rows only when more that one occurance of the value in a column occurs. in my case, i need to select rows when there is more than one occurance of the same tracking number.

So if i had a small database with a table called billing with 6 rows and the value of the column "tracking number" was as follows (1, 2, 3, 2, 3, 3), i would want the query to return 5 rows - 2,3,2,3,3.

Additionally, i need another query that does just the opposite - returns 1 row - 1.

I've tried several attemplts at inner joins without much luck.

Thanks,
John
jrattingerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sventhanCommented:

select x.*
(
select c.*, rn = row_number() over (partition by tracking_numer)
from billing
) x
where rn > 1

select x.*
(
select c.*, rn = row_number() over (partition by tracking_numer)
from billing
) x
where rn =1
0
HainKurtSr. System AnalystCommented:
here:

select * from myTable
where id in (select id from myTable group by id having count(1)>1)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rajkumar GsSoftware EngineerCommented:
Here I demonstrated with temporary tables and your sample data. This contains the two queries
CREATE TABLE #table
(
	TrackingNo INT
)

INSERT INTO #table
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 3


-- Query 1
SELECT * FROM #table WHERE TrackingNo IN 
(
SELECT TrackingNo  FROM #table
GROUP BY TrackingNo
HAVING COUNT(TrackingNo) > 1
)

-- Query 2
SELECT * FROM #table WHERE TrackingNo IN 
(
SELECT TrackingNo  FROM #table
GROUP BY TrackingNo
HAVING COUNT(TrackingNo) = 1
)

DROP TABLE #table

Open in new window


Raj
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HainKurtSr. System AnalystCommented:
my demo :)


with myTable as(
select 1 id union all
select 2 union all 
select 3 union all
select 2 union all
select 3 union all
select 3
)
select * from myTable 
where id in (select id from myTable group by id having count(1)>1) 

id
2
3
2
3
3

Open in new window

0
Rajkumar GsSoftware EngineerCommented:
:)

Hainkurt, I didn't see your query as it was not posted when I start tying.

Raj
0
jrattingerAuthor Commented:
Awesome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

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.