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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:28:38+00:00 2026-06-01T04:28:38+00:00

I get an error when i do the following: SELECT * from cGift c1

  • 0

I get an error when i do the following:

SELECT *
from cGift c1 left join
    (select c2.gidnumb, "t" new from cGift c2 where c2.gidnumb = 
        (select c3.gidnumb from cgift c3 where c3.id = c2.id and c3.date =
            (select min(c4.date) from cgift c4 where c2.id == c4.id ))) tNew
    on tNew.gidnumb = c1.gidnumb

Basically i have the table cgift with a list of donations on it. I need a query that return cgift with an extra column containing either “t” or null. The first donation (cgift.date) of each donor (cgift.id) should be “t”, the rest null.

Example:

gidnumb..id....date......new
10.......1.....2/1/2010..null
11.......2.....1/1/2010..t
12.......3.....1/1/2010..t
13.......1.....3/1/2010..null
14.......2.....2/1/2010..null
15.......4.....1/1/2010..t
16.......1.....1/1/2010..t

The nulls could be blancs or f or wtvr.

Can anyone tell me what’s wrong with my query, it’s driving me nuts.

  • 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-01T04:28:40+00:00Added an answer on June 1, 2026 at 4:28 am

    I think the following should work in pretty much any SQL product:

    SELECT
        cGift.gidnumb,
        cGift.id,
        cGift.date,
        first.isfirst
    FROM cGift
    LEFT JOIN (
        SELECT id, MIN(date) AS date, 't' AS isfirst
        FROM cGift
        GROUP BY id
    ) first ON cGift.id = first.id AND cGift.date = first.date
    

    UPDATE (addressing additional criteria):

    If a person may donate more than once on MIN(date) and you only want one donation to be marked with t, you could, for instance, do this:

    SELECT
        cGift.gidnumb,
        cGift.id,
        cGift.date,
        first.isfirst
    FROM cGift
    LEFT JOIN (
        SELECT
            MIN(g.gidnumb) AS g.gidnumb,
            g.id,
            g.date,
            't' AS isfirst
        FROM cGift g
        INNER JOIN (
            SELECT id, MIN(date) AS date
            FROM cGift
            GROUP BY id
        ) f ON g.id = f.id AND g.date = f.date
        GROUP BY g.id, g.date
    ) first ON cGift.id = first.id AND cGift.date = first.date
    

    That is, the innermost query finds minimum days for every person, like in the previous solution, but then it also details the list with specific gidnumb values, making sure that only one row per person will match this list in the cGift table.

    That query should still be runnable in any DBMS. It might well be less efficient, given the double grouping. Here’s an alternative, which also uses only standard SQL, no vendor-specific features (it should also be a bit more flexible than the previous query):

    SELECT
        gidnumb,
        id,
        date,
        CASE
            WHEN NOT EXISTS (
                SELECT *
                FROM cGift
                WHERE id = g.id
                    AND (
                        date < g.date OR
                        date = g.date AND gidnumb < g.gidnumb
                    )
            )
            THEN 't'
        END AS isfirst
    FROM cGift g
    

    As you can see, the isfirst column is calculated using a self-test on the table: if there’s no row in this table with the same id and either earlier date or, if the date is the same, lesser gidnumb, this row should be marked as t. In the absence of ELSE part of the CASE, ELSE NULL is implied. You can, if you like add something like ELSE 'f'.

    Still, your SQL product might possess features which you could benefit from by constructing a possibly simpler and more efficient query. Some products, for instance, support ranking functions (which are part of SQL standard too already, it’s just that they are not universally supported yet), and here’s what you could do with a ranking function called ROW_NUMBER():

    SELECT
        gidnumb,
        id,
        date,
        CASE ROW_NUMBER() OVER (PARTITION BY id ORDER BY date, gidnumb)
            WHEN 1
            THEN 't'
        END AS isfirst
    FROM cGift g
    

    This query ranks rows partitioning them by id and sorting first by date, then by gidnumb. Every row with the ranking of 1 in this case becomes the one that should be distinguished with t.

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

Sidebar

Related Questions

I have the following sql query: UPDATE (SELECT * FROM table_A INNER JOIN table_B
I have the following query: SELECT `snap`.`ID`, `user`.`username`, `vote`.`type` FROM (`snap`) JOIN `user` as
When I try to do the select statement, I always get the following error:
I get following error when trying to access Xampp from a network I've tried
I've got a MySQL query somewhat like the following: SELECT * FROM products LEFT
I have the following query: SELECT * FROM ( SELECT * FROM View_ExecutiveForecastReport WHERE
I have the following query: select id from table1 where some_func(?) = 1; where
I have the following query: SELECT l.id, (SELECT amount FROM lead_status WHERE lead_id =
I have the following query: SELECT * FROM (SELECT products.uid, products.title, products.ean, products.description, products.price,
So I have the following query: IF ( ( SELECT RevisionNumber FROM SchemaVersion )

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.