I was trying to understand how history tables work in HANA.
I have a query whether the following requirement can be achieved using history table.
Suppose, I've a table which maintains site and article wise STOCK.
The STOCK gets updated according to the change in inventory.
Now, if I want the current as well as the previous picture of site-article wise stock can I use the history table for same.
I created a history table and inserted a row in it which gives output :
SELECT site,article,stock FROM PH_STOCK_HISTORY AS OF utctimestamp '2019-05-22 12:36:17.909';
I updated the stock and re-run the query :
SELECT site,article,stock FROM PH_STOCK_HISTORY AS OF utctimestamp '2019-05-22 12:37:38.55'
Now, I want to get the earlier and the current picture of my stock in the same query.
Is that feasible to achieve this ?
May be you can achieve this by Union query.
SELECT site,article,stock FROM PH_STOCK_HISTORY AS OF utctimestamp '2019-05-22 12:36:17.909'
union
SELECT site,article,stock FROM PH_STOCK_HISTORY AS OF now();
Try this :)
Related
I have a table that will get refreshed every Friday on a weekly basis, I need to place a check to make sure , the records that is available in the table is the latest refreshed records before we consume the data.
What is the best way to check using ORACLE SQL query.
The check query could be as follows
select min(update_date) from your_table
If the result is less that the last expected refresh date, you see that at least some rows were not updated.
I created a table named user_preferences where user preferences have been grouped by user_id and month.
Table:
Each month I collect all user_ids and assign all preferences:
city
district
number of rooms
the maximum price they can spend
The plan assumes displaying a graph showing users' shopping intentions like this:
The blue line is the number of interested users for the selected values in the filters.
The graph should enable filtering by parameters marked in red.
What you see above is a simplified form for clarifying the subject. In fact, there are many more users. Every month, the table increases by several hundred thousand records. The SQL query retrieving data (feeding) for chart lasts up to 50 seconds. It's far too much - I can't afford it.
So, I need to create a table (table/aggregation/data mart) where I will be able to insert the previously calculated numer of interested users for all combinations. Thanks to this, the end user will not have to wait for the data to count.
Details below:
Now the question is - how to create such a table in PostgreSQL?
I know how to write a SQL query that will calculate a specific example.
SELECT
month,
count(DISTINCT user_id) interested_users
FROM
user_preferences
WHERE
month BETWEEN '2020-01' AND '2020-03'
AND city = 'Madrid'
AND district = 'Latina'
AND rooms IN (1,2)
AND price_max BETWEEN 400001 AND 500000
GROUP BY
1
The question is - how to calculate all possible combinations? Can I write multiple nested loop in SQL?
The topic is extremely important to me, I think it will also be useful to others for the future.
I will be extremely grateful for any tips.
Well, base on your query, you have the following filters:
month
city
distirct
rooms
price_max
You can try creating a view with the following structure:
SELECT month
,city
,distirct
,rooms
,price_max
,count(DISTINCT user_id)
FROM user_preferences
GROUP BY month
,city
,distirct
,rooms
,price_max
You can make this view materialized. So, the query behind the view will not be executed when queried. It will behave like table.
When you are adding new records to the base table you will need to refresh the view (unfortunately, posgresql does not support auto-refresh like others):
REFRESH MATERIALIZED VIEW my_view;
or you can scheduled a task.
If you are using only exact search for each field, this will work. But in your example, you have criteria like:
month BETWEEN '2020-01' AND '2020-03'
AND rooms IN (1,2)
AND price_max BETWEEN 400001 AND 500000
In such cases, I usually write the same query but SUM the data from the materialized view. In your case, you are using DISTINCT and this may lead to counting a user multiple times.
If this is a issue, you need to precalculate too many combinations and I doubt this is the answer. Alternatively, you can try to normalize your data - this will improve the performance of the aggregations.
Im currently working with Oracle APEX and have some problems.
I have an inventory table and a bike table. I want to assign a part to a bike. So I also have a bike_with_inventory table which contains the ID of the bike, the ID of the inventory and the bike_with_inventory ID.
Now in APEX, I have a Form on the bike_with_inventory table where I select a bike and a part for the bike. I made a form on a table and its working. The only problem is that if the part already is in the bike it generates a new ID in the bike_with_inventory table instead of updating it. I tried to do it with a merge
MERGE INTO bike_with_inventory bwi
USING (SELECT ID, targetAmount FROM bike_with_inventory) i
ON (bwi.ID = i.ID)
WHEN MATCHED THEN
UPDATE SET bwi.targetAmount =bwi.targetAmount + :P17_TARGET_AMOUNT;
But if I process this Merge it does what I want. But also generates a new ID. So it updates the selected one with e.g 5 and also generates an ID with targetAmount 5.
Can someone help me with this ? I hope I could explain it, so that youre able to understand my problem. Thank you a lot. PS im using APEX 5.1. But I guess also non APEX can understand that i guess its not that hard of a problem.
I have been struggling with creating a query in Access to select a distinct field with the criteria of having the newest entry in the database.
Heres a brief summary of how what my table conssists of. I have a table with surveying data collected from 2007 to the present. We have field with a survey marks name with corresponding adjustment data. In the corresponding data there is field with the adjusmtent date. Many of the marks have been occupied mutiple times and only want to retrieve the most recent occupation information.
Roughly i want to
SELECT DISTINCT STATUS_POINT_DESIGNATION
FROM __ALL_ADJUSTMENTS
WHERE [__ALL_ADJUSMENTS]![ADJ_DATE]=MAX(ADJ_DATE)
I seem to be getting confused how relate the select a distinct value with a constraint. Any Suggestions?
DH
Seems you could achieve your aim of getting the latest observation for each survey point by a summary function:
SELECT STATUS_POINT_DESIGNATION, Max(ADJ_DATE) AS LatestDate, Count(STATUS_POINT_DESIGNATION) AS Observations
FROM __ALL_ADJUSTMENTS
GROUP BY STATUS_POINT_DESIGNATION;
I would like help with this sql. I need to find the latest date for each circuit ID (field name-strip_ec_circuit_id) based on a created date(field name-create_bill_date). I need to only find the latest date, while the other ones can be deleted. Can you help me do this?
This is a basic group by query that should work in any database:
select strip_ec_circuit_id, max(create_bill_date) as lastDate
from t
group by strip_ec_circuit_id
I'm not sure what you mean by delete all the others. Do you actually want to delete the rows from the table that are not the max?