Monday, March 12, 2012

Need Report select statement help CR9

i need to report for a center (a location #) within a date range where item #1 and item #2's qty's are not the same. The center and date range is fine

"{DS_DATA.DBCTR} = 50 and
{DS_DATA.DBDATE} in DateTime (2006, 02, 20, 0, 0, 0) to DateTime (2006, 03, 19, 0, 0, 0)"

it is the the item's that i am stuck on. How can I select where the center = 50 the date is between x and y, and the qty's for items 1 and 2 are NOT the same on the same day for the same location. I keep envisioning in sql string terms like a select statement within a select statement but you cant do that in crystal. at least I dont think. HELP!It might help if we knew your table / data structure.

I assume that there are multiple rows per DBCTR, DBDATE pair i.e. that there's an item column and a quantity column.
I assume that the DS_DATA table's key is DBCTR, DBDATE, ITEM_NO.
I assume that the DBDATE column is a date (rather than a datetime with a non-zero time).

So, the SQL is something like

Select t1.dbctr, t1.dbdate, t1.qty, t2.qty
from ds_data t1
inner join ds_data t2 on t2.DBCTR = t1.DBCTR and t2.DBDATE = t1.DBDATE
and t1.item_no = 1 and t2.item_no = 2
where t1.qty <> t2.qty
and ...

(Yes, I did mean to put the item_no restrictions on the join - I've no idea how big your table is or how many items there could be and it limits the cartesian product before where clause filtering.)

So you could either put the SQL straight into the 'Add command' or do it via the graphical interface: add the table in the database expert twice (the 2nd time it'll be given a different name), add the inner join (just the column joins here) as links and then add the item_no and qty restrictions to your record selection formula, using the correctly aliased table.|||It didn't even dawn on me to put the table in twice and do the join. It was stairing me right in the face. I actually tried using the repository and used this sql statement:
select d.dscenter, d.DSDATE, d.dsvoid, s.storevoid from
(select DBCTR DSCENTER, DBDATE DSDATE, dbqty DSVOID from ds_user.ds_data where dbitem = 11200 )D ,
(select DBCTR STORECTR, DBDATE STOREDATE, dbqty STOREVOID from ds_user.ds_data where dbitem = 21200 )S
where
d.dsdate = s.storedate
and
d.dscenter = s.storectr
and
d.dsvoid <> s.storevoid

it worked too and the speed was no different. Thanks for the help

No comments:

Post a Comment