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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:26:42+00:00 2026-06-07T08:26:42+00:00

Im trying to do a query with the following. I have simplified the query

  • 0

Im trying to do a query with the following. I have simplified the query as much as possible for purposes of the question. It is basically for an email schedular app I am writing.

Two tables. An email can belong to a particular schedular. A schedular comprises of several emails. Each email that has been sent is marked with a specific step number. step1, step2, step3, step4, step5

email_schedular (id, datecreated, recipient)
email(id, email_schedular_id, text, subject, sent, stepNumber)

Now what im trying to do is determine in a report if a step has been sent or not. I have had a go at it but im not sure if im going about it the right way?

SELECT
  ed.id,
  ed.datecreated
  ed.recipient
  if(e.stepNumber= 1, 'Y', 'N')  AS step_1_sent,
  if(e.stepNumber= 2, 'Y', 'N')  AS step_2_sent,
  if(e.stepNumber= 3, 'Y', 'N')  AS step_3_sent,
  if(e.stepNumber= 4, 'Y', 'N')  AS step_4_sent
FROM 
  email_echedular ed
JOIN email e ON (e.email_echedular_id = ed.id)
GROUP BY ed.id
  • 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-07T08:26:44+00:00Added an answer on June 7, 2026 at 8:26 am

    Instead of using an N for not sent, use a NULL. Then when you group them, you can take the MAX() value for each and eliminate the NULLs down to one row. The end result should be one row per ed.id with a Yes for sent steps and NULL for unsent steps.

    SELECT
      ed.id,
      ed.datecreated
      ed.recipient
      MAX(if(e.stepNumber= 1, 'Y', NULL)) AS step_1_sent,
      MAX(if(e.stepNumber= 2, 'Y', NULL)) AS step_2_sent,
      MAX(if(e.stepNumber= 3, 'Y', NULL)) AS step_3_sent,
      MAX(if(e.stepNumber= 4, 'Y', NULL)) AS step_4_sent
    FROM 
      email_schedular ed
      LEFT JOIN email e ON (e.email_schedular_id = ed.id)
    GROUP BY ed.id
    

    If you really don’t want a NULL for the non-sent columns, you can wrap those in COALESCE() to replace the NULL with N:

    SELECT
      ed.id,
      ed.datecreated
      ed.recipient
      COALESCE(MAX(if(e.stepNumber= 1, 'Y', NULL)), 'N') AS step_1_sent,
      COALESCE(MAX(if(e.stepNumber= 2, 'Y', NULL)), 'N') AS step_2_sent,
      COALESCE(MAX(if(e.stepNumber= 3, 'Y', NULL)), 'N') AS step_3_sent,
      COALESCE(MAX(if(e.stepNumber= 4, 'Y', NULL)), 'N') AS step_4_sent
    FROM 
      email_schedular ed
      LEFT JOIN email e ON (e.email_schedular_id = ed.id)
    GROUP BY ed.id
    

    Edit Changed to LEFT JOIN and fixed join condition…

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

Sidebar

Related Questions

I am trying to make a sql query following these rules: basically i have
I'm trying to figure out the best query to accomplish the following. I have
Im trying to query the Netflix OData feed. I have the following query that
I have the following XML which I am trying to query with XDocument: <E2ETraceEvent
I have (simplified for this question) a table 'TAB' with two columns 'id' (integer
I have the following query, and I am trying to produce four columns, the
I have the following query and I'm trying to update table1 with the Total
I have following classes and db schema. I am trying to query this data
I am trying to sort query result into an array. I have following query.
I have the following two (simplified for the sake of example) tables in my

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.