473,387 Members | 1,483 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,387 software developers and data experts.

Query rows of an entry into columns

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
Expand|Select|Wrap|Line Numbers
  1. select
  2.     sum(user_item_credit_amount) as ip_amt, 
  3.     amount_received,
  4.     p.amount_received - sum(user_item_credit_amount) bal,
  5.     --p.date_received,
  6.     p.check_credit_num,
  7.     p.packout_id,
  8.     pk.invoice_num,
  9.     date_received,
  10.     p.payments_id
  11. from payments p, item_payments i, packout pk
  12. where i.payments_id = p.payments_id
  13.     and pk.packout_id = i.packout_id
  14. group by  date_received,pk.invoice_num,amount_received,check_credit_num,p.packout_id,    p.payments_id

Any suggestions?
Nov 30 '07 #1
2 1235
ck9663
2,878 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. select
  2.     sum(user_item_credit_amount) as ip_amt, 
  3.     amount_received,
  4.     p.amount_received - sum(user_item_credit_amount) bal,
  5.     --p.date_received,
  6.     p.check_credit_num,
  7.     p.packout_id,
  8.     pk.invoice_num,
  9.     date_received,
  10.     p.payments_id
  11. from payments p, item_payments i, packout pk
  12. where i.payments_id = p.payments_id
  13.     and pk.packout_id = i.packout_id
  14. 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:



Expand|Select|Wrap|Line Numbers
  1.  
  2. select a.invoice_num, a.ip_amt, b.total_paid, balance = a.ip_amt - b.total_paid
  3. from mytable
  4. 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
  5.  
  6.  
Nov 30 '07 #2
Expand|Select|Wrap|Line Numbers
  1. select a.invoice_num, a.ip_amt, b.total_paid, balance = a.ip_amt - b.total_paid
  2. from mytable
  3. 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
  4.  
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.
Nov 30 '07 #3

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

Similar topics

5
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...
7
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 ,...
3
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...
0
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...
28
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...
2
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;...
1
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...
6
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...
5
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...
1
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,...
0
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,...
0
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$) { } ...
0
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...
0
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...
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?
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
jinu1996
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...

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.