Sql query for multiple tables of SQLite - sql

I have an SQL database (pq) with 3 tables as sample is shown in image. I am trying to achieve below things:
Select only rows with variable P from the MEASUREMENT column in Table 3. I tried below query but it didnt produced the correct output.
select distinct pq_data.READ_TIME,OBS_METER,MEASUREMENT,VALUE from pq_data ORDER BY MEASUREMENT;
Then, fetch the data columns CUST_CLASS, and SOLAR from Table 1 into Table 3 according to OBS_METER id. The OBS_METER column is not available in Table 1 but the it can be obtained from OBS_LOCATION in Table 2.
The expected output of SQL query is Table 3 with additional columns from other tables,such as:
row id READ_TIME OBS_METER OBS_LOCATION MEASUREMENT VALUE CUST_CLASS SOLAR
28/01/2018 2018/01/28 01:55:00 105714 6787 P 284 R F
..........
I searched for existing answers: 1 , 2 but I couldnt able to write a SQL query which will produce above expected output.

Select only rows with variable P from the MEASUREMENT column in Table 3.
select * from pq_data WHERE MEASUREMENT='P';
Then, fetch the data columns CUST_CLASS, and SOLAR from Table 1 into
Table 3 according to OBS_METER id.
select *
from pq_data pd
inner join meter_mapping mm on pd pd.obs_meter=mm.obs_meter
inner join location_mapping lm on mm.obs_location=lm.obs_location
WHERE pd.MEASUREMENT='P'
The expected output of SQL query is Table 3 with additional columns
from other tables:
You did not specify which table is the rowid that you wanted, I assumed that it was from pq_data.
Also, I don't know if an entry on pq_data will always have a match in meter_mapping (and location_maping). If it don't you need to use "left join" (or right).
It would be easier if you used the actual name of the tables in your questions (instead of table 1, 2 and 3).
select pd.rowid, pd.READ_TIME, pd.OBS_METER, mm.OBS_LOCATION, pd.MEASUREMENT, pd.VALUE, lm.CUST_CLASS, lm.SOLAR
from pq_data pd
inner join meter_mapping mm on pd pd.OBS_METER=mm.OBS_METER
inner join location_mapping lm on mm.OBS_LOCATION=lm.OBS_LOCATION
WHERE pd.MEASUREMENT='P'

Related

Bigquery join 2 tables with id concated from 4 columns and create a new table dynamically

I have two tables in Bigquery from two different data sources, lets say x and y. I want to join these two tables on os_name, tracker_name, date, country columns. For that i am using concat function and joining like this:
full outer join x on concat(x.date,x.os_name,x.tracker_name, x.country) = concat(y.date,y.os_name,y.tracker_name,y.country_code)
as a query result common columns also gets duplicated. like in the result there is os_name and os_name_1, country_code, country_code_1 etc. columns. I don't want that. Final columns should be as in the example below in Final Table Schema.
I want to return all records from both sides. For example if there is no match in table y
y_install, and y_purcase will be 0, and vice versa.
X TABLE SCHEMA:
os_name,
tracker_name,
date ,
country
install
purchase
Y TABLE SCHEMA:
os_name,
tracker_name,
date,
country,
y_install,
y_purchase
Final Table Schema required:
os_name,
tracker_name,
date ,
country
install
purchase,
y_install,
y_purchase
I am going to schedule the query and write results to destination table at given interval.
Can you help me out with this query.
Regarding the final table, I don't understand whether you want to return first NON NULL result or whether you want to have e.g. an array which will contain both results from both tables in case both tables a valid value. In my sample table, do you want row 1,2 (actually the same thing) or 3?
row_number
x_install
y_install
final_table_install
1
23
50
23
2
NULL
50
50
3
23
50
[23,50]
It comes out that What I wanted to use was union all. First, I added the non-common columns to the two tables so that the schemas of the two tables are equal. So I was able to vertically merge tables using union all. Thanks for trying to help out anyway.

Oracle 11 SQL lists and two table comparison

I have a list of 12 strings (strings of numbers) that I need to compare to an existing table in Oracle. However I don't want to create a table just to do the compare; that takes more time than it is worth.
select
column_value as account_number
from
table(sys.odcivarchar2list('27001', '900480', '589358', '130740',
'807958', '579813', '1000100462', '656025',
'11046', '945287', '18193', '897603'))
This provides the correct result.
Now I want to compare that list of 12 against an actual table with account numbers to find the missing values. Normally with two tables I would do a left join table1.account_number = table2.account_number and table two results will have blanks. When I attempt that using the above, all I get are the results where the two records are equal.
select column_value as account_number, k.acct_num
from table(sys.odcivarchar2list('27001',
'900480',
'589358',
'130740',
'807958',
'579813',
'1000100462',
'656025',
'11046',
'945287',
'18193',
'897603'
)) left join
isi_owner.t_known_acct k on column_value = k.acct_num
9 match, but 3 should be included in table1 and blank in table2
Thoughts?
Sean

