Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:55:09+00:00 2026-05-23T13:55:09+00:00

I have a table that holds availability status for workers. Here is the structure:

  • 0

I have a table that holds availability status for workers. Here is the structure:

CREATE TABLE [dbo].[Availability]
(
    [OID]                  BIGINT        IDENTITY (1, 1) NOT NULL,
    [LocumID]              BIGINT        NOT NULL,
    [AvailableDate]        SMALLDATETIME NOT NULL,
    [AvailabilityStatusID] INT           NOT NULL,
    [LastModifiedAt]       TIMESTAMP     NOT NULL,
    CONSTRAINT [PK_Availability] PRIMARY KEY CLUSTERED ([OID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

And here is a result:

OID                  LocumID              AvailableDate           AvailabilityStatusID LastModifiedAt
-------------------- -------------------- ----------------------- -------------------- ------------------
1                    1                    2009-03-02 00:00:00     1                    0x0000000000201A8C
2                    2                    2009-03-04 00:00:00     1                    0x0000000000201A8D
3                    1                    2009-03-05 00:00:00     1                    0x0000000000201A8E
4                    1                    2009-03-06 00:00:00     1                    0x0000000000201A8F
5                    2                    2009-03-07 00:00:00     1                    0x0000000000201A90
6                    7                    2009-03-09 00:00:00     1                    0x0000000000201A91
7                    1                    2009-03-11 00:00:00     1                    0x0000000000201A92
8                    1                    2009-03-12 00:00:00     2                    0x0000000000201A93
9                    1                    2009-03-14 00:00:00     1                    0x0000000000201A94
10                   1                    2009-03-16 00:00:00     1                    0x0000000000201A95

Now, the table has over 3mil record and I noticed that there are inconsistencies in my data. I need to somehow find rows where for any [AvailableDate], the [LocumID] (regardless of how many,) must be unique. So, basically, a worker can have one of these [AvailabilityStatusID] = 1, 2, 3, or 4 on one date. However, in this table, there are errors where a worker is entered twice or more against a [AvailableDate] with same [AvailabilityStatusID] or different [AvailabilityStatusID]

How can I detect these records?

Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:55:10+00:00Added an answer on May 23, 2026 at 1:55 pm
    WITH x AS 
    (
      SELECT LocumID, dt = AvailableDate
         FROM dbo.Availability
         GROUP BY LocumID, AvailableDate 
         HAVING COUNT(*) > 1
    )
    SELECT a.OID, a.LocumID, a.AvailableDate, 
        a.AvailabilityStatusID, a.LastModifiedAt
     FROM x
     INNER JOIN dbo.Availability AS a
     ON x.LocumID = a.LocumID
     AND x.dt = a.AvailableDate
     ORDER BY a.LocumID, a.AvailableDate;
    

    Once you clean this data up (not sure what your rule will be regarding which rows to keep), you should consider a unique constraint on (LocumID, AvailableDate). Here is how you would create the constraint (though you will not be able to create it until you have removed the duplicates):

    ALTER TABLE dbo.Availability 
        ADD CONSTRAINT uq_l_ad 
        UNIQUE (LocumID, AvailableDate);
    

    Of course now you will have new errors returned to your application (Msg 2627), since your current code clearly doesn’t already check if a LocumID/AvailabilityDate combination already exists before adding a new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that holds information about different events, for example CREATE TABLE
I have a table that holds information about cities in a game, you can
I have a table that holds only two columns - a ListID and PersonID.
Given that I have a table that holds vehicle information and one of those
I have a MySQL table that holds many entries with repeated IDs (for various
I have a table (table variable in-fact) that holds several thousand (50k approx) rows
Lets suppose that I have a Category table with a column that holds the
I have a table that holds nested categories. I want to avoid duplicate names
I have a table that holds agreement information. It works well for 95% of
I have a database table that holds information for received files. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.