Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Select using "Distinct"

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-06-11 : 11:17:34
I'm having problems selecting distinct records:

SELECT                 distinct(dbo.Item.CompanyItemId), dbo.Job.CompanyJobId AS JobID,  dbo.Batch.ReportDate AS BatchDate,
dbo.JobNoteEvent.Note as Notes,dbo.Item.UnitOfMeasure AS UOM, dbo.SourceType.CompanySourceTypeId,
dbo.Product.CompanyProductId
FROM dbo.Batch INNER JOIN
dbo.Event ON dbo.Batch.BatchGuid = dbo.Event.BatchGuid INNER JOIN
dbo.Job ON dbo.Event.JobGuid = dbo.Job.JobGuid INNER JOIN
dbo.ProductionEvent ON dbo.Event.EventGuid = dbo.ProductionEvent.EventGuid INNER JOIN
dbo.Item ON dbo.Event.ItemGuid = dbo.Item.ItemGuid INNER JOIN
dbo.Source ON dbo.ProductionEvent.SourceGuid = dbo.Source.SourceGuid INNER JOIN
dbo.SourceType ON dbo.Source.SourceTypeGuid = dbo.SourceType.SourceTypeGuid LEFT OUTER JOIN
dbo.Product ON dbo.ProductionEvent.ProductGuid = dbo.Product.ProductGuid left outer join
dbo.JobNoteEvent on Event.EventGuid = dbo.JobNoteEvent.EventGuid
WHERE dbo.job.CompanyJobId = 3505048 and dbo.SourceType.CompanySourceTypeId = 'PR'
ORDER BY dbo.Job.CompanyJobId, dbo.Item.CompanyItemId


Result:

04001 3505048 2008-04-13 NULL SY PR NULL
04101 3505048 2008-04-08 NULL SY PR NULL
04101 3505048 2008-04-10 NULL SY PR NULL
05001 3505048 2008-04-07 NULL TN PR NULL
05001 3505048 2008-04-08 NULL TN PR NULL
05001 3505048 2008-04-09 NULL TN PR NULL
05001 3505048 2008-04-10 NULL TN PR NULL
05001 3505048 2008-04-13 NULL TN PR NULL
05001 3505048 2008-04-23 NULL TN PR NULL
05001 3505048 2008-04-24 NULL TN PR NULL
05001 3505048 2008-04-27 NULL TN PR NULL
05001 3505048 2008-06-05 NULL TN PR NULL
05001 3505048 2008-06-08 NULL TN PR NULL

My result should only be 3 records (column 1) values 04001, 04101 and 05001.

Thank you.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-11 : 11:19:38
So you want to retun only column1?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 11:21:26
Or if you want to return all columns, you need to specify what values should you be getting for other fields while we take distinct value for column1. Illustrate it based on your sample data.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-06-11 : 11:36:11
Reading both of your questions it got clear to me. Changing my "Select" to this:

SELECT distinct(dbo.Item.CompanyItemId), dbo.Job.CompanyJobId AS JobID  


gave me this result:

04001 3505048
04101 3505048
05001 3505048

I should be ok. Thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 12:35:13
quote:
Originally posted by snufse

Reading both of your questions it got clear to me. Changing my "Select" to this:

SELECT distinct(dbo.Item.CompanyItemId), dbo.Job.CompanyJobId AS JobID  


gave me this result:

04001 3505048
04101 3505048
05001 3505048

I should be ok. Thank you.



Nope. it wont be. You will still get duplicates. distinct will only ensure the entire combination of columns is unique not dbo.Item.CompanyItemId alone.
Go to Top of Page
   

- Advertisement -