Column generation from table data with a query - sql

I have the following tables:
Table A
Field Name Field Value
Column1 Column1value
Column2 Column2value
Column3 Column3value
Column4 Column4value
Table B
Column1 Column2 Column3 Column4
Column1value Column2value Column3value Column4value
How do I write a query to generate Table B from Table A?

If you need, using the next query you can get the column names from TableA:
SELECT
name
FROM
syscolumns
WHERE
id = (SELECT id FROM sysobjects WHERE xtype='U' and NAME='TableA')
but if you just want to copy TableA to TableB, use the
SELECT
*
INTO TableB
FROM TableA

You could use SELECT INTO and the resulting TableB below will have the same data and column types as the source table:
SELECT
Column1value,
Column2value,
Column3value,
Column4value
INTO TableB
FROM TableA

Related

select query for one-to-many relations table

I have 2 tables. These two tables have one-to-many relations.
TABLE - A
column1 column2
1 label1
2 label2
TABLE - B
Bcolumn1 Bcolumn2 Bcolumn3
1 value1 value4
1 value2 value5
2 value3 value6
RESULT TABLE
column1 column2 json
1 label1 [{"Bcolumn":value1,"Bcolumn":value4},{"Bcolumn":value2,"Bcolumn":value5}]
2 label2 [{"Bcolumn":value3,"Bcolumn":value6}]
I want to get RESULT TABLE1 using TABLE - A and TABLE - B.
how can I get this result?
Thank you.
Use SQLite's JSON1 Extension functions:
SELECT a.column1, a.column2,
json_group_array(json_object('Bcolumn', b.Bcolumn2, 'Bcolumn', b.Bcolumn3)) json
FROM tableA a INNER JOIN tableB b
ON b.Bcolumn1 = a.column1
GROUP BY a.column1;
See the demo.
What you are looking for in sqlite is the group_concat() function. It's tricky, cause you have he values you want to concat in 2 different columns. Basically you can do the following
select
a.column1
, a.column2
, '[{' || group_concat('"Bcolumn":' || b.bcolumn2 || '"Bcolumn":' || b.bcolumn3,'};{') || '}]' as json_output
from tablea a
inner join tableb b on
a.column1 = b.bcolumn1
group by
a.column1
, a.column2
;
I tested this solution with MSSQL 2019 and string_agg(), but from the documentation (https://www.sqlitetutorial.net/sqlite-group_concat/) this should work just as well in sqlite.
The trick is to use '};{' as separator, because like this, you will only have to care about the opening and closing brackets and nothing in the middle.

Compare results from column1 from column2 using SQL

Backstory,
My company runs redundant call recording servers, each with a list of extensions.
We query these using SQL. I can see there is a 20+ extension difference between the two servers. These are columns that exist in the same table...so essentially I need to do the following:
Compare column1 data from column 2 'server1' in table system.name with column1 data from column 2 'server2' in table system.name and display those that DO NOT exist on both, but exist on one or the other.
Based on what I can undertand from your question
Select column1
from table1 a
where column2 = 'server1'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server2'
)
UNION
Select column1
from table1 a
where column2 = 'server2'
and not exists
(select *
from table1 b
where a.column1 = b.column1
and b.column2 = 'server1'
)

How to combine two sets of data without common field

I came across a tricky SQL to see whether anyone can help
SELECT column1, column2 as highestNo
FROM tableA
INNER JOIN TableB
on TableA.tNo = TableB.Plan_tno
returned Result (it will always be one row returned)
column1 highestNo
J111646912 201603010576
Select Column3, Column4
From TableB
Inner join TableC
On TableB.key1 = TableC.plan_key
Where TableB.Column3< highestNo(get it from last set)
Returned result shall be
Column3 Column4
201603010525 J111646547
201603010004 B233435353
201603010324 J435345445
201603010570 H345353535
How can I combine the script and result as one piece instead of two pieces here?
so I can one set of result returned.
You can use a CROSS JOIN:
Select Column3, Column4, t.column1, t.highestNo
From TableB
Inner join TableC On TableB.key1 = TableC.plan_key
cross join (
SELECT column1, column2 as highestNo
FROM tableA
INNER JOIN TableB on TableA.tNo = TableB.Plan_tno
) as t
Where TableB.Column3 < t.highestNo
The single record returned by the first query will be simply appended to the rest of the records returned by the second query.
Just use a scalar subquery;
Select Column3, Column4
From TableB b Inner join
TableC c
On b.key1 = c.plan_key
Where b.Column3 < (select column2 as highestNo
from tableA a inner join
TableB b
on a.tNo = b.Plan_tno
);
You say the subquery always returns one row. Otherwise, I would advise using max(column2) in the subquery.

Trying to Merge data from one table to another

Trying to do a process where I have a table that is existing. This is table A
I have staging table which I have uploaded the data. This is table B.
Table A already contains data in there apart from some data which i need added from table B to Table A.
So Table B has a extra column which I need it to match with the data already existing in Table A.
So layout currently in Table A is:
Column 1 Column 2 Column 3
AB
ABC
Layout in Table B is:
Column 1 Column 2 Column 3
ABC Yellow Test1
AB Blue Test2
So I need these columns 2 and 3 moved to table A from Table B, so they match correctly with the data that is already in column 1.
Tried my best to explain and english is my 2nd language, so i apologise for any mistakes. Anyway know what best way to go with this, would it be a merge?
Thanks
Update TableA
Set TableA.Column2=B.Column2,
TableA.Column3=B.Column3
From TableA A
Inner Join TableB B ON B.Column1=A.Column1
Use Left Outer join to merge data
SELECT TableA.clomun1,
Tableb.column2.tableb.column3
FROM tableA
LEFT JOIN tableB
ON table1.column1 = table2.column1;
Following query is working fine in MySQL , try it out :
update TableA as a inner join TableB as b on a.Column1=b.Column1
set a.Column2 = b.Column2 , a.Column3=b.Column3
where a.Column1=b.Column1;
And for Oracle use this one :
update TableA
set TableA.Column2 =
(select TableB.Column2 from TableB where TableA.Column1=TableB.Column1),
TableA.Column3 =
(select TableB.Column3 from TableB where TableA.Column1=TableB.Column1);
MERGE INTO TableA
USING (SELECT Column1, COlumn2, Column3
FROM TableB) Q
ON (TableA.Column1=TableB.Column1)
WHEN MATCHED THEN UPDATE SET TableA.Column2=Q.Column2
, TableA.Column3=Q.Column3;
This works fine in Oracle. You can also insert records that are not in Table A adding
WHEN NOT MATCHED THEN INSERT (Column1, Column2, Column3)
VALUES (Q.Column1, Q.Column2, Q.Column3)

using sql create a new table with 2 fields, field1 from tableA and field1 from table B

Am new to SQL and am stuck here with a very simple-looking query request.
I have 2 tables, both having exactly the same structure (IE same no. of columns, same no. Of rows) except for the actual contents. so for example,tableA has 2 columns called col1&col2; tableB has 2 columns too called col1&col2. Now I want to create a 3rd new tale, where 1st column is tableA's col1, and 2nd column is tableB's col1. preferably the name of the 1st column is fromTableA, and name of 2nd column is fromTableC. How do I achieve this please? I tried all the following ways but I always get the same error: "number of query values and destination fields are not the same."
variation 1:
insert into newTable(fromTable1,fromTable2)
select col1 from table1
select col1 from table2
variation 2:
insert into newTable(fromTable1,fromTable2)
select col1 from table1,col1 from table2
variation 3:
insert into newTable(fromTable1,fromTable2)
select col1 from table1, table2
Presumably you have fields in the two tables that can be joined, so this:
insert into newtable (romTable1,fromTable2)
select a.col1, b.col1
from table1 a, table2 b
where a.col1 = b.col1;
The a/b are aliases that differentiate between the two columns in each table. If you don't have fields to join then whatever you're trying to do probably needs a rethink.
You may try following sql query to achieve your purpose:
with OrderedTableA as (
select row_number() over (order by Col1) RowNum, *
from TableA (nolock)
),
OrderedTableB as (
select row_number() over (order by Col1) RowNum, *
from TableB (nolock)
)
select T1.Col1, T2.Col2 into TableC
from OrderedTableA T1
full outer join OrderedTableB T2 on T1.RowNum = T2.RowNum
Above query will create a new table as TableC with column col1 from TableA and col2 from TableB. You may change the queries to your need.
I hope you will understand the above queries. Give it a try.