SQL: Merge 2 Columns or more from different tables into one Alias new Column - sql

I have 2 tables: DisconnectionsData and DisconnectionsVoice.
both of them have columns: WeekNum,Month,Quarter,Year,IncomeLost and ID.
the names of the columns are different between the 2 tables, but the data inside them is parallel (Quarter and QuarterNumber is literally the same).
my wish is to FULL JOIN both tables into one table with only 6 columns.
i cant figure out how to make an alias, for example: how do i merge DisconnectionsData.Quarter and DisconnectionsVoice.QuarterNumber
into one column with the alias name of QuarterOfDisconnection.
that the desired result:
thank you.

You don't specify which sql are you using? You can try below.
SELECT *
FROM DisconnectionsData
UNION ALL
SELECT *
FROM DisconnectionsVoice

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.

Sum all columns by omitting 3 columns

I have table with more than 34 columns.
I need to aggregate(sum) of the columns.
To find:
Sum of 2 columns
Sum of remaining columns except.
Please find the table structure with following columns.
S.NO
Handling charge
Storage Charge
...
..
.... 34th column charge
I have more than 3000 rows of records based on above mentioned columns.
May I know how to query or calculate above mentioned find.
For Find 1 Query:
**Update dbo.tablename
set columnname isnull(charge1,0)+isnull(charge2,0)**
I am struggling to find find 2:
Can anyone help?
You can query the table to get the list of columns name then apply the Sum() function.
Query table column names:
SELECT *
FROM DatabaseName.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'tableName'

SQL Selecting and Comparing Data from 2 tables

I have 2 tables with 1 column in common. I want to select all data from table1 and want to restrict it with a condition. The thing I could not do is writing the where condition with the non-common column.
Here is the code:
$sql="SELECT kategoriler.adi as katadi, urunler.* FROM `urunler`
LEFT JOIN `kategoriler`
ON urunler.kategori_id=kategoriler.Id
WHERE urunler.kategori_id=$id
OR
kategoriler.ust_id = $id";
ust_id is a parent column of kategori_id and kategoriler.Id. I want to select the child values of it.
(Btw as you can see the common columns are urunler.kategori_id and kategoriler.Id)

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

I want to combine/union two tables and have it create a field that identifies which table it came from

I want to combine/union two tables and have it create a field that identifies which table it came from. I saw an answer for SQL but I do not need max values. I just need to Union two tables. Here is my current SQL for a union query I made through access' query.
SELECT [TableA].[1As], [TableA].[2As]
UNION
SELECT [TableB].[1As], [TableA].[2As];
I want the tables to create whichtabl field and populate whatever word I tell it based on the the table it came from to look like this:
Fields: 1As 2As WhichTabl
data: 100 1 TableA
110 0 TableB
thanks in advanced! Please excuse me I am a newbie!
You can just add in the column as a constant:
SELECT "TableA" as which, [TableA].[1As], [TableA].[2As]
FROM TableA
UNION ALL
SELECT "TableB", [TableB].[1As], [TableB].[2As]
FROM TableB