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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:15:06+00:00 2026-06-09T08:15:06+00:00

Here is my SQL query: SELECT (CASE (elapsed_time_from_first_login IS NULL) WHEN true THEN 0

  • 0

Here is my SQL query:


SELECT (CASE (elapsed_time_from_first_login IS NULL) 
                        WHEN true THEN 0 
                        ELSE elapsed_time_from_first_login END) 
FROM (
   SELECT (now()::ABSTIME::INT4 - min(AcctStartTime)::ABSTIME::INT4) 
   FROM radacct
   WHERE UserName = 'test156') AS elapsed_time_from_first_login;

When I execute the above query, I get this error:

ERROR: CASE types record and integer cannot be matched

From the error message I understand that PostgreSQL take the second select, respectively elapsed_time_from_first_login as a row, even if it will always be a single value (because of the min() function).

Question: do you have some suggestions on how to deal with this query?

  • 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-09T08:15:08+00:00Added an answer on June 9, 2026 at 8:15 am

    I suppose, what you are actually trying to do should look like this:

    SELECT COALESCE((SELECT now() - min(acct_start_time)
                     FROM   radacct
                     WHERE  user_name = 'test156')
                  , interval '0s')
    

    While there is an aggregate function in the top SELECT list of the subselect, it cannot return "no row". The aggregate function min() converts "no row" to NULL, and the simple form below also does the trick.

    db<>fiddle here
    Oldsqlfiddle

    Other problems with your query have already been pointed out. But this is the much simpler solution. It returns an interval rather than an integer.

    Convert to integer

    Simplified with input from artaxerxe.
    Simple form does the job without check for "no row":

    SELECT COALESCE(EXTRACT(epoch FROM now() - min(acct_start_time))::int, 0)
    FROM   radacct
    WHERE  user_name = 'test156';
    

    Details about EXTRACT(epoch FROM INTERVAL) in the manual.

    Aggregate functions and NULL

    If you had used the aggregate function count() instead of sum() as you had initially, the outcome would be different. count() is a special case among standard aggregate functions in that it never returns NULL. If no value (or row) is found, it returns 0 instead.

    The manual on aggregate functions:

    It should be noted that except for count, these functions return a
    null value when no rows are selected. In particular, sum of no rows
    returns null, not zero as one might expect, and array_agg returns
    null rather than an empty array when there are no input rows. The
    coalesce function can be used to substitute zero or an empty array for
    null when necessary.

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

Sidebar

Related Questions

i have a sql query: SELECT TAT, SUM(CASE WHEN [Month Entered] = 1 THEN
SQL server 2008 Hello here is my query which returns the result SELECT *
I have a huge query which uses case/when often. Now I have this SQL
OK, here's yet another sql query that's giving me headaches. Case: I'm listing a
I have the following query in sql: SELECT a1 FROM dbo.myProductNames WHERE keycode =
In a SQL Server 2005 database I'm working on this query: select * from
I have the following SQL query: SELECT DISTINCT business_key FROM Memory WHERE concept <>
I have the following sql query SELECT * FROM jos_jcalpro_events AS e LEFT JOIN
I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
I would like to check if there's no data coming from SQL query then

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.