I have looked far and wide for a way to get rows of results into columns to no avail. I have order numbers that have multiple payments and I need the balance left unpaid. My anger comes from MSSQL2000 w/o pivot
Here's an example:
Column names:
[ip_amt | packout_id | payments_id | date_received | invoice_num | amount_received]
[html]
114.1300 22859 105 2007-02-05 INV123 94.27
NULL 22859 2628 2007-03-05 INV123 19.86[/HTML]
$114.13 is what I paid, and $94.27 + $19.86 is what I was paid over two months.
I need the balance amount but in the first column, I've only got the balance of one month and the next month doesn't have enough data. How can I sum up the two amount_received cells on the far-right column?
What I need is for it to say "Balance = $0.00 for this invoice" since over the two months, it equals the $114.13
This is the view that I am querying to get the columns -
select
-
sum(user_item_credit_amount) as ip_amt,
-
amount_received,
-
p.amount_received - sum(user_item_credit_amount) bal,
-
--p.date_received,
-
p.check_credit_num,
-
p.packout_id,
-
pk.invoice_num,
-
date_received,
-
p.payments_id
-
from payments p, item_payments i, packout pk
-
where i.payments_id = p.payments_id
-
and pk.packout_id = i.packout_id
-
group by date_received,pk.invoice_num,amount_received,check_credit_num,p.packout_id, p.payments_id
Any suggestions?
2 1235
I have looked far and wide for a way to get rows of results into columns to no avail. I have order numbers that have multiple payments and I need the balance left unpaid. My anger comes from MSSQL2000 w/o pivot
Here's an example:
Column names:
[ip_amt | packout_id | payments_id | date_received | invoice_num | amount_received]
[html]
114.1300 22859 105 2007-02-05 INV123 94.27
NULL 22859 2628 2007-03-05 INV123 19.86[/HTML]
$114.13 is what I paid, and $94.27 + $19.86 is what I was paid over two months.
I need the balance amount but in the first column, I've only got the balance of one month and the next month doesn't have enough data. How can I sum up the two amount_received cells on the far-right column?
What I need is for it to say "Balance = $0.00 for this invoice" since over the two months, it equals the $114.13
This is the view that I am querying to get the columns -
select
-
sum(user_item_credit_amount) as ip_amt,
-
amount_received,
-
p.amount_received - sum(user_item_credit_amount) bal,
-
--p.date_received,
-
p.check_credit_num,
-
p.packout_id,
-
pk.invoice_num,
-
date_received,
-
p.payments_id
-
from payments p, item_payments i, packout pk
-
where i.payments_id = p.payments_id
-
and pk.packout_id = i.packout_id
-
group by date_received,pk.invoice_num,amount_received,check_credit_num,p.packout_id, p.payments_id
Any suggestions?
am assuming INVOICE_NUM is a unique key.
try: -
-
select a.invoice_num, a.ip_amt, b.total_paid, balance = a.ip_amt - b.total_paid
-
from mytable
-
inner join (select invoice_num, sum(amount_received) as total_paid from mytable group by invoice_num) b on a.invoice_num = b.invoice_num
-
-
-
select a.invoice_num, a.ip_amt, b.total_paid, balance = a.ip_amt - b.total_paid
-
from mytable
-
inner join (select invoice_num, sum(amount_received) as total_paid from mytable group by invoice_num) b on a.invoice_num = b.invoice_num
-
Thanks for the reply. No, invoice num can be linked to multiple payments and therefore payments_id is. I messed around a bit and found a solution I think others can adapt it as needed. I hope this helps someone.
Let me clarify the application first:
I have orders that are paid over time and an accrued balance. I need to balance for the order at a given month however I only have the total I paid out and the individual amounts I was paid so I can't do something like this:
100 -10(january) - 10(february) because each time I subtract 10, it will be for the total (100). Which sucks. So given the above data, here it is:
select * from
(select ip_amt, payments_id, amount_received,invoice_num,packout_id, date_received, (case when amount_received - ip_amt < 0 then (select sum(amount_received)-ip_amt from payments where packout_id = temp.packout_id) end) as bal_master,
(case when amount_received - ip_amt > 0 then
(select sum(amount_received)-ip_amt from payments where packout_id = temp.packout_id) end) as bal_overallocated
from temp
as resultset
order by date_received, invoice_num
NOTES:
The key is in the CASE statements. I have a view that is summing the ip_amt which for the first entry is the total for all payments and then each row under it (following payment) is null.
I'm outta time on this project so when I get a chance, I'll elaborate but this should help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bernie |
last post by:
Greetings,
I have 3 servers all running SQL Server 2000 - 8.00.818. Lets call
them parent, child1, and child 2.
On parent, I create a view called item as follows:
CREATE view Item as...
|
by: Bing Wu |
last post by:
Hi Folks,
I have a very large table containing 170 million rows of coordinats:
CREATE TABLE "DB2ADMIN"."COORDINATE" (
"FID" INTEGER NOT NULL ,
"AID" INTEGER NOT NULL ,
"X" REAL NOT NULL ,...
|
by: Serious_Practitioner |
last post by:
Good day, and thank you in advance for any assistance you can provide.
I have a table in an Access 2000 .mdb file, and I've run into something odd
and insolvable, at least for me. The database is...
|
by: Ireneus Broncel |
last post by:
I have a class which reads Groups and Users from ActiveDirectory. The
Problem is, that i have about 10000 rows as product. When I am trying to
read the "memberOf" Objects out of this field i get...
|
by: kiqyou_vf |
last post by:
I'm trying to pull data from 2 different tables and do a loop to
retrieve more than one row. I'm having problems with aligning the
information. Can someone lead me in the right direction? I've done...
|
by: schoultzy |
last post by:
Hello Everyone,
This is probably a simple fix so be kind when/if you reply. The query
below retrieves information for individuals based on a column named
ATTRIB_DEF, and assorted other columns;...
|
by: Sam Durai |
last post by:
Hello, How can I identity the list of columns (with the table in which
it is located) present in a database which has
A blank or NULL value for ALL rows ( if the column is CHAR)
A zero (0) for...
|
by: gerbski |
last post by:
Hi all,
I am relatively new to ADO, but up to now I got things working the way
I wanted. But now I've run into somethng really annoying.
I am working in MS Access. I am using an Access...
|
by: Sam |
last post by:
Hi,
I have one table like :
MyTable {field1, field2, startdate, enddate}
I want to have the count of field1 between startdate and enddate, and
the count of field2 where field2 = 1 between...
|
by: pmouseca |
last post by:
Hello all,
I have two tables, one call Assignments, the other call Grades.
In Assignment, the columns are:
Asn_ID, Asn_Name, Asn_MaxGrade
In Grades, the columns are:
Grade_ID,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |