0

I'm having a heck of a time figuring out how to write this query and im hoping someone can point me in the right direction.

I have a system to manage assets and there are two tables relevant to this question. item, which has one record for each asset held, then item_activity which stores a record each time an item to checked in or checked out of the storage area.

I need to produce a report of of items held at any point for a date range (IE how many items did our storage see in a year). In other words I need to see all items that were stored in storage in a year, no matter how long they were there (a status of Checked In). It's easy to look at my activity table and look for a status of checked in over a date range. What i'm having trouble with is capturing the items that were checked in before my date range started and didn't move at all over that year.

Here is my schema

item

  • id (primary key)
  • make
  • model
  • serial_number
  • type
  • value
  • etc...

item_activity

  • activity_id
  • item_id (relates to the item table)
  • status (Checked In, Checked Out, Assigned, etc)
  • timestamp
  • user
  • notes

I'm sure someone has run into this scenario before, but I can't seem to find any previous questions on this, or possibly don't know the right words to use to locate the question. Thank you for your help! I'm very new to SQL.

3
  • 1
    Could you please elaborate more what exactly needs to be reported? I couldn't understand it. Jan 18, 2016 at 14:36
  • So you want all items that where in storage at least once in the year, no matter if they were available all year, two consecutive weeks or every other month. Yes? Jan 18, 2016 at 14:37
  • Correct @ThorstenKettner. Sorry I didn't explain that very well.
    – cdanik
    Jan 18, 2016 at 14:38

2 Answers 2

0

All activity......

SELECT * 
FROM Item_Activity A
INNER JOIN Item I ON I.id = A.Item_ID
WHERE
   A.timestamp BETWEEN @YourStartDate AND @YourEndDate;

All items that had activity between the 2 dates specifeid......

SELECT I.* 
FROM Item I
INNER JOIN Item_Activity A ON I.id = A.Item_ID
WHERE
   A.timestamp BETWEEN @YourStartDate AND @YourEndDate;
0

You are right about finding those items that had an action in that year. That's simple:

select distinct item_id
from item_activity
where status in ('Checked In', 'Checked out')
and year(timestmp) = 2015;

So we miss those items that where checked in but not checked out afterwards before 2015 (i.e. last action before 2015 was check-in).

select item_id
from
(
  select item_id, status
  from item_activity
  where status in ('Checked In', 'Checked out')
  and year(timestmp) < 2015
  order by timestmp desc limit 1 -- latest record before 2015
) last_item_action
where status = 'Checked In'; -- latest action was check-in

Both queries combined:

select item_id
from item_activity
where status in ('Checked In', 'Checked out')
and year(timestmp) = 2015
union
select item_id
from
(
  select item_id, status
  from item_activity
  where status in ('Checked In', 'Checked out')
  and year(timestmp) < 2015
  order by timestmp desc limit 1
) last_item_action
where status = 'Checked In';

(In this combined query you can even limit the first query to where status = 'Checked In', because we don't care any longer about check-outs here. That was just for items checked in before 2015 which we find now with the second query.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.