Does DB2 Support temporal tables join and aggregation? - sql

I am looking for a resource which indicates whether Db2 on Ubuntu supports temporal tables join and aggregation or not.
Example:
customer (
customer_id ,
name ,
address ,
valid_period ,
tx_period
);
orders (
order_id ,
customer_id,
order_total,
valid_period ,
tx_period
);
Does DB2 support temporal join for these kind of tables on period fields?

The Db2 documentation has information on restrictions for system-period temporal tables. They also apply for bi-temporal tables. Next, look at querying bi-temporal tables. It is combining predicates on application periods and system periods.

Related

SQL to find unique counts between two date fields

I was reading this but can't manage to hack it to work on my own problem.
My data has the following fields, in a single table in Postgres:
Seller_id (varchar) (contains_duplicates).
SKU (varchar) (contains duplicates).
selling_starts (datetime).
selling_ends (datetime).
I want to query it so I get the count of unique SKUs on sale, per seller, per day. If there are any null days I don't need these.
I've tried before querying it by using another table to generate a list of unique "filler" dates and then joining it to where the date is more than the selling_starts and less than the selling_ends fields. However, this is so computationally expensive that I get timeout errors.
I'm vaguely aware there are probably more efficient ways of doing this via with statements to create CTEs or some sort of recursive function, but I don't have any experience of this.
Any help much appreciated!
try this :
WITH list AS
( SELECT generate_series(date_trunc('day', min(selling_starts)), max(selling_ends), '1 day') AS ref_date
FROM your_table
)
SELECT seller_id
, l.ref_date
, count(DISTINCT sku) AS sku_count
FROM your_table AS t
INNER JOIN list AS l
ON t.selling_starts <= l.ref_date
AND t.selling_ends > l.ref_date
GROUP BY seller_id, l.ref_date
If your_table is large, you should create indexes to accelerate the query.

How to create a nested table on Google BigQuery and maintaining the same level of data aggregation

I am new in using BigQuery.
I have two tables FBA Master and a Country Table. Each table shows the same results but at a different aggregation level. The FBA Master table has data aggregated by Platforms and the Country table has data aggregated by countries.
I am looking to join the FBA master table with the country table by Account id ,Ad id and Campaign Id. Hence, importing the country data into the FBA master table and maintaining the same level of aggregation of the FBA master table.
Could you please help with this? I was trying to create a nested script on big query by following some tutorials online but with no results.
See attached the link to an example of the data set and my objective table.
https://docs.google.com/spreadsheets/d/1_GNgLN3_AMW0XExZMEWnWEbLhfma5o3uU2cc-G30Y_M/edit?usp=sharing
Since your joining is based on Account id ,Ad id and Campaign Id, you have to ensure there are no duplicates on both the sides (FBA_Master as well as Country_Master)
My solution would be this way
SELECT A.* EXCEPT(PLATFORM), B.* EXCEPT(COUNTRY), STRUCT(PLATFORM,COUNTRY) FROM
(SELECT ACCOUNT_ID, AD_ID, CAMPAIGN_ID , STRING_AGG(DISTINCT PLATFORM ORDER BY PLATFORM) PLATFORM) A
LEFT JOIN
(SELECT ACCOUNT_ID, AD_ID, CAMPAIGN_ID , STRING_AGG(DISTINCT COUNTRY ORDER BY COUNTRY) COUNTRY) B
ON A.ACCOUNT_ID = B.ACCOUNT_ID AND A.AD_ID = B.AD_ID AND A.CAMPAIGN_ID=B.CAMPAIGN_ID

Which of these is preferable? Adding a column to the table or using sub-query to get data?

