Print Page | Close Window

Combine two files into one

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Report Design
Forum Discription: The best way to design a report and problems you have encountered
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=12117
Printed Date: 17 May 2024 at 8:10pm


Topic: Combine two files into one
Posted By: Kitty1
Subject: Combine two files into one
Date Posted: 20 Jan 2011 at 8:21am
Hello,
 
I want to write a report that will pull data from 2 different tables, but combine them as one - based on a common field.  Specifically, I want to use field PART NUMBER in our inventory file to look up  1. all open lines on purchase orders, also 2. all open work orders.   The data has to be blended as one file, sorted by part number.  Is there a way to do this???
 
 



Replies:
Posted By: hilfy
Date Posted: 20 Jan 2011 at 9:23am

Do you have a third table that identifies all of your part numbers and what they relate to?  If you have that, this is not difficult.  If you don't have that, you're going to have to write a "Command" which is a SQL Select statement instead of selecting tables and having Crystal create the SQL for you.

Assuming you have three tables, Parts, PurchaseOrders, and WorkOrders, this should get you going in the right direction:
 
1.  Link from Parts to PurchaseOrders on Part Number.  Right-click on the link and select "Link Options" and make this a Left Outer Join.  This will include all parts from the Parts table even if they're not in PurchaseOrders.
 
2.  Link from Parts to WorkOrders. Make the link a Left Outer Join as in step 1.
 
3.  Set up the record selection formula - In the Select Expert, select any field to get you started - it doesn't matter which field because you're not actually going to use it.  Click on "Show Formula" and then on "Formula Editor".  You'll need to enter something like the following:
(not IsNull({PurchaseOrders.PartNumber}) or not IsNull({WorkOrders.PartNumber)) and
(IsNull({PurchaseOrders.PartNumber}) or {PurchaseOrders.OpenOrder}= 1) and
(IsNull({WorkOrders.PartNumber}) or {WorkOrders.OpenOrder} = 1)
 
This assumes that there could be a purchase order without a work order or a work order without a purchase order.  If there will always be both a purchase order and a work order, you can skip the steps to change the joins and just use the Select Expert to pull in open purchase orders or open work orders.
 
-Dell
 


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 21 Jan 2011 at 7:44am

hello and thank you for your reply.  what i want to do is read all the records, both work orders and purchase orders, as if they are one file.  in other words, i use the part to link to each file, but i would like to somehow output them to a temporary file, where i could then sort both files together by due date.    Is this possible ?



Posted By: Kitty1
Date Posted: 21 Jan 2011 at 8:11am
just another point on the report i'm writing and what i'm trying to do.  what i want to do is when a person enters a part number for parameter, i want to print the on-hand qty. of the part, then, in order of date, depending on if it is a work order or a purchase order, add or subtract and keep a running total of what the qty. on hand will be as of that date. 


Posted By: hilfy
Date Posted: 21 Jan 2011 at 10:05am
How are you SQL skills?  The only way I can think of to do what you need is to write command that has a UNION query that will pull together the work orders and the purchase orders as if they were in the same table.
 
You mention files.  What type of database are you connecting to?  Commands are only available for databases - they're not available for file-based data.  For example, if your report is reading data from Excel files, you'll need to have a way of loading them into a database in order to create this report.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 24 Jan 2011 at 7:10am
Hi, Thanks again for your follow-up.  :-)
 
 I have no SQL skills.   :-(   What I do  know about Crystal reports is self-taught. I'm actually pulling the information from tables.  Is there an easy way to explain how to do a Union of the two tables ? 
 
 


Posted By: hilfy
Date Posted: 24 Jan 2011 at 9:30am
First off, what type of database are you connecting to?  I need that to determine whether a command is even an option.  Then I would need to know the structure of your tables, how they "link together", and which fields you need.  From there I can probably put together some sample SQL for you.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 25 Jan 2011 at 10:12am
Hi, I'm connecting to ODBC data tables.


Posted By: Kitty1
Date Posted: 25 Jan 2011 at 10:16am

I did read about Union queries,  but according to what I read, both tables must have the same table structure.  you are not able to perform a union on two tables with different fields.   These tables both have a common field  (part number)  but I want to look at P.O. # in the purchase order table, and Work Order # in the job file and use this as one field, also qty on order/qty. committed and due dates.   They do not have the same table structures.  



Posted By: hilfy
Date Posted: 25 Jan 2011 at 11:03am
If they have similar structures and the fields you're using are of the same type, you can use them - the field names don't have to be the same, just the field types.  If there is a field in one table that you need, but no corresponding field in the other table, use Null in the select.
 
The field names in your result set will be the ones from the first Select in the union query.
 
Here's an example
 
Select
  PO_Number as OrderNumber,
  Order_date,
  qty as quantity
from purchase_orders

union

Select
  WO_Number as OrderNumber,
  Due_Date as Order_Date,
  qty_on_order as quantity
from work_orders
 
This pulls data from two tables with different field names, but the same structure in terms of the fields that are included in the query.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 26 Jan 2011 at 2:49am
thank you.   i hate to sound stupid, but i am so new at this.  how and where would i enter this command?  and would i link to both files or just one ?


Posted By: hilfy
Date Posted: 26 Jan 2011 at 3:17am
In the Database Expert, you would replace both tables with a command that selects everything you need for the report.  To add a command, open the connection on the left side of the screen and look for "Add Command".  If it's not there, then the type of database you're using doesn't allow commands.  I can't think of a different way of getting the data in the format that you need, though.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 27 Jan 2011 at 7:14am
Thank you SO MUCH!  I think I'm headed in the right direction now.  Still some bugs to work out, but that seems to do the trick of combining the two tables!    Now I want to somehow ADD to the inventory on hand if it's a purchase order and subtract if it's a work order.  keep a running total by date.  any suggestions ?   I really appreciate all your help!!!  Tongue


Posted By: hilfy
Date Posted: 27 Jan 2011 at 11:05am

Because you just need the starting inventory, this is going to be a little complicated, but it is doable.

1.  Add a string to each Select in the union query that will identify which table the data is coming from.  This is as simple as adding something like the following to the PO Select
 
'P' as TableType
 
and this to the WO Select:
 
'W' as TableType
 
2.  While you're editing the command, also add the inventory quantity to each of the select statements.  This will bring in the same value for all records for each part, but that's ok - we can work with that.  For this example I'm going to use the field name "QtyOnHand" for this data.
 
3. Create a formula that can be used to summarize the quantity changes.  I'm going to call this {@QtyForInventory}.  It will look something like this:
 
If {command.TableType} = 'P' then {command.qty} else ({command.qty} * -1)
 
4.  If you need just the final quantity, create a formula that looks like this:
 
{command.QtyOnHand} + sum({@QtyForInventory}, {command.PartNumber})
 
Let me know if you need a running total instead, that will take a little more work.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 28 Jan 2011 at 5:20am

Hello, yes I'm looking to have a running total on each line.   for the first record I want to add or subtract to the beginning qty on hand, and then on the next line I want to add or subtract from the result of the previous record, and then continue the same on the next line... for example:

Part No. 2041
 
Qty on Hand  1,421
 
work order/po#                qty                     date              Inventory Balance
 
25633                             2098                  1/1/2011                 3519
8407                                 100                  3/28/2011               3419
8410                                   20                  3/28/2011               3399
 
 
I can't figure out how to do this.  Any help you could give me would be greatly appreciated, you have been so awesome already!  :-)
 


Posted By: Kitty1
Date Posted: 28 Jan 2011 at 5:28am
p.s.  This is how my command looks now, how would i change it?  i tried adding the p and w for tabletypes but got an error message.
 
Select PART, PURCHASE_ORDER as ORDERNO, DATE_DUE_LINE as ORDERDATE,QTY_ORDER as QTY from V_PO_LINES
Union
Select PART,JOB as OrderNumber,DATE_DUE as ORDERDATE,QTY_COMMITTED as QTY from V_JOB_COMMITMENTS


Posted By: hilfy
Date Posted: 28 Jan 2011 at 7:08am
What is the name of the inventory table?  Does it have a PART field to link to?  What is the name of the field that has the qty on hand?
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 28 Jan 2011 at 8:37am
the Inventory table name is V_INVENTORY_MSTR  and the part number field is PART


Posted By: Kitty1
Date Posted: 28 Jan 2011 at 8:38am

sorry...the qty on hand field is called QTY_ONHAND

thanks!



Posted By: hilfy
Date Posted: 28 Jan 2011 at 9:26am
Change your SQL to something like this:
Select 
  'P' as TABLETYPE, 
  po.PART,
  po.PURCHASE_ORDER as ORDERNO,
  po.DATE_DUE_LINE as ORDERDATE,
  po.QTY_ORDER as QTY,
  inv.QTY_ONHAND
from V_PO_LINES as po
  inner join V_INVENTORY_MSTR as inv on po.PART = inv.PART

Union

Select 
  'W' as TABLETYPE,
  jc.PART,
  jc.JOB as OrderNumber,
  jc.DATE_DUE as ORDERDATE,
  jc.QTY_COMMITTED as QTY,
  inv.QTY_ONHAND
from V_JOB_COMMITMENTS
  inner join V_INVENTORY_MSTR as inv on po.PART = inv.PART

Then you'll need to do the following:
 
1.  Create the {@QtyForInventory} formula as specified above. 
 
2.  Group your report on {command.PART}
 
3.  Create a running total (I'll call it {#Inventory}):
Field to Summarize:  {@QtyForInventory}
Evaluate: On Each Record
Reset: On change of group {command.PART}
 
4.  Create a formula: {command.QTY_ONHAND} + {#Inventory}
 
This will display your running total on the inventory.
 
-Dell
 


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 28 Jan 2011 at 11:07am
Hi,
 
Thanks!  I'm really confused on the SQL.  I tried entering it exactly as it's written and I got errors.
 
This is what I have now.   It took the table type o.k. that seems to be working.
 
 
Select 'P' as TABLETYPE, PART, PURCHASE_ORDER as ORDERNO, DATE_DUE_LINE as ORDERDATE,QTY_ORDER as QTY from V_PO_LINES
Union
Select 'W' as TABLETYPE,PART,JOB as OrderNumber,DATE_DUE as ORDERDATE,QTY_COMMITTED as QTY from V_JOB_COMMITMENTS
 
 
 
but when i try to select  the on-hand qty from V_INVENTORY_MSTR, it doesn't work.
 


Posted By: hilfy
Date Posted: 31 Jan 2011 at 7:17am
You have to create the joins to V_Inventory_Master in order to select data from it. 
 
I think I see where the error is in what I posted  - Change these lines at the end of the SQL:
 
from V_JOB_COMMITMENTS
  inner join V_INVENTORY_MSTR as inv on po.PART = inv.PART
to this:
 
from V_JOB_COMMITMENTS as jc
  inner join V_INVENTORY_MSTR as inv on jc.PART = inv.PART
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 01 Feb 2011 at 2:59am

I am so CLOSE now to what I want.....I have the running total adding or subtracting as it should.  The only problem now I'm getting is on the first record I want the balance of on-hand +/- qty.    Right now this is what I'm getting:

Qty. On Hand   1,421

w/o or p.o.#                  Due Date           +/- Qty              Inventory Balance
 
008407                        3/28/2011            -100                -100
008410                        3/28/2011              -20                -120
008453                        3/28/2011              -10                -130
 
 
on the first line, inventory balance should be 1,321
on the second line, should be 1,301  ...etc....
 
 


Posted By: hilfy
Date Posted: 01 Feb 2011 at 3:27am
You need to create a formula that takes your running total and adds it to the qty on hand number - since qty on hand is the same value for all records for a given part number, you don't have to do anything with that number.  Your formula will look something like this (using the name of the running total from my example):
 
{command.QTY_OnHand} - {#Inventory}
 
Use this formula on the report instead of just the running total.
 
-Dell


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Kitty1
Date Posted: 01 Feb 2011 at 10:03am
Bingo!  Got it !!!  LOL    Thank you so very much for all of your help.  Clap  I sincerely appreciate it, and sorry it took a while for things to "sink in" at times! 



Print Page | Close Window