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

SQL Query - grouping?

senojeel
I just need a push in the right direction.

I querying a MSSQL 2000 Database. I have couple of tables, one is Organizations and the other is Installments. What they do is, the Organizations is obviously org info. The installments is a table that has payments paid out to the organizations. What I want is to query the Organizations and have in the same result row all payments for that org.

For Example.
Organizations table - OrgId, OrgName
Installments - Installment Id, OrgId, NumberPayments, PaymentAmount

I want the results to be
OrgID, OrgName, NumberPayments, PaymentAmount1, PaymentAmount2

Does that make sense?

BTW, if it makes it any easier, there are always going to be only 2 payments.

Thanks!
Shawn
Aug 15 '07 #1
4 1136
azimmer
200 Expert 100+
I just need a push in the right direction.

I querying a MSSQL 2000 Database. I have couple of tables, one is Organizations and the other is Installments. What they do is, the Organizations is obviously org info. The installments is a table that has payments paid out to the organizations. What I want is to query the Organizations and have in the same result row all payments for that org.

For Example.
Organizations table - OrgId, OrgName
Installments - Installment Id, OrgId, NumberPayments, PaymentAmount

I want the results to be
OrgID, OrgName, NumberPayments, PaymentAmount1, PaymentAmount2

Does that make sense?

BTW, if it makes it any easier, there are always going to be only 2 payments.

Thanks!
Shawn
If there are no more than two payments:
Expand|Select|Wrap|Line Numbers
  1. select o.OrgID, o.OrgName, (select count(*) from Installments i where i.OrgID=o.orgID) as NumberPayments, p1.PaymentAmount, p2.PaymentAmount
  2. from Organizations o, Installments p1, Installments p2
  3. where o.OrgID=p1.OrgID and o.OrgID=p2.OrgID and p1.[Installment ID]<>p2.[Installment ID]
  4.  
BTW: What is NumberPayment in the Installments table?
Aug 16 '07 #2
If there are no more than two payments:
Expand|Select|Wrap|Line Numbers
  1. select o.OrgID, o.OrgName, (select count(*) from Installments i where i.OrgID=o.orgID) as NumberPayments, p1.PaymentAmount, p2.PaymentAmount
  2. from Organizations o, Installments p1, Installments p2
  3. where o.OrgID=p1.OrgID and o.OrgID=p2.OrgID and p1.[Installment ID]<>p2.[Installment ID]
  4.  
BTW: What is NumberPayment in the Installments table?
NumberPayment is just which payment it is, first or second, which I guess I wouldn't need if we are counting the number of payments already with this query.

Thanks, I will give this a try.

Shawn
Aug 16 '07 #3
NumberPayment is just which payment it is, first or second, which I guess I wouldn't need if we are counting the number of payments already with this query.

Thanks, I will give this a try.

Shawn
Okay. The query is returning either 4 or 8 rows for each organization, where it should be one. The NumberPayments is correct (2) for all rows of the org. The p1.PaymountAmount is correct on all 4 or 8 rows and the p2.PaymentAmount is never right (The number there doesn't make sense at all)

FYI, Each Installment has its own row in the InstallmentsOrg table.

Here is my query. I changed a few things.

Expand|Select|Wrap|Line Numbers
  1. select o.OrgId, o.OrgName, 
  2.     (select count(*)
  3.         from InstallmentsOrg i 
  4.         where i.OrgID=o.orgID AND YEAR(i.DateCreated) = 2007 AND i.InstallmentType = 'RAP') as NumberPayments,
  5.     p1.InstallmentAmt as Payment1,
  6.     p2.InstallmentAmt as Payment2,
  7.     p1.DateCreated,
  8.     p1.InstallmentType
  9. from Organizations o, InstallmentsOrg p1, InstallmentsOrg p2
  10. where o.OrgID=p1.OrgId and o.OrgID=p2.OrgId and p1.InstallmentId<>p2.InstallmentId AND YEAR(p1.DateCreated) = 2007 AND p1.InstallmentType = 'RAP'
Aug 16 '07 #4
So I think I might be closer?

This probably isn't the best way to do it, but I am getting closer on my results.

Here is my query:
Expand|Select|Wrap|Line Numbers
  1. select 
  2.     i.OrgId, 
  3.     o.OrgName, 
  4.     i1.InstallmentAmt as Installment1, 
  5.     i2.InstallmentAmt as Installment2,
  6.     min(i.InstallmentId) as InstallmentId_1, 
  7.     max(i.InstallmentId) as InstallmentId_2
  8. FROM InstallmentsOrg i
  9. LEFT JOIN Organizations o ON i.OrgId = o.OrgId
  10. Left Join InstallmentsOrg i1 ON i.InstallmentId = i1.InstallmentId
  11. Left Join InstallmentsOrg i2 ON i.InstallmentId = i2.InstallmentId
  12.  
  13. WHERE i.InstallmentType = 'RAP' and YEAR(i.DateCreated) = 2007
  14.  
  15. GROUP BY i.OrgId,o.OrgName,i1.InstallmentAmt, i2.InstallmentAmt
  16.  
  17. order by o.OrgName 
  18.  
My problem now is I can't get Installment2 to equal the 2nd Installment that was the ID of InstallmentId_2

Make sense?
Aug 16 '07 #5

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

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
5
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted...
9
by: cyrus.kapadia | last post by:
Let's say I have the following table: entry product quality 1 A 80 2 A 70 3 A 80 4 B 60 5 B 90 6 ...
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
7
by: jane | last post by:
HI, I was ask to do a query to get the member active condition I had table to show the member number and active status in three month. ACC A1 A2 A3 ---- -----------...
4
by: Philippe | last post by:
Hello, I encounter a problem that I cannot solve myself... The problem is the following: I make a table: several records: the first field is always a number, the following field is always...
5
by: Terri | last post by:
The following query will give me the top ten states with the most callers. SELECT TOP 10 Count(Callers.CallerID) AS CountOfCallerID, Callers.State FROM Callers GROUP BY Callers.State ORDER BY...
1
by: Regnab | last post by:
I often want to query the database with certain parameters in my query which I do not want included in the query result. The problem is when I want to group on 1, sum the 2nd (and group by) and...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
4
by: lorirobn | last post by:
Hi, I have a report displaying items that are missing from a room. I created 2 queries, the first getting the items IN the room, and the second being an "unmatched" query that references the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.