Link to home
Start Free TrialLog in
Avatar of Ray
RayFlag for United States of America

asked on

Only return values on first row of join

Table A is ORDERS.  In this, there is the OrderNo and other columns such as:  total cost, total tax, total discount, total shipping.
Table B is ITEMS.  In this there is order# and rows for each item on an order (1 or more per order).

The problem:
I want my output to only show the total cost, total tax, total discount, total shipping on the first row of each order (there may be multiple rows due to multiple items).

As it is today shown below, but gives "orderDate, customer#, total cost, total tax, total discount, total shipping" on every row.  I only need this once per orderNo.  I'd like those fields to be blank on subsequent rows within each order.

select  tableA.OrderNO, orderDate, customer#, total cost, total tax, total discount, total shipping
 ,tableB.Item#, ItemDescription, ItemCost, ItemSalesPrice
from tableA
   join tableB on tableA.orderno = tableB.orderno

I'm using DB2 for i, but I'm hopeful I could adapt most any SQL query regardless of specific SQL implementation.
Avatar of PortletPaul
PortletPaul
Flag of Australia image

This is a formatting or report requirement that is best performed after sql has supplied the raw data.

Just imagine the nightmare if trying to sort the output by order number if most rows have a blank in that column.
Avatar of Ray

ASKER

The "blanks" would be in the "amounts".  Imaging trying to get totals when the order total, sales tax, discount is repeated on every row :-)
For better or worse all of those things are applied at the order level, not item.  Yet I still need a listing of the items with the order output.

I don't disagree with your assessment, although I'd much prefer to "spit it out" in it's final form since manual manipulation is quite messy.
can you provide some sample data and expected output
Hi!

I would do two queries for this. One for the total cost and one to list the items.

select  tableA.OrderNO, orderDate, customer#, total cost, total tax, total discount, total shipping
 from tableA
  where customerno = ? and orderdate = 

Open in new window


select  tableA.OrderNO, orderDate, customer#,
 ,tableB.Item#, ItemDescription, ItemCost, ItemSalesPrice
from tableA
   join tableB on tableA.orderno = tableB.orderno
   where tableA.orderno = ? and customerno = ? 

Open in new window


You should  skip the columns customer# from the latter query as I put it only there for you to verify that the data in both queries matches.
I assume that you know the customerno and orderdate in the where clause I use and I use the orderno from the first query in the latter.

Regards,
    Tomas Helgi
ASKER CERTIFIED SOLUTION
Avatar of Rex
Rex

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ray

ASKER

I swear Rex read my mind!  Exactly what I was looking to do.  Thank you very much!

I appreciate everyone's input!