canonquiche
Registered User.
- Local time
- Yesterday, 23:34
- Joined
- Jan 5, 2016
- Messages
- 11
I have a server inventory table that gets updated monthly. The fields include server name, date, cost, etc.
What I want is a query that identifies any servers that have been added or removed since the previous month (added or removed in the sense of they were used or not used in the current month). It would look something like this:
current_server | previous_server | current_date | previous_date
xyz000000001 | ------------- | Jan 2016 | Dec 2015
xyz000000002 | ------------- | Jan 2016 | Dec 2015
------------ | abc0000000001 | Jan 2016 | Dec 2015
In other words, current_server indicates servers that have been added. Conversely, previous_server indicates servers that have been removed.
For the other fields in the table (like cost), I am able to achieve something like this using an inner join query to find cost changes. But I can't seem to figure out how to do the same thing for changes in the servers.
What I want is a query that identifies any servers that have been added or removed since the previous month (added or removed in the sense of they were used or not used in the current month). It would look something like this:
current_server | previous_server | current_date | previous_date
xyz000000001 | ------------- | Jan 2016 | Dec 2015
xyz000000002 | ------------- | Jan 2016 | Dec 2015
------------ | abc0000000001 | Jan 2016 | Dec 2015
In other words, current_server indicates servers that have been added. Conversely, previous_server indicates servers that have been removed.
For the other fields in the table (like cost), I am able to achieve something like this using an inner join query to find cost changes. But I can't seem to figure out how to do the same thing for changes in the servers.
Code:
SELECT prev.server_name, prev.server_cost, cur.server_cost, (cur.server_cost-prev.server_cost) AS server_cost_increase, cur.date AS [current], prev.date AS previous
FROM Invoice_ABC AS cur INNER JOIN Invoice_ABC AS prev ON cur.server_name = prev.server_name
GROUP BY prev.server_name,, prev.server_cost, cur.server_cost, (cur.server_cost-prev.server_cost), cur.date, prev.date, cur.server_name
HAVING ((cur.date)=(SELECT Max([cur].[date]) from Invoice_ABC)) AND ((prev.date)=(SELECT Dateadd("m", -1, Max(cur.date)) FROM Invoice_ABC)
ORDER BY cur.date DESC , cur.server_name;