Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Having trouble with starting table Post Reply Post New Topic
Author Message
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Topic: Having trouble with starting table
     Posted: 24 Jun 2009 at 12:32pm
Ok, I'll try my best to explain this clearly.  I'm using Great Plains SQL tables.  I have three tables:

Table 1 (RM30201) has a record of all invoices and what check amounts were applied to them.

Table 2 (SOP30200) is a sales history table, which lists invoices and there details, but not the information on the individual line items on the invoice.  In other words, it would show the ship date, and document total, but not the different line items and there quantities, etc.

Table 3 (SOP30300) is a sales history line item table.  It lists the details on the line items for each invoice.

My problem is that Table 1 is my starting point and sometimes has more than one record for an invoice.   This is an accounting issue that cannot be overcome, sometimes we just get multiple checks for the same invoice.

Table 2 only has one record for each invoice but needs to be joined to Table 3 which will have a record for each item on its invoice.  Because Table 1 has multiple records I am getting duplicates of the sop tables. 

If Table 1 only had 1 record for each invoice (which showed the sum of check amounts applied to the invoice) I would be fine.  How can I get around this?  Is there, for instance, a way to take Table 1 and from it create a new table that would sum multiple entries and provide a distinct column for invoice number and a column for the total check amounts applied?

There is no other table with the data I need.  Here is my CR command. 

select
   *
from
   rm30201
inner join
  sop30200
on
  rm30201.aptodcnm=sop30200.sopnumbe
left join
  sop30300
on
  sop30200.sopnumbe=sop30300.sopnumbe
 
 
IP IP Logged
AdamField
Groupie
Groupie


Joined: 04 Jun 2009
Online Status: Offline
Posts: 88
Quote AdamField Replybullet Posted: 25 Jun 2009 at 12:55am
Hey Bredlee,
 
I had a simular problem here @the job and if you don't need the data in the check amount you can just do a view in the SQL with a distinct on the invoice number
SELECT DISTINCT invoice FROM RM30201
This would select the every invoice number only 1 time
 
If you need the info from the check and you need or the highest or the lowest you can use:
 
SELECT     TOP 100 PERCENT x.h_document, x.h_user, x.RecCount
FROM         (SELECT     h_document, h_user, COUNT(*) AS RecCount
                       FROM          dbo.hissto
                       GROUP BY h_document, h_user) x INNER JOIN
                          (SELECT     h_document, MIN(RecCount) AS RecCount
                            FROM          (SELECT     h_document, h_user, COUNT(*) AS RecCount
                                                    FROM          dbo.hissto
                                                    GROUP BY h_document, h_user) y
                            GROUP BY h_document) z ON x.RecCount = z.RecCount AND x.h_document = z.h_document

you will have to change the field info (as this is a copy paste from mine witch is invoice and user (sales person) just change document -> you invoice field and user -> check
you can change the MIN (recCount ) to MAX if you need the highest check amount
 
ps: a friend of mine wrote this in MSSQL but  don't ask me to explain :p
 
Hope this helps you a bit
 
Greetings
 
Adam
 
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 25 Jun 2009 at 6:18am
Or...you could start with table 2 and join it to table 3...this is the order and the detail for the order, you could then do a left join to table 1, the payments, which will still give you multiple lines...so doing what I hate...join 2 and 3 and call a subreport for table 1. 
 
I don't know what the objective of the report is, but this would give you the ability to show payments to an invoice (1 or more) and the details of the invoice.
 
It all depends on what and how the report is to display
 
HTH
IP IP Logged
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Posted: 25 Jun 2009 at 8:12am
Adam,

I understand the use of the distinct parameter, but what I need is say the rm30201 table has the following:

invoice number | check amount
00101               | $100
00101               | $150
00102               | $50

to change to the following:

invoice number | check amount
00101               | $250
00102               | $50

From there I would link to the sales tables on invoice number.

I don't see how this ties in with your response, if i'm missing something please elaborate.  Thanks!
IP IP Logged
bradlee27514
Newbie
Newbie


Joined: 24 Jun 2009
Location: United States
Online Status: Offline
Posts: 20
Quote bradlee27514 Replybullet Posted: 25 Jun 2009 at 11:53am
This worked

select
  *
from
(select
  aptodcnm, sum(actualapplytoamount) as totalapplyamount
from
  rm30201
group by
  aptodcnm)
as
  rm30201
inner join
  sop30200
on
  rm30201.aptodcnm=sop30200.sopnumbe
left join
  sop30300
on
  sop30200.sopnumbe=sop30300.sopnumbe 
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 25 Jun 2009 at 2:53pm
Sorry, misunderstood what you were desiring.  That things are doubling doesn't say much.  In a statement type report you might want to show each payment separately, not just the combined amount paid.  I was going off the premise that, that was the case, not that you wanted to join multiple tables and just get the sum of the payments, that would have been much simpler.  Also, from the way the tables were layed out, there is no need for a left join, if an invoice exists, it will have detail lines, so that is an inner join situation.  If a payment is made, it must be to an invoice, so that is basically an inner join as well.  If you want a left join, I would think it would be on all the invoices, and any possible payments that have been applied to the invoices.  Hence my solution.  If I misunderstood how the tables connect and the intent of the report, I'm sorry.
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum