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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:53:36+00:00 2026-06-12T01:53:36+00:00

I have the following query: SELECT sum((select count(*) as itemCount) * SalesOrderItems.price) as amount,

  • 0

I have the following query:

SELECT sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount, 'rma' as     
    "creditType", "Clients"."company" as "client", "Clients".id as "ClientId", "Rmas".* 
FROM "Rmas" JOIN "EsnsRmas" on("EsnsRmas"."RmaId" = "Rmas"."id") 
    JOIN "Esns" on ("Esns".id = "EsnsRmas"."EsnId") 
    JOIN "EsnsSalesOrderItems" on("EsnsSalesOrderItems"."EsnId" = "Esns"."id" ) 
    JOIN "SalesOrderItems" on("SalesOrderItems"."id" = "EsnsSalesOrderItems"."SalesOrderItemId") 
    JOIN "Clients" on("Clients"."id" = "Rmas"."ClientId" )
WHERE "Rmas"."credited"=false AND "Rmas"."verifyStatus" IS NOT null 
GROUP BY "Clients".id, "Rmas".id;

The problem is that the table "EsnsSalesOrderItems" can have the same EsnId in different entries. I want to restrict the query to only pull the last entry in "EsnsSalesOrderItems" that has the same "EsnId".

By “last” entry I mean the following:

The one that appears last in the table "EsnsSalesOrderItems". So for example if "EsnsSalesOrderItems" has two entries with "EsnId" = 6 and "createdAt" = '2012-06-19' and '2012-07-19' respectively it should only give me the entry from '2012-07-19'.

  • 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-12T01:53:37+00:00Added an answer on June 12, 2026 at 1:53 am
    SELECT (count(*) * sum(s."price")) AS amount
         , 'rma'       AS "creditType"
         , c."company" AS "client"
         , c.id        AS "ClientId"
         , r.* 
    FROM   "Rmas"            r
    JOIN   "EsnsRmas"        er ON er."RmaId" = r."id"
    JOIN   "Esns"            e  ON e.id = er."EsnId"
    JOIN  (
       SELECT DISTINCT ON ("EsnId") *
       FROM   "EsnsSalesOrderItems"
       ORDER  BY "EsnId", "createdAt" DESC
       )                     es ON es."EsnId" = e."id"
    JOIN   "SalesOrderItems" s  ON s."id" = es."SalesOrderItemId"
    JOIN   "Clients"         c  ON c."id" = r."ClientId"
    WHERE  r."credited" = FALSE
    AND    r."verifyStatus" IS NOT NULL 
    GROUP  BY c.id, r.id;
    

    Your query in the question has an illegal aggregate over another aggregate:

    sum((select count(*) as itemCount) * "SalesOrderItems"."price") as amount
    

    Simplified and converted to legal syntax:

    (count(*) * sum(s."price")) AS amount
    

    But do you really want to multiply with the count per group?

    I retrieve the the single row per group in "EsnsSalesOrderItems" with DISTINCT ON. Detailed explanation:

    • Select first row in each GROUP BY group?

    I also added table aliases and formatting to make the query easier to parse for human eyes. If you could avoid camel case you could get rid of all the double quotes clouding the view.

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

Sidebar

Related Questions

I have the following query: select prop_id , sum(amount)bnp_spent , (select count(*) from cost
I have the following query: SELECT SUM(count) FROM (SELECT 'artist' AS table_name, COUNT(*) as
I have the following query: select col1, sum( col2 ), count( col3 ) from
so I have the following query: SELECT DISTINCT d.iID1 as 'id', SUM(d.sum + d.count*r.lp)/sum(d.count)
I have the following query SELECT YEAR(N.TICKETDATE) AS TICKETYEAR, MONTH(N.TICKETDATE) AS TICKETMONTH, SUM(DISTINCT N.NETWEIGHTHAULED)/COUNT(DISTINCT
I have the following query, in the top select statement (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) works well but
I have a query something like the following: select sum(t1.qty) t1Total, sum(t2.qty) t2Total, sum(t1.qty
I have the following query: SELECT item.`ID`, item.`Name`, item.`UnitCost`, item.`Price`, discount.`rate` FROM item INNER
I have the following query SELECT s.name, s.surname, s.id_nr, s.student_nr, s.createdate, s.enddate, (SELECT count(*)
I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st

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.