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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:46:11+00:00 2026-06-01T16:46:11+00:00

I’ve been trying to write this query but can’t seem to get it right

  • 0

I’ve been trying to write this query but can’t seem to get it right for some reason or another..

What I need to do is:

Change the status of a question to ‘closed’ if there has not been an update associated with this question inserted into the qUpdateTable in the last 24 hours.

I only want it to be closed if a staff member has replied to it at least once.

You can determine if a staff member or a user has replied to the question by checking the qUpdateTable and seeing if a the StaffID field is empty or has a value for that particular tickets updates. If there is a staffID then it has been updated by a staff member, however if it does not then the qUpdate was done by a user.

Essentialy the way this works is a user posts a question by inserting into the Question table, and replies are made by inserting into the qUpdate table and linked to the original question using the foreign key – “QuestionID”.

The tables:

CREATE TABLE Staff
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(40) NOT NULL
);

CREATE TABLE Customer
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(40) NOT NULL,
    Email       VARCHAR(40) NOT NULL
);

CREATE TABLE Product
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name    TEXT NOT NULL
);

CREATE TABLE Question
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Problem VARCHAR(1000),
    Status  VARCHAR(20) NOT NULL DEFAULT 'open', 
    Priority    INTEGER NOT NULL,
    LoggedTime  TIMESTAMP NOT NULL,
    CustomerID  INTEGER NOT NULL,
    ProductID   INTEGER NOT NULL,
    FOREIGN KEY (ProductID) REFERENCES Product(ID),
    FOREIGN KEY (CustomerID) REFERENCES Customer(ID),
    CHECK (Status IN ('open','closed') AND Priority IN (1,2,3))

);

CREATE TABLE qUpdate
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Message VARCHAR(1000) NOT NULL,
    UpdateTime  TIMESTAMP NOT NULL,
    QuestionID  INTEGER NOT NULL,
    StaffID INTEGER,
    FOREIGN KEY (StaffID) REFERENCES Staff(ID),
    FOREIGN KEY (QuestionID) REFERENCES Question(ID)
);

Some sample inserts:

INSERT INTO Customer (ID, Name, Email) VALUES (1, 'testname1', 'testemail1');

INSERT INTO Customer (ID, Name, Email) VALUES (2, 'testname2', 'testemail2');

INSERT INTO Staff (ID, Name) VALUES (1, 'Don Keigh');

INSERT INTO Product (ID, Name) VALUES (1, 'Xbox');

INSERT INTO Question (ID, Problem, Status, Priority, LoggedTime, CustomerID, ProductID) 
VALUES  (1, 'testproblem1', 'open', 3, '2012-04-14 09:30', 2, 1);

INSERT INTO Question (ID, Problem, Status, Priority, LoggedTime, CustomerID, ProductID) 
VALUES  (2, 'testproblem2', 'open', 3, '2012-04-14 09:30', 2, 1);

INSERT INTO qUpdate (ID, Message, UpdateTime, StaffID, QuestionID) VALUES (2, 'testmessage1','2012-07-12 14:27', 1, 1);

INSERT INTO qUpdate (ID, Message, UpdateTime, QuestionID) VALUES (3, 'testmessage1','2012-06-18 19:42', 2);

What I’ve done so far (which obviously doesn’t work)

UPDATE Question
SET Status = 'closed' 
WHERE EXISTS
(SELECT qUpdate.QuestionID  
MAX(qUpdate.UpdateTime - Now() = INTERVAL '1 day') FROM qUpdate
LEFT JOIN Question ON qUpdate.QuestionID = Question.ID
WHERE qUpdate.StaffID IS NOT NULL);

I realise my explanation may be a bit confusing so if you need more info post and I’ll reply ASAP

  • 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-06-01T16:46:12+00:00Added an answer on June 1, 2026 at 4:46 pm
    UPDATE Question
    SET Status = 'closed' 
    where
    -- this clause asserts there's at least one staff answer
    exists (
      select null from qUpdate
      where qUpdate.QuestionID = Question.ID
      and StaffID is not null
    )
    -- this clause asserts there's been no update in the last 24 hours
    and not exists (
      select null from qUpdate
      where qUpdate.QuestionID = Question.ID
      and qUpdate.UpdateTime > (now() - interval '24 hours') 
    )
    and Status = 'open';
    

    You’ll almost certainly want an index on qUpdate(QuestionID) or possibly qUpdate(QuestionID,UpdateTime) or qUpdate(QuestionID,StaffID) to get good performance on the subselects in the exists() tests.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.