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 582967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:44:34+00:00 2026-05-13T14:44:34+00:00

I have two tables: CREATE TABLE #HOURS (DAY INTEGER, HOUR INTEGER) CREATE TABLE #PERSONS

  • 0

I have two tables:

CREATE TABLE #HOURS
(DAY INTEGER,

HOUR INTEGER)

CREATE TABLE #PERSONS
(DAY INTEGER, HOUR INTEGER,
 Name NVARCHAR(50))
GO

INSERT #HOURS VALUES (1, 5)
INSERT #HOURS VALUES (1, 6)
INSERT #HOURS VALUES (1, 8)
INSERT #HOURS VALUES (1, 10)
INSERT #HOURS VALUES (1, 14)
INSERT #HOURS VALUES (1, 15)
INSERT #HOURS VALUES (1, 16)
INSERT #HOURS VALUES (1, 17)
INSERT #HOURS VALUES (1, 18)


INSERT #PERSONS VALUES (1, 5, 'Steve')
INSERT #PERSONS VALUES (1, 6, 'Steve')
INSERT #PERSONS VALUES (1, 7, 'Steve')
INSERT #PERSONS VALUES (1, 8, 'Steve')
INSERT #PERSONS VALUES (1, 10, 'Steve')
INSERT #PERSONS VALUES (1, 14, 'Steve')
INSERT #PERSONS VALUES (1, 15, 'Steve')
INSERT #PERSONS VALUES (1, 16, 'Steve')
INSERT #PERSONS VALUES (1, 17, 'Steve')

INSERT #PERSONS VALUES (1, 10, 'Jim')
INSERT #PERSONS VALUES (1, 11, 'Jim')
INSERT #PERSONS VALUES (1, 12, 'Jim')
INSERT #PERSONS VALUES (1, 13, 'Jim')

GO

Hours shows the work hours and #Persons shows the persons that entered the system on hourly base.
I’d like to find the persons whose work hours matches the hours table. But he or she can skip two work hours.

I’ve tried this:

select t.Day, sum(t.Nulls)
from  
(select h.Hour, h.Day
      , Case   
            WHEN p.Hour is null Then 1 ELSE 0 END Nulls
from #HOURS h  
left join #PERSONS P on h.Hour = p.Hour AND h.Day = p.Day) t  
group by t.Day
HAVING sum(t.Nulls) < 2

But this only works when there is not different persons on the same day 😉

Any suggestions?

  • 1 1 Answer
  • 1 View
  • 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-13T14:44:34+00:00Added an answer on May 13, 2026 at 2:44 pm

    What I think you mean is that for each combination of day and person, you want to return it if the number of valid hours that that person worked on that day is within 2 hours of the number of valid hours that exist for that day. If so, this should do the trick:

    SELECT
        Day
        , Name
        , HoursWorked
        , HoursInDay
    FROM (
        SELECT
            p.Day
            , p.Name
            , COUNT(*) HoursWorked
            , (SELECT COUNT(*) FROM #Hours H2 WHERE H2.Day = P.Day) HoursInDay
        FROM
            #Persons P INNER JOIN #Hours H 
                ON P.Day = H.Day And P.Hour = H.Hour
        GROUP BY
            p.Day, p.Name
         ) Data
    WHERE
        HoursWorked + 2 >= HoursInDay
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: CREATE TABLE Table1 ( ID VARCHAR(15), Value INT ); INSERT
I have two tables create table tblchildinfo (id int, name varchar(50), pickuppointid int, dropdownpointid
I have two tables: create table Number( num number(5)); create table Entry(id number(3), name
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have two tables: codes_tags and popular_tags. codes_tags CREATE TABLE `codes_tags` ( `code_id` int(11)
I have two tables: entitytype and project . Here are the create table statements:
I have two tables A and B as defined bellow. create table A (
I am using luasql. I have two tables of this type: IPINFO CREATE TABLE
I have created two tables & inserted values as shown below . Table 1

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.