Reputation: 497
I given task for stock entries management where stock can be either three types(1-supplier, 2-Companies, 3-customer),
i have 2 tables(A and B) to maintain it,A use to track current status, and B used as history table to track the movement
initial will be stock type 1, so inserted into A, if company buy it, table B will inserted from table A for that record and will updated stock type in table A,
so to get closing stock balance till a particular date i can read from table A,
but having difficulty to get opening stock balance where i have to read from table B
can anyone help, sorry if my question confusing
structure of the table:
TABLE A
NUM_REF CHAR (6) NOT NULL,
BRANCH CHAR (5) NOT NULL,
STOCK_TYPE CHAR (3) NOT NULL,
INSERT_DATE TIMESTAMP NOT NULL
TABLE B
NUM_RIF CHAR (6) NOT NULL,
BRANCH CHAR (5) NOT NULL,
STOCK_TYPE CHAR (3) NOT NULL,
INSERT_DATE CHAR(2) NOT NULL,
UPDATE_DATE TIMESTAMP NOT NULL
record from table A
000295 10400 03 10/24/2011 5:11:12 PM 10/11/2011 12:27:21 PM
000338 10400 02 10/14/2011 7:51:56 PM 10/13/2011 12:54:28 PM
000399 10400 03 10/24/2011 6:25:53 PM 10/21/2011 6:13:20 PM
000400 10400 02 10/14/2011 8:06:21 PM 10/14/2011 4:21:37 PM
000410 10400 03 10/18/2011 5:17:36 PM 10/18/2011 5:17:36 PM
000416 10400 03 10/18/2011 5:27:19 PM 10/18/2011 5:27:19 PM
000420 10400 03 10/25/2011 6:18:35 PM 10/17/2011 3:01:49 PM
000423 10400 03 10/25/2011 6:18:35 PM 10/17/2011 4:10:00 PM
000450 10400 03 10/19/2011 4:45:38 PM 10/18/2011 2:30:40 PM
record from table B
000284 10400 01 10/10/2011 5:49:15 PM
000288 10400 01 10/11/2011 12:22:50 P
000289 10400 01 10/11/2011 12:23:35 P
000295 10400 01 10/11/2011 12:27:21 P
000295 10400 03 10/13/2011 2:47:33 PM
000295 10400 03 10/13/2011 2:58:31 PM
000295 10400 03 10/13/2011 3:22:19 PM
000295 10400 03 10/13/2011 4:34:07 PM
000295 10400 03 10/13/2011 4:41:08 PM
000295 10400 03 10/13/2011 4:41:40 PM
000295 10400 03 10/13/2011 4:45:05 PM
000295 10400 03 10/13/2011 4:46:36 PM
000295 10400 03 10/13/2011 4:56:03 PM
000295 10400 03 10/13/2011 4:58:47 PM
000295 10400 03 10/14/2011 11:15:47 A
000295 10400 03 10/14/2011 5:45:33 PM
000296 10400 01 10/11/2011 12:27:21 P
000328 10400 01 10/13/2011 12:20:49 P
000328 10400 03 10/14/2011 12:31:07 P
000328 10400 03 10/17/2011 3:41:45 PM
000328 10400 03 10/18/2011 12:14:21 P
000328 10400 02 10/20/2011 4:41:39 PM
000331 10400 01 10/13/2011 12:25:02 P
000331 10400 03 10/14/2011 3:03:31 PM
000331 10400 03 10/17/2011 3:54:02 PM
000333 10400 01 10/13/2011 12:33:19 P
000333 10400 03 10/14/2011 4:01:59 PM
000333 10400 03 10/18/2011 11:06:18 A
000337 10400 01 10/13/2011 12:52:23 P
000338 10400 01 10/13/2011 12:54:28 P
000369 10400 01 10/14/2011 12:33:43 P
000369 10400 02 10/14/2011 8:05:03 PM
000369 10400 03 10/17/2011 2:42:19 PM
000369 10400 03 10/18/2011 2:24:56 PM
000399 10400 01 10/21/2011 6:13:20 PM
000400 10400 01 10/14/2011 4:21:37 PM
000420 10400 01 10/17/2011 3:01:49 PM
000420 10400 03 10/17/2011 3:13:07 PM
000420 10400 03 10/25/2011 6:16:43 PM
000423 10400 01 10/17/2011 4:10:00 PM
000423 10400 03 10/25/2011 6:16:43 PM
000424 10400 01 10/17/2011 4:22:04 PM
000424 10400 03 10/25/2011 6:42:06 PM
000425 10400 01 10/17/2011 4:24:41 PM
000425 10400 03 10/25/2011 6:42:06 PM
Upvotes: 0
Views: 33609
Reputation: 4663
select * from tablea order by insert_date desc fetch first 1 row only for read only with ur
Upvotes: 0
Reputation: 11
Have you tried Max(date)
to get the last record? Alternately, you can use Min(date)
for earliest date.
I have limited the search to last date of a month closing and using max(date)
to get last entry up to the closing date.
Select item, max(inventorydate)
from table
where inventorydate is < '6/30/2011'
Upvotes: 1
Reputation: 133
SELECT StockId, Max(Date)
FROM (SELECT StockId, max(Date)
FROM TableA
GROUP BY StockId
UNION ALL
SELECT StockId, max(Date)
FROM TableB
GROUP BY StockId) a
GROUP BY StockId
Upvotes: 0