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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:22:16+00:00 2026-06-12T02:22:16+00:00

Considering the following table: CREATE TABLE `foo`( `aaa` VARCHAR(45) NOT NULL, `bbb` VARCHAR(45) NOT

  • 0

Considering the following table:

CREATE TABLE `foo`(
  `aaa` VARCHAR(45) NOT NULL,
  `bbb` VARCHAR(45) NOT NULL,
  `ccc` INT NOT NULL,
  `ddd` INT NOT NULL,
  PRIMARY KEY(`aaa`, `bbb`, `ccc`)
);

the following data:

INSERT INTO `foo` (`aaa`, `bbb`, `ccc`, `ddd`)
VALUES
('qwe', 'rty', 0, 123),
('asd', 'fgh', 0, 456),
('asd', 'fgh', 1, 678);

the following result is to be gotten:

'qwe', 'rty', 0, 123
'asd', 'fgh', 1, 678

The objective is to get

  • Only one record for each (aaa, bbb) keypair
  • Favour records with ccc = 1 if there are more than 1 record for a (aaa, bbb) keypair in the table

So I think I need to concatenate result sets of 2 queries:

SELECT * FROM `foo` WHERE `ccc` = 1

and

SELECT * FROM `foo` WHERE `ccc` = 0

discarding a second query result row when there is already a same (aaa, bbb) keypair row in the first query result.

How to code it in SQLite?

  • 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-12T02:22:17+00:00Added an answer on June 12, 2026 at 2:22 am

    Try this one,

    SELECT  a.*
    FROM    foo a
            INNER JOIN
            (
                SELECT  aaa, bbb, MAX(ccc) maxC
                FROM foo
                GROUP BY aaa, bbb
            ) b ON a.aaa = b.aaa AND
                    a.ccc = b.maxC AND
                    a.bbb = b.bbb
    

    SQLFiddle Demo

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

Sidebar

Related Questions

Considering the following table: create table inttest ( someint INT(10) NULL DEFAULT NULL );
I have the following table: CREATE TABLE notes (noteId INTEGER PRIMARY KEY ASC, note,
I have a table like the following one CREATE TABLE [dbo].[MyTable]( [MyTableID] [int] IDENTITY(1,1)
Considering the following table: someId INTEGER #PK ageStart TINYINT(3) ageEnd TINYINT(3) dateBegin INTEGER dateEnd
First execute these tables and data dumps :- CREATE TABLE IF NOT EXISTS `Tags`
How to store results from following query into another table, considering there is an
I'm considering the following database structure but I'm not sure what type of Rails
I have the following table structure ( reduced of course ): CREATE TABLE `log`
Considering the following table: User CreatedDateTime Quantity ----- ----------------- -------- Jim 2012-09-19 01:00 1
Considering following code public class A { public static void main(String[] args) { new

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.