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

  • Home
  • SEARCH
  • 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 8415309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:24:32+00:00 2026-06-10T01:24:32+00:00

CREATE TABLE IF NOT EXISTS `projects` ( `idproject` int(10) unsigned NOT NULL AUTO_INCREMENT, `name`

  • 0
CREATE TABLE IF NOT EXISTS `projects` (
 `idproject` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `date` datetime NOT NULL,
  `status` enum('new','active','closed') NOT NULL,
  `priority` enum('low','medium','high') NOT NULL,
  PRIMARY KEY (`idproject`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=20

Here are some data:

 INSERT INTO `projects` (`idproject`, `name`, `date`, `status`, `priority`) VALUES
(1, 'iCompany', '2011-03-23 11:41:44', 'new', 'medium'),
(2, 'John Doe & Co.', '2011-04-09 14:38:04', 'closed', 'low'),
(3, 'ACME, Inc.', '2011-05-21 11:43:11', 'active', 'high'),
(4, 'John Doe & Co.', '2011-03-28 15:19:45', 'active', 'low'),
(5, 'John Doe & Co.', '2011-03-08 15:16:32', 'new', 'low'),
(6, 'ACME, Inc.', '2011-04-05 20:58:42', 'active', 'low'),
(7, 'Mega Corp', '2011-04-21 08:08:53', 'new', 'low'),
(8, 'iCompany', '2011-04-17 08:40:36', 'active', 'medium'),
(9, 'iCompany', '2011-05-18 14:36:48', 'active', 'low'),
(10, 'John Doe & Co.', '2011-04-18 19:08:25', 'new', 'medium'),
(11, 'ACME, Inc.', '2011-05-19 13:11:04', 'active', 'low'),
(12, 'Foo Bars', '2011-03-03 17:19:29', 'new', 'high'),
(13, 'ACME, Inc.', '2011-04-23 20:42:33', 'active', 'medium'),
(14, 'Foo Bars', '2011-05-13 09:18:15', 'active', 'medium'),
(15, 'ACME, Inc.', '2011-03-20 14:37:18', 'new', 'low'),
(16, 'Foo Bars', '2011-04-18 13:46:23', 'active', 'high'),
(17, 'iCompany', '2011-05-31 07:13:32', 'closed', 'low'),
(18, 'Foo Bars', '2011-05-31 15:43:39', 'active', 'low'),
(19, 'John Doe & Co.', '2011-05-28 11:28:32', 'active', 'medium')

I’d like to have the list of all projects:
– with their latest (chronologically) status and priority,
– with the number of days between the first and latest entry (0 if there is
only one entry), you may ignore the hours,
– sorted by by priority (‘high’ first), then by name,
– without the projects where the latest status is ‘closed’ (omitted from
result).

Output should be:

+---------------+-----------+---------------+----------------+
¦name           ¦total_days ¦latest_status  ¦latest_priority ¦
+---------------+-----------+---------------+----------------+
¦ACME, Inc.     ¦62         ¦active         ¦high            ¦
¦John Doe & Co. ¦81         ¦active         ¦medium          ¦
¦Foo Bars       ¦89         ¦active         ¦low             ¦
¦Mega Corp      ¦0          ¦new            ¦low             ¦
+---------------+-----------+---------------+----------------+

So far i got to write this:

 SELECT name,status FROM projects  group by name order by priority desc,name

please help?

  • 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-10T01:24:34+00:00Added an answer on June 10, 2026 at 1:24 am
    SELECT *
    FROM (
      SELECT name, 
        DATEDIFF(MAX(date), MIN(date)) total_days,
        (SELECT tt.status FROM projects tt 
         WHERE t.name = tt.name AND tt.date = MAX(t.DATE)) latest_status,
        (SELECT tt.priority FROM projects tt 
         WHERE t.name = tt.name AND tt.date = MAX(t.DATE)) latest_priority
      FROM projects t
      GROUP BY name 
      ) t
    WHERE latest_status != 'closed'
    ORDER BY (CASE latest_priority
              WHEN 'high' THEN 0
              WHEN 'medium' THEN 1
              WHEN 'low' THEN 2 
              END), name;
    
    • total days: take the DATEDIFF of the MAX and MIN date, which will give you the number of days in between;
    • latest status: fetch the status for which the row’s date is equal to the MAX date;
    • latest priority: fetch the priorityfor which the row’s date is equal to the MAX date;
    • order by: translate each priority string to a numerical value and order by it.

    Here’s an sqlfiddle.

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

Sidebar

Related Questions

SQL STRUCTURE CREATE TABLE IF NOT EXISTS `map` ( `id` int(11) NOT NULL AUTO_INCREMENT,
Using the command: CREATE TABLE IF NOT EXISTS `test`.`t1` ( `col` VARCHAR(16) NOT NULL
I have a code: CREATE TABLE IF NOT EXISTS Person ( name varchar(24) ...
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
Given the table: CREATE TABLE IF NOT EXISTS `users` ( `id` bigint(20) NOT NULL
I have this table: CREATE TABLE IF NOT EXISTS `produtos` ( `id` int(11) NOT
I have the following table: CREATE TABLE IF NOT EXISTS `customer_list` ( `id` INT
i have 2 tables CREATE TABLE if not exists tag_name( + tagid INTEGER PRIMARY
I've got a table like so: CREATE TABLE IF NOT EXISTS grades(_id, timestamp, extra);
I have 2 tables in my mysql database: CREATE TABLE IF NOT EXISTS `RECIPES`

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.