What I meant was, there's a Table A having 5 columns. I have a SP that I use to get 3 columns from Table A and one column from Table B. Now, would it be better to add the column from Table B to Table A or use a sub-query in that SP to get that column from Table B?
Your question is still confusing and it very much looks like you haven't understood how to use a relational database. So let me try to explain:
Let's say you have two tables:
client
client_number
first_name
last_name
date_of_birth
...
order
order_number
client_number
order_date
...
These are two separate tables, so as to have a normalized relational database. The order table contains the client number, so you can look up the clients name and date of birth in the client table. The date of birth may be important in order know whether the client is allowed to order certain articles. But you don't want to store the date of birth with every order - it doesn't change.
If you want to look up the age you can use a sub query:
select
order_number,
order_date,
quantity,
(
select date_of_birth
from client c
where c.client_number = o.client_number
)
from order o
where item = 'whisky';
but most often you would simply join the tables:
select
o.order_number,
o.order_date,
o.quantity,
c.date_of_birth,
c.first_name,
c.last_name
from order o
join client c on c.client_number = o.client_number;
You would however not change your tables and invite redundancy with all its problems only not to have to join. You design your database such that it is a well-formed relational database, not such that it makes your latest query easy to write. It is very, very common to use joins and subqueries, and having to use them usually shows that you built your database well.
I think this a good time you look up database normalization, e.g. in Wikipedia https://en.wikipedia.org/wiki/Database_normalization.

Performance impact due to Join

I have two tables PRODUCT and ACCOUNT.
PRODUCT table columns
product_id (PK)
subscription_id
ACCOUNT table columns
account_nbr
subscription_id
(account_nbr and subscription_id are primary key columns in account table)
... Other columns
I have to find account_nbr and subscription_id for a product_id.
I get product_id as input. Using it I can get the subscription_id from PRODUCT table and
using subscription_id I can get the account_nbr value from ACCOUNT table.
Instead of getting the info in two queries, can it be done in one query?
Something like the below:
select distinct a.acct_nbr,p.subscription_id
from ACCOUNT a,PRODUCT p
where v.product_id = ' val 1' and
v.subscription_id = p.subscription_id
Will the performance of the above query be low compared to two separate queries?
I have to find account_nbr and subscription_id for a product_id.
So, you're correct in your approach you need to JOIN the two result-sets together:
select p.account_nbr, p.subscription_id
from account a
join product p
on a.subscription_id = p.subscription_id
where p.product_id = :something
Some points:
You have an alias v in your query; I don't know where this came from.
Learn to use the ANSI join syntax; if you make a mistake it's a lot more obvious.
You're selecting a.acct_nbr, which doesn't exist as-is.
There's no need for a DISTINCT. ACCOUNT_NBR and PRODUCT_ID are the primary key of ACCOUNT.
Will the performance of the above query be low compared to two separate queries?
Probably not. If your query is correct and your tables are correctly indexed it's highly unlikely that whatever you've coded could beat the SQL engine. The database is designed to select data quickly.

How to reconcile common rows in two tables columnwise in oracle?

I have two tables in a database. The tables are called bi_employee and hr_employee. These two tables have few similar columns and then other extra columns specific to each table.
I need to reconcile the data between these two tables based on some user defined columns.
Lets say the common columns are id(pk), emp_code, region, country, title, division etc.
Now when I reconcile these two tables, I would want to see the rows which are there in both the tables but only differ in some columns.
e.g. emp_code 1000 is prsent in both the tables but in hr_employee his title is jr. developer but in bi_employee his title is sr.developer.
I do not want the records which are in one table but not in another table.
I only need to reconcile the rows which are present in both the tables but on columnwise which will be selected by the user.
User may chose to reconcile based on title or region or country or all of them.
Please help.
EDIT 1:
This is what I have done so far, with the following query I could get all the records which are there in both the tables. Now I just need to compare their columns to see if there are any mismatches.
SELECT emp_code FROM bi_employee INTERSECT SELECT emp_code FROM hr_employee
From what I understand, there is only one column that relates the recodes in each table; emp_code. Then you want to show records where the emp_code is the same in each talbe, but other field(s) are different.
You can do that with a simple join and filter in a WHERE clause...
SELECT
*
FROM
bi_employee
INNER JOIN
hr_employee
ON bi_employee.emp_code = hr_employee.emp_code
WHERE
(bi_employee.title <> hr_employee.title)
OR (bi_employee.region <> hr_employee.region)
etc, etc
(If any fields are nullable, you'll need to account for that with something like ISNULL(bi.x, '') <> ISNULL(hr.x, '')).
You might try this.
select hr.<list of columns to reconcile from hr table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode
minus
select bi.<list of columns to reconcile from bi table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode