Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Selecting Unique record from a table


Selecting Unique record from a table

Author
Message
rajborntodare
rajborntodare
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 510
Hi I have a table (Billing Table) having multiple records for
people having one record per person per each month.

how to get a list of the guys having record just for one month (say feb)
and doesn't have any other months.

please give an advise..
DataGuy
DataGuy
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 226
declare @StartDt DATE
declare @EndDt DATE
SET @StartDt = '2/1/2014'
SET @EndDt = '2/28/2014'

SELECT DISTINCT YOURCUSTOMERID
,YOURCUSTOMERNAME

FROM YOURTABLE
WHERE YOURDATECOLUMN BETWEEN @StartDt AND @EndDt


Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14984 Visits: 39007
rajborntodare (2/10/2014)
Hi I have a table (Billing Table) having multiple records for
people having one record per person per each month.

how to get a list of the guys having record just for one month (say feb)
and doesn't have any other months.

please give an advise..



i think you also need to add a test for the other months with NOT EXISTS:


-- Created By: Lowell Izaguirre
-- Create Date: |CurrentDate|
-- Description:
-- Modified By: Lowell Izaguirre
-- Modified Date: |CurrentDate|
-- Description:

declare @StartDt DATE
declare @EndDt DATE
SET @StartDt = '2/1/2014'
SET @EndDt = '2/28/2014'

SELECT DISTINCT T1.YOURCUSTOMERID
,T1.YOURCUSTOMERNAME

FROM YOURTABLE T1
WHERE T1.YOURDATECOLUMN BETWEEN @StartDt AND @EndDt
AND NOT EXISTS (SELECT 1
FROM YOURTABLE T2
WHERE T1.YOURCUSTOMERID = T2.YOURCUSTOMERID
AND (T2.YOURDATECOLUMN < @StartDt OR T2.YOURDATECOLUMN > @EndDt )
)



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

rajborntodare
rajborntodare
SSC-Enthusiastic
SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)SSC-Enthusiastic (111 reputation)

Group: General Forum Members
Points: 111 Visits: 510
Appreciate your help

But their is another problem
where there can be also a customer who is having bill in both jan, feb may be all months

but I want them who have only in jan
DataGuy
DataGuy
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 226
Thats where the NOT EXISTS comes into play which Lowell posted. If any of your customers have records outside the range they will be elminated from the results.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search