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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:46:22+00:00 2026-06-09T12:46:22+00:00

I have a query: SELECT I.Id , CAST(SUBSTRING_INDEX(GROUP_CONCAT(I.StatusId ORDER BY I.TransactionId DESC), ‘,’, 1)

  • 0

I have a query:

SELECT I.Id
    , CAST(SUBSTRING_INDEX(GROUP_CONCAT(I.StatusId ORDER BY I.TransactionId DESC), ',', 1) AS UNSIGNED) AS StatusId
    , SUM(I.RefundAmount) AS RefundAmount
    FROM (
        SELECT I.Id
            , IT.Id AS TransactionId
            , IT.StatusId
            , IF(IT.TypeId = 2, IT.RefundAmount, 0) AS RefundAmount
            FROM Items I
            INNER JOIN ItemTransactions IT ON IT.ItemId = I.Id
            WHERE I.Id = someValue
    ) I
    GROUP BY I.Id
    HAVING StatusId = 1 AND RefundAmount = 0

Where Items table has transaction records stored in ItemTransactions table. I’ve been using this type of query and works for me until this time, got some issues with the having clause.

The query works in SQL Editors but not working properly when used on stored procedures. (Don’t get me wrong, I’ve been using this query for most of my stored procedures). Debugging line per line, found that there is an issue with having clause.

As a temporary fix, I changed the query to:

SELECT I.Id
    , I.StatusId
    , I.RefundAmount
    FROM (
        SELECT I.Id
            , CAST(SUBSTRING_INDEX(GROUP_CONCAT(I.StatusId ORDER BY I.TransactionId DESC), ',', 1) AS UNSIGNED) AS StatusId
            , SUM(I.RefundAmount) AS RefundAmount
            FROM (
                SELECT I.Id
                    , IT.Id AS TransactionId
                    , IT.StatusId
                    , IF(IT.TypeId = 2, IT.RefundAmount, 0) AS RefundAmount
                    FROM Items I
                    INNER JOIN ItemTransactions IT ON IT.ItemId = I.Id
                    WHERE I.Id = someValue
            ) I
            GROUP BY I.Id
            --HAVING StatusId = 1 AND RefundAmount = 0
    ) I
    WHERE I.StatusId = 1 AND I.RefundAmount = 0

The query works fine. But I’d like to know if somebody has already encountered this, and found a fix. I’m using MySQL 5.0.

Thanks

  • 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-09T12:46:24+00:00Added an answer on June 9, 2026 at 12:46 pm

    WHERE clause is used to filter data on the common attributes and expressions. HAVING clause is used to filter data after groupping had been performed and it’s arguments should be either the ones of the GROUP BY clause, or expressions containing aggregate functions.
    It is also illegal to use the alias of the column in WHERE, GROUP BY or HAVING clauses, while it does works for the ORDER BY clause.

    One option, as you’ve found, is to use a sub-query and then column references will work.
    Another one is to duplicate the whole expression in the HAVING clause:

    SELECT I.Id,
        CAST(SUBSTRING_INDEX(GROUP_CONCAT(I.StatusId ORDER BY I.TransactionId DESC),
               ',', 1) AS UNSIGNED) AS StatusId,
        SUM(I.RefundAmount) AS RefundAmount
        FROM (
            SELECT I.Id
                , IT.Id AS TransactionId
                , IT.StatusId
                , IF(IT.TypeId = 2, IT.RefundAmount, 0) AS RefundAmount
                FROM Items I
                INNER JOIN ItemTransactions IT ON IT.ItemId = I.Id
                WHERE I.Id = someValue
        ) I
        GROUP BY I.Id
        HAVING
        CAST(SUBSTRING_INDEX(GROUP_CONCAT(I.StatusId ORDER BY I.TransactionId DESC),
             ',', 1) AS UNSIGNED) = 1
        AND SUM(I.RefundAmount) = 0;
    

    EDIT: A closer look leaded to this question Using column alias in WHERE clause of MySQL query produces an error and to the MySQL documentation, that outlines that it is possible to use column aliases in GROUP BY, HAVING and ORDER BY clauses if such aliases are quoted with backticks, like:

    ...
    HAVING `StatusId` = 1 AND `RefundAmount` = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: SELECT sl.sms_prefix, sum( sl.parts ) , cp.country_name, CAST(SUM(sl.parts) AS NUMERIC(10,4))
I have this query : SELECT ..... ,( CAST(col1 AS MONEY) / CAST (col2
I have a query: select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total from dq.data group
I have this query: SELECT COUNT(*) AS invoice_count, IFNULL(SUM(qa_invoices.invoice_total), 0) AS invoice_total, IFNULL(SUM(qa_invoices.invoice_discount) ,0)
I have a query like this: SELECT Name, REPLACE(RTRIM(( SELECT CAST(Score AS VARCHAR(MAX)) +
I have a query select *, right( convert(varchar, cast(JOB_DONE_time-JOB_send_time as datetime), 121), 12 )
I have the following query: SELECT o.OrderNumber, cast(o.DateOrdered as datetime), cast(oi.Category as varchar(max)) FROM
I have the following query: SELECT CAST([Action] AS NVARCHAR(4000)) AS CastAction, CHARINDEX(CAST([Action] AS NVARCHAR(4000)),
I have a SQL query: SELECT b . * , CONCAT( GROUP_CONCAT( c.name )
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt

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.