I have one input table, it has dates and a fixed number of events. I need to create, from it, the list of events with date of occurrence, and the list of combined events and their occurrence.
Example:
initial table:
CREATE TABLE events (
date DATE PRIMARY KEY,
e1 INTEGER,
e2 INTEGER,
e3 INTEGER
);
date | e1 | e2 | e3 |
--------------------------
2017-02-04 | 2 | 1 | 26 |
2017-02-05 | 14 | 2 | 1 |
2017-02-06 | 1 | 3 | 2 |
Output 1
eventN | total | date1 | date2 |...| date'N'
--------------------------------------------------------
01 | 3 | 2017-02-04 | 2017-02-05 |...| 2017-02-06
02 | 2 | 2017-02-05 | 2017-02-06 |...| (null)
...
26 | 1 | 2017-02-04 | (null) |...| (null)
Output 2
CobineEventN | total | date1 | ... | date'N'
-----------------------------------------------------
0102 | 2 | 2017-02-05 | ... | 2017-02-06
0103 | 1 | 2017-02-06 | ... | (null)
....
2526 | 1 | 2017-02-04 | ... | (null)
....
Limitations:
this has to be done in SQLite.
there is no limit for the dates (i.e. 'n' unique dates).
the events, are a fixed list of (around)50 ids
the output will be tables, one for each type of combination.
the author SQL skills.
After some talk with a teacher, he pointed out that my model was all wrong from start.
changing the table to:
REATE TABLE event (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, date date NOT NULL, event smallint unsigned NOT NULL);
*'id' is unnecessary
something like:
Id date event
31 2016-10-05 1
44 2016-10-07 1
32 2016-10-05 2
I could use a query like:
select A.event as nA, B.event as nB, C.event as nC, date
from event as A, event as B, event as C
where A.date = B.date
and B.date=C.date
and nC<>nA
and nC<>nB
and obtain the values needed.
nA nB nC date
1 2 3 2016-10-05
1 2 3 2016-10-07
1 2 4 2016-10-07
...
Although the format is not exactly what I had imagined, the results work fine.
And I don't need to create any more columns for the rest of the project, just have to do the right queries.
BR
Related
I have three data tables and they are given below. My first table name is product_table, the second table name is product_purchase_table, and the third table name is product_transfer_table. I'm trying to join these three tables like table name Table After Joint which is given below. My data will be joint once after another following the date like the given Table(Table After Joint).
product_table
row_id product_id product_name
1 101 Accounting Book
Product_Purchase_table
row_id product_id date quantity price
1 101 2020-10-25 100 1000
2 101 2020-10-29 200 2000
Product_transfer_table
row_id product_id date t_quantity t_price
1 101 2020-10-26 10 120
2 101 2020-10-27 15 180
3 101 2020-10-30 5 60
Table After Joint
row_id product_id product_name date quantity price t_quantity t_price
1 101 Accounting Book 2020-10-25 100 1000
2 101 Accounting Book 2020-10-26 10 120
3 101 Accounting Book 2020-10-27 15 180
4 101 Accounting Book 2020-10-29 200 2000
5 101 Accounting Book 2020-10-30 5 60
You need a FULL OUTER join of Product_Purchase_table and Product_transfer_table (emulated by LEFT joins and UNION ALL because SQLite does not support FULL OUTER join) and INNER join the result to product_table
SELECT row_number() over (partition by pt.product_id order by t.date) row_id,
pt.product_id, pt.product_name,
t.date, t.quantity, t.price, t.t_quantity, t.t_price
FROM product_table pt
INNER JOIN (
SELECT ppt.product_id, ppt.date, ppt.quantity, ppt.price,
ptt.t_quantity, ptt.t_price
FROM Product_Purchase_table ppt LEFT JOIN Product_transfer_table ptt
ON ptt.product_id = ppt.product_id AND ptt.date = ppt.date
UNION ALL
SELECT ptt.product_id, ptt.date, ppt.quantity, ppt.price,
ptt.t_quantity, ptt.t_price
FROM Product_transfer_table ptt LEFT JOIN Product_Purchase_table ppt
ON ptt.product_id = ppt.product_id AND ptt.date = ppt.date
WHERE ppt.product_id IS NULL
) t ON t.product_id = pt.product_id
ORDER BY pt.product_id, t.date
See the demo.
Results:
> row_id | product_id | product_name | date | quantity | price | t_quantity | t_price
> -----: | ---------: | :-------------- | :--------- | -------: | ----: | :--------- | :------
> 1 | 101 | Accounting Book | 2020-10-25 | 100 | 1000 | null | null
> 2 | 101 | Accounting Book | 2020-10-26 | null | null | 10 | 120
> 3 | 101 | Accounting Book | 2020-10-27 | null | null | 15 | 180
> 4 | 101 | Accounting Book | 2020-10-29 | 200 | 2000 | null | null
> 5 | 101 | Accounting Book | 2020-10-30 | null | null | 5 | 60
I need convert columns to rows in Teradata without TD_UNPIVOT. My table
ID |Code_1 | Code_2 | Code_3 | Code_4|
1 |1000 | 2000 | 3000 | 4000 |
1 |1000 | 2000 | 3000 | NULL |
1 |1000 | 2000 | NULL | NULL |
1 |1000 | NULL | NULL | NULL |
I need to convert Code_1, Code_2, Code_3, Code_4 to 2 columns: first column will have all Code_n (without NULL), second one will have Level of Code:
ID | Code_n | Level_of_Code
1 | 4000 | 4
1 | 3000 | 3
1 | 2000 | 2
1 | 1000 | 1
It means, than I should know when Code has NULL (in which level Code_1, Code_2, Code_3 or Code_4 and after that convert it to columns with numbers of max level where I have not NULL).
Please help me.
Thank you
You can produce rows by using multiple select statements and doing a union all to concatenate them together.
Select id, code_1 as "code_n", 1 as "level_of_code" from your table
Union all
Select id, code_2,2
Union all
Select id, code_3,3
Union all
Select id, code_4,4;
I have the following table in an sqlite database
+----+-------------+-------+
| ID | Week Number | Count |
+----+-------------+-------+
| 1 | 1 | 31 |
| 2 | 2 | 16 |
| 3 | 3 | 73 |
| 4 | 4 | 59 |
| 5 | 5 | 44 |
| 6 | 6 | 73 |
+----+-------------+-------+
I want to get the following table out. Where I get this weeks sales as one column and then the next column will be last weeks sales.
+-------------+-----------+-----------+
| Week Number | This_Week | Last_Week |
+-------------+-----------+-----------+
| 1 | 31 | null |
| 2 | 16 | 31 |
| 3 | 73 | 16 |
| 4 | 59 | 73 |
| 5 | 44 | 59 |
| 6 | 73 | 44 |
+-------------+-----------+-----------+
This is the select statement i was going to use:
select
id, week_number, count,
(select count from tempTable
where week_number = (week_number-1))
from
tempTable;
You are comparing values in two different rows. When you are just writing week_number, the database does not know which one you mean.
To refer to a column in a specific table, you have to prefix it with the table name: tempTable.week_number.
And if both tables have the same name, you have to rename at least one of them:
SELECT id,
week_number,
count AS This_Week,
(SELECT count
FROM tempTable AS T2
WHERE T2.week_number = tempTable.week_number - 1
) AS Last_Week
FROM tempTable;
In case of you want to take a query upon a same table twice, you have to put aliases on the original one and its replicated one to differentiate them
select a.week_number,a.count this_week,
(select b.count from tempTable b
where b.week_number=(a.week_number-1)) last_week
from tempTable a;
The first thing is my English is basic. Sorry.
Second thing, and the most important here: I can't find the way to do a simple query. My table is like this:
------------------------------------------
id_det_iti | id_iti | orden_iti| id_ciudad
--------------------------------------------
1 | 1 | 1 | 374
2 | 1 | 2 | 25
3 | 1 | 3 | 241
4 | 2 | 1 | 34
5 | 2 | 2 | 22
6 | 2 | 3 | 352
7 | 2 | 4 | 17
--------------------------------------------
Then, I wanna get results like this:
------------------------------------------
id_iti | min | id_ciudad | max | id_ciudad
------------------------------------------
1 | 1 | 374 | 3 | 241
2 | 1 | 34 | 4 | 17
------------------------------------------
I need to show the max and the min value in the same row group by id_iti.
I have tried to use full join, but I'm working with sqlite, and that's not an option. I spend a long day trying with different options but I can't found the solution. I hope you guys can help me.
Thanks in advance!
Edit:
SELECT a.id_iti, c.id_ciudad, d.id_ciudad
FROM detalle_itinerario as a,
(SELECT MAX(orden_iti),id_ciudad, id_iti FROM detalle_itinerario) AS c
INNER JOIN
(SELECT MIN(orden_iti),id_ciudad, id_iti FROM detalle_itinerario) AS d
ON c.id_iti=d.id_iti
GROUP BY a.id_iti;
That's only one of my attempts, but I get just values of the first coincidence.
First, use a simple query to get the min/max values for each group:
SELECT id_iti,
MIN(orden_iti) AS min,
MAX(orden_iti) AS max,
FROM detalle_itinerario
GROUP BY id_iti;
You can the use these values to join back to the original table:
SELECT a.id_iti,
a.min,
a2.id_ciudad,
a.max,
a3.id_ciudad
FROM (SELECT id_iti,
MIN(orden_iti) AS min,
MAX(orden_iti) AS max
FROM detalle_itinerario
GROUP BY id_iti) AS a
JOIN detalle_itinerario AS a2 ON a.id_iti = a2.id_iti AND a.min = a2.orden_iti
JOIN detalle_itinerario AS a3 ON a.id_iti = a3.id_iti AND a.max = a3.orden_iti;
I want to use SELECT * from multiple tables table1 and table2 to get the output table which is nothing but table2 data appended to table1. How do I construct the SELECT * FROM statement ?
Table1:
id model datetime driver distance
---|-----|------------|--------|---------
1 | S | 04/03/2009 | john | 399
2 | X | 04/03/2009 | juliet | 244
3 | 3 | 04/03/2009 | borat | 555
Table2:
id model datetime driver distance
---|-----|------------|--------|---------
4 | 3 | 03/03/2009 | john | 300
5 | X | 03/03/2009 | juliet | 200
Desired output:
model datetime driver distance
-----|------------|--------|---------
S | 04/03/2009 | john | 399
X | 04/03/2009 | juliet | 244
3 | 04/03/2009 | borat | 555
3 | 03/03/2009 | john | 300
X | 03/03/2009 | juliet | 200
Try this out this might help you
SELECT table1.model, table1.datetime, table1.driver, table1.distance FROM table1
UNION ALL SELECT table2.model, table2.datetime, table2.driver, table2.distance FROM table2;
union statement get slow for fetching the large data from the db, you can use join here,
select * from table0 left join table2 on table0.b = table2.b where table2.col is not null