sql server - Crystal Reports or T-SQL: Comparing multiple records values based on values in another table -
i attempting compare , perform calculations on sets of values have multiple entries in crystal reports or t-sql (ms sql server) , calculation.
example data:
table (millions of rows)
entry id 1, vendor, product id, inventory count, inventory value entry id 2, vendor, product id, inventory count, inventory value ... entry id 99, vendor, product id, inventory count, inventory value
table b: (millions of rows)
entry id 1, datetime, entrytype entry id 2, datetime, entrytype ... entry id 99, datetime, entrytype
an entry created in both tables every transaction instance , both tables 5 million plus rows, entry several batch operations occur on schedules , maintenance activities.
i need compare 2 values in table , calculate difference in values of inventory count & inventory value, between recent date time or entry id update in table b (max) regardless of table b entry type , last table b value of type "10" vendor & product id in table a.
so if entry 1 of type '10' , entry 99 of type 62, subtract values of entry 99 values of entry 1 come current inventory counts , values.
this how vendor choose implement in software , looking way information outside of application.
a crystal report way highly preferred, t-sql solution work me long returns products or doesn't require hard coding product ids , default return complete inventory unless further filtered in statement or crystal reports filter.
i running crystal reports 2011 ms sql server 2008 sp3
give query try. runs in few seconds on 3 million rows of random test data (having right indexes on tables important). called table 'inventory' , table b 'entry'.
select x.vendor, x.productid, i2.inventorycount - i.inventorycount inventorycountdiff, i2.inventoryvalue - i.inventoryvalue inventoryvaluediff ( select i.vendor, i.productid, max(e.entrydate) maxentrydate, (select max(e2.entryid) entry e2 join inventory i2 on i2.entryid = e2.entryid , i2.vendor = i.vendor , i2.productid = i.productid e2.entrytype = 10) maxentryid inventory join entry e on e.entryid = i.entryid group i.vendor, i.productid ) x join inventory on x.vendor = i.vendor , x.productid = i.productid join entry e on i.entryid = e.entryid join inventory i2 on x.vendor = i2.vendor , x.productid = i2.productid join entry e2 on i2.entryid = e2.entryid e.entrydate = x.maxentrydate , e2.entryid = x.maxentryid
whatever query end using, favor , put in stored procedure , use procedure source report. crystal reports painful enough use without trying compose complex query in designer.
Comments
Post a Comment