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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:08:20+00:00 2026-05-17T17:08:20+00:00

I have the following table: CREATE TABLE posting ( id integer NOT NULL PRIMARY

  • 0

I have the following table:

CREATE TABLE "posting" (
    "id" integer NOT NULL PRIMARY KEY,
    "amount" real NOT NULL,
    "balance" real NOT NULL,
    "account_id" integer NOT NULL REFERENCES "account" ("id"),
    "asset_type_id" integer NOT NULL REFERENCES "asset_type" ("id")        
)

For this table, I manually generate ids in a way that there’s no gaps(records can’t be deleted). It is guaranteed that the following statement will return the latest record :

SELECT * FROM posting ORDER BY id DESC LIMIT 1

The problem is that now I need to retrieve not only the last, but the last record for each combination of ‘account_id’ and ‘asset_type_id’. For example, lets say I have two ‘accounts’ and two ‘asset_types’ (both with ids 1 and 2) and the following records(omiting amount and balance):

id | account_id | asset_type_id 
1  |    1       |    1
2  |    2       |    1
3  |    1       |    2
4  |    2       |    1
5  |    2       |    2
6  |    2       |    2

It will return me records 6, 4, 3 and 1 since records 5 and 2 were “replaced” by 6 and 4 respectively. I have no idea how to represent this in SQL, any help is appreciated.

  • 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-05-17T17:08:21+00:00Added an answer on May 17, 2026 at 5:08 pm

    What You need to peek is the largest value of ID for group of account_id and asset_type_id

    SELECT MAX(ID),account_id,asset_type_id FROM posting group by account_id, asset_type_id;

    Table

    id | account_id | asset_type_id 
    1  |    1       |    1
    2  |    2       |    1
    3  |    1       |    2
    4  |    2       |    1
    5  |    2       |    2
    6  |    2       |    2
    

    After group

    id | account_id | asset_type_id 
    1   |    1       |    1
    2,4 |    2       |    1
    3   |    1       |    2
    5,6 |    2       |    2
    

    After Max

    id | account_id | asset_type_id 
    1  |    1       |    1
    3  |    1       |    2
    4  |    2       |    1
    6  |    2       |    2
    

    EDIT:

    Q2:How To run this query without using whole table ?

    The SQL select statement has some phases that are done in specific order each of this phase create a temporary input table for next phase.

    (5) SELECT (5-2) DISTINCT (5-3) TOP (<top specification>) 
    (5-1) <select clauses>
    (1) FROM (1-J) <left table> <connection type> JOIN <right table> ON <predicates of on clause>
    <alias>
    (2) WHERE <predicates of where clause>
    (3) GROUP BY <grouping specification>
    (4) HAVING <predicates of having clause>
    (6) ORDER BY <list specifying the order>
    

    As You see the first step is the FROM, that in your case is one table, then we go to WHERE so when You put some predicates here for example account_id between 1 and 10 AND asset_type_id between 1 and 10 you will only operate on this piece of table in step 3 that is group by.

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

Sidebar

Related Questions

I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
We have the following table: CREATE TABLE [dbo].[CampaignCustomer]( [ID] [int] IDENTITY(1,1) NOT NULL, [CampaignID]
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
CREATE TABLE `pastebin` ( `pid` int(11) NOT NULL auto_increment, `poster` varchar(16) default NULL, `posted`

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.