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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:33:34+00:00 2026-05-20T15:33:34+00:00

I’ve done a query like this : SELECT Code, kind, SUM(valP) AS value FROM

  • 0

I’ve done a query like this :

SELECT Code,
       kind,
       SUM(valP) AS value 
FROM transaction 
WHERE tp = 'N' and date = '20110312' 
GROUP BY Code,kind ORDER BY Code,kind;

on a database table that give me this resultset :

+------+------+------+
| Code |  kind| value|
+------+------+------+
|  1   |  TR  | 25,99| 
|  1   |  CT  | 22,17| 
|  2   |  TR  | 14,23| 
|  3   |  DD  | 09,67| 
|  3   |  DD  | 23,87|
|  3   |  CT  | 34,87|
+------+------+------+

The code represent a transaction code, kind represent payments type, and value represent the value of the payments.

I want to inclue in the result set only the grouped result that contain a kind type = ‘CT’ but i want to include also the other payments kind in the same transaction, for example i’ve tried by adding an having to the upper query:

SELECT Code,
       kind,
       SUM(valP) AS value 
FROM transaction 
WHERE tp = 'N' and date = '20110312' 
GROUP BY Code,kind HAVING kind = 'CT' 
ORDER BY Code,kind;

but i obtain something like this :

+------+------+------+
| Code |  kind| value|
+------+------+------+
|  1   |  CT  | 22,17| 
|  3   |  CT  | 34,87|
+------+------+------+

instead i want to retrieve something like this :

+------+------+------+
| Code |  kind| value|
+------+------+------+
|  1   |  TR  | 25,99| 
|  1   |  CT  | 22,17| 
|  3   |  DD  | 09,67| 
|  3   |  DD  | 23,87|
|  3   |  CT  | 34,87|
+------+------+------+

All lines of the transaction that have within a record with type CT , is there a way to obtain this ?

How can i do ?

I’m actually working on a SQL Server 2005 but i may need run a similar query in PostgreSQL.

Thanks

  • 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-20T15:33:35+00:00Added an answer on May 20, 2026 at 3:33 pm

    You can add condition that tells “This Code must have a row with CT” sa do a sub-query:

    SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ;
    

    And to your first query add a filter to show only those records which have Code in previous subquery:

    ... AND Code IN (SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ) ...
    

    This will get rid of record Code 2, because 2 will no be in results from first query

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

Sidebar

Related Questions

No related questions found

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.