For MS Access SQL, want to use EXCEPT in Access for three columns in table1 to 1 column in table 2

I have 3 columns in table A. I am trying to design a query that will call out all the values (in the three columns) that do not apepar in the 1 column I have in table B. If it helps to make it more clear, table B is a list of currencies in ISO codes and table A is three columns of currencies being used, I am identifying all those values that are NOT using ISO codes to denote their currency.
Currently, I can't seem to get them all to match to the one column, so I made 2 more columns in table B so I can match them individually. My constraints are, I cannot change table A and I must do this in one query. What I got so far is below.
SELECT m.Currency1, i.ISO_Code, m.Currency2 , i.ISO_Code1, m.Currency3, i.ISO_Code2
FROM A AS m
LEFT JOIN B AS i
ON m.Currency=i.ISO_Code
AND m.Currency2=i.ISO_Code1
AND m.Currency3=i.ISO_Code2
WHERE i.ISO_Code is NULL
OR i.ISO_Code1 is NULL
OR i.ISO_Code2 is NULL;
I wouldn't bother making multiple columns in 'B'. I played with this in SQLFiddle and got it to work.
Something like this:
SELECT
m.Currency1, i.ISO_Code,
m.Currency2, j.ISO_Code AS ISO_Code1,
m.Currency3, k.ISO_Code AS ISO_Code2
FROM A AS m
LEFT JOIN B as i
ON m.Currency1 = i.ISO_Code
LEFT JOIN B as j
ON m.Currency2 = j.ISO_Code
LEFT JOIN B as k
ON m.Currency3 = k.ISO_Code
WHERE
i.ISO_Code IS NULL OR
j.ISO_Code IS NULL OR
k.ISO_Code IS NULL

Table whose columns are random samples from an original column

I have numerical data ​​related to clinical information from people with a particular disease recorded in an specific column ('Lab') from Table A.
I need to get a Table B with 30 rows and 50 columns.
The columns of Table B should be random samples from the values ​​contained in column 'Lab' (nearly 3300 registers).
I am able to get a table with one column using:
SELECT Lab FROM Table_A sample (1) WHERE Lab IS NOT NULL;
Is it possible make a query using the SELECT command that results in Table B with all its 50 columns without the need of getting its columns one by one?
You can use the RAND operator with ORDER BY, like this:
SELECT * FROM Table_A ORDER BY RAND( ) LIMIT 0 , 30

SQL INNER JOIN vs. WHERE ID IN(...) not the same results

I was surprised by the outcome of these two queries. I was expecting same from both. I have two tables that share a common field but there is not a relationship set up. The table (A) has a field EventID varchar(10) and table (B) has a field XXNumber varchar(15).
Values from table B column XXNumber are referenced in table A column EventID. Even though XXNumber can hold 15 chars, none of the 179K rows of data is longer than 10 chars.
So the requirement was:
"To avoid Duplicate table B and table A entries, if the XXNumber is contained in a table A >“Event ID” number, then it should not be counted."
To see how many common records I have I ran this query first - call it query alpha"
SELECT dbo.TableB.XXNumber FROM dbo.TableB WHERE dbo.TableB.XXNumber in
( select distinct dbo.TableA.EventId FROM dbo.TableA )
The result was 5322 rows.
The following query - call it query delta which looks like this:
SELECT DISTINCT dbo.TableB.XXNumber, dbo.TableB.EventId
FROM dbo.TableB INNER JOIN dbo.TableA ON dbo.TableB.XXNumber= dbo.TableB.EventId
haas returned 4308 rows.
Shouldn't the resulting number of rows be the same?
The WHERE ID IN () version will select all rows that match each distinct value in the list (regardless of whether you code DISTINCT indide the inner select or not - that's irrelevant). If a given value appears in the parent table more than once, you'll get multipke rows selected from the parent table for that single value found in the child table.
The INNER JOIN version will select each row from the parent table once for every successful join, so if there are 3 rows in the child table with the value, and 2 in the parent, then there will be 6 rows rows in the result for that value.
To make them "the same", add 'DISTINCT' to your main select.
To explain what you're seeing, we'd need to know more about your actual data.