SQL Query to find missing entries

I have a table called Diary with columns Entry_Date, UserID and so on
Users are required to make an entry each day.
I would like to find out dates that Users haven't made entries.
I'm not sure how to do this but would start by selecting the Distinct Dates and the Distinct Users
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
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.

PortletPaulfreelancerCommented:
you need a reliable table of dates, which could be a generated using a recursive CTE

getting a distinct set of dates from that table *might* work, but it *might* not
-- establish date range
DECLARE @fromdate datetime
      , @todate datetime
SELECT
        @fromdate = '20130901' -- or -- '2013-09-01'
      , @todate   = '20131001' -- or -- '2013-10-01'

;WITH
        DateCTE (theDate)
        AS (
                SELECT
                        @fromdate AS theDate
                UNION ALL
                        SELECT
                                DATEADD(DAY, 1, theDate)
                        FROM DateCTE
                        WHERE theDate < dateadd(day,-1,@todate)
                )

-- use it here
SELECT
        *
FROM DateCTE

-- left join yourtable on DateCTE.theDate = ???

OPTION (MAXRECURSION 1000)

Open in new window

0
Barry CunneyCommented:
WITH
  sequenced AS
(
  SELECT
    ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY EntryDate) AS SequenceID,
    *
  FROM
    Diary
)
SELECT
  *
FROM
  sequenced    AS a
INNER JOIN
  sequenced    AS b
    ON  a.userID    = b.userid
    AND a.SequenceID = b.SequenceID - 1
WHERE
  b.Entry_Date <> DATEADD(DAY, -1, a.Entry_Date)
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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
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

From novice to tech pro — start learning today.