473,420 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

SQL Working with two rows at the same time.

2 2Bits
My transaction table has tran code SSN, and an amount, followed by another Tran code for same SSN, and the Amount is -ve. I habe to create report that has SSN and amount (= psitive - negative) how ?
Jul 17 '23 #1
2 12984
Rina0
13 Byte
To create a report that shows the SSN and the net amount (positive - negative) for each SSN, you can use SQL to perform the necessary calculations. Here's a sample SQL query that should achieve this:

Expand|Select|Wrap|Line Numbers
  1. sql
  2. SELECT SSN, SUM(amount) AS net_amount
  3. FROM (
  4.     SELECT SSN, 
  5.            CASE 
  6.                WHEN tran_code = 'SSN' THEN amount
  7.                ELSE -amount
  8.            END AS amount
  9.     FROM transaction_table
  10. ) AS t
  11. GROUP BY SSN;
  12.  
Explanation:
1. The inner subquery (aliased as `t`) uses a `CASE` statement to differentiate between positive and negative amounts based on the `tran_code`. If the `tran_code` is 'SSN', it keeps the amount as is, otherwise, it negates the amount.
2. The outer query then groups the results from the subquery by `SSN` and calculates the `SUM` of the `amount` for each `SSN`, giving you the net amount.

Make sure to replace `transaction_table` with the actual name of your transaction table in the database. When you execute this query, you should get the desired report with SSN and the corresponding net amount.
Jul 20 '23 #2
mmezzeld
2 2Bits
Thank you very much.

Moe.
Jul 20 '23 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steven Scaife | last post by:
I am trying to work out the length of time that a page was opened till it reached another page, I amusing the script below End_Of_Call = formatdatetime(now(), 0) 'work out the duration of the...
4
by: John Smith | last post by:
Hi, I'm having trouble working out the best way of calculating the time difference between two times on the same day. The example I have found does return the hours (in this case 8) but forgets...
3
by: Ant | last post by:
Hi, I need to find out what data types I need to work with time durations & how to add/subtract them from clock time. For example: A customer logs in at 7:30 in the morning. I need notification...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
1
by: Andrew Poulos | last post by:
I have a simple page with a form in it that gets posted to the following ASP <%@ language="javascript" %> <% var login_success_page = "../intro.asp"; var login_failed_page =...
1
by: Abhishek Bhatt | last post by:
I need to disable the mouse on click of ADD button. I wrote following code for that Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long on click of ADD ShowCursor...
1
by: spudse | last post by:
I am creating a PHP web application where users can see which events have occured between two input dates. These events and their datetime are stored in a database in UTC timezone. The user is...
6
by: Lara1 | last post by:
I'm trying to get certain cells to show a hovering alert message when I click on them. (I don't want an error-message style box to pop up, because I'll eventually want it to show for lots of cells...
6
by: vspsdca | last post by:
Hi, I'm a newbie here and does not really have any formal training on VBA. I just need help to get my hyperlink to work. I have a form in MS Access with a browse button where a user can select a...
6
by: Mike Copeland | last post by:
I am working with a file of data that contains "time values" (e.g. "1225365563"), and I want to convert this data into something I can use in a program I'm developing. From what I've read about...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.