I have 2 SQL table.Table A and Table B. Both of this table have 10000 records respectively.
In Table A have 3 Column=>
ColumnA,ColumnB,ColumnC
In Table B have 3 Column=>
ColumnD,ColumnE,ColumnF
My Requirement is to select ColumnA and ColumnD with their original record(10000).
My question is how can I select only ColumnA and ColumnD.
The first problem is I cant join this two table because this two table stands as separately.
The second problem is I cant Union this two table because my requirement is to get Two column but when I union, I only get one column with combining two column.
You could create an on-the-fly join column via ROW_NUMBER, and then join on that:
WITH cte1 AS (
SELECT ColumnA, ROW_NUMBER() OVER (ORDER BY ColumnA) rn
FROM TableA
),
cte2 AS (
SELECT ColumnD, ROW_NUMBER() OVER (ORDER BY ColumnD) rn
FROM TableB
)
SELECT t1.ColumnA, t2.ColumnD
FROM cte t1
INNER JOIN cte t2
ON t1.rn = t2.rn;
Of course, this would just pair the 10K records using the arbitrary orderings in the A and D columns. If you have some specific logic for how these two columns should be paired up, then let us know. The bottom line is that you can't easily get away from the concepts of join or union to bring these two columns together.
Related
I have below mentioned two data sets, Table A and Table B, and I am trying to get to Table C as output dataset using Table A and TableB, can you help me with SQL query to come up wit this out put. I am mainly trying to calculate DueAmount column in TableC, and logic to derive this column is mentioned in Calculation column
Data Screenshot:
TableA, TableB and Output screenshot
I thought about trying the logic in which Table A can be expanded to multiple row for each period , and then join TableA with TableB, but I am looking for some logic which will be more efficient for large number of ID's.
The below will generate the results desired:
SELECT p.PeriodId, CASE WHEN b.BAmt < a.AAmt THEN 0 ELSE b.BAmt - a.AAmt END AS DueAmt
FROM (SELECT PeriodId FROM TableA UNION SELECT PeriodId FROM TableB) p
CROSS APPLY (SELECT SUM(Amount) AS AAmt FROM TableA WHERE PeriodId <= p.PeriodId) a
CROSS APPLY (SELECT SUM(Amount) AS BAmt FROM TableB WHERE PeriodId <= p.PeriodId) b
I have multiple SELECT queries which is ran against different tables.
The output of all the queries have the same number of rows (every query when ran individually will have the same number of rows). Is there a way I can combine the output of all these queries into a single result? (Keep out from first query and add the output of next query as a column to the output of the next query). I dont want to save these tables into database as I am just doing some validation testing.
Example:
SELECT AAA,BBB,CCC FROM Table1
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
I tried writing combining the query as
SELECT Table1.AAA,Table1.BBB,Table1.CCC,T1.DDD
FROM Table1,
(SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA)T1
I tried doing the above combined query, but instead of getting 11 rows as output (both queries above had result of 11 rows), I am getting 35 rows as output.
Hope the question made sense!
You'll need to specify a criteria to match each row the first query with which row of the second query.
If, for example, the column AAA is unique in both queries and you want to match rows with the same values you could do:
select a.*, b.*
from (
SELECT AAA,BBB,CCC FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.aaa = a.aaa
If there aren't any clear matching rules, you can produce an artificial row number on each result set and use it to match rows. For example:
select
a.aaa, a.bbb, a.ccc,
b.ddd, b.aaa
from (
SELECT AAA, BBB, CCC,
row_number() over(order by aaa) as rn
FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA,
row_number() over(order by table1.aaa, table2.ddd) as rn
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.rn = a.rn
If you have several results and want to have all of them as additional columns you can simply use ",":
create table temp1 as select '1' as c1 from DUAL;
create table temp2 as select '2' as c2 from DUAL;
create table temp3 as select '3' as c3 from DUAL;
select a.c1, b.c2, c.c3 from temp1 a, (select c2 from temp2) b, (select c3 from temp3) c;
An alternative could also be that you want to have all the results as additional rows then you would use UNION ALL between the individual results.
Table A has a single field.
Table B has many fields.
I want to copy each value from Table A to a row on Table B but I have no values with which to join the two tables.
It doesn't matter which row in B each value in A goes into as long as each value in Table A only appears once in Table B.
I do not want to use loops.
You could assign a row number to each of the two tables, and then do an update join:
WITH cte1 AS (
SELECT col, ROW_NUMBER() OVER (ORDER BY col) rn
FROM TableA
),
WITH cte2 AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM TableB
)
UPDATE t2
SET col = t1.col
FROM cte2 t2
INNER JOIN cte1 t1
ON t1.rn = t2.rn
This solution makes several assumptions, including that TableB already has a destination column col for the data coming from the first TableA table's single column, and that the types match in both tables. It also assumes that TableB has more rows than TableA to fit the data from TableB. If not, data would be lost.
I have five queries and each of them will return me single column multiple row output. I want to to write a function which will contain all of these queries.
Can anyone help?
query 1:
Select Col1 as X from Table1;
query 2:
Select Col3 as Y from Table2;
From a function I want to get a table which will have columns
X, Y
How to club these queries under single function?
Add a ROW_NUMBER() to each of the queries and join them by the row number.
Depending on number of rows returned by each of the query you'd join then by inner, left or full join.
Example below assumes that two queries return the same number of rows.
WITH
CTE1
AS
(
SELECT Col1 as X, ROW_NUMBER() OVER(ORDER BY Col1) AS rn
FROM Table1
)
,CTE2
AS
(
SELECT Col3 as Y, ROW_NUMBER() OVER(ORDER BY Col3) AS rn
FROM Table2
)
SELECT
CTE1.X, CTE2.Y
FROM
CTE1
INNER JOIN CTE2 ON CTE1.rn = CTE2.rn
Use the UNION operator:
SELECT
column_1
FROM
tbl_name_1
UNION ALL
SELECT
column_1
FROM
tbl_name_2;
If there is a relation between the two tables, try using a join.
Maybe a simple inner join would be possible here?
select Col1 as X from Table1
join
on Table1.Col1_name = Table2.col3_name
I have two tables which I would like to place side-by-side exactly as they are. For example,
tableOne tableTwo
columnOne | columnTwo | columnThree columnI | columnII | columnIII
The data in the two tables do not need to be related whatsoever -- the tables have the same row count -- and the data is already sorted in the two tables. Basically, I would like to do a full outer join on the two tables without an on operator.
How can I do this in a SQL query?
Well, you do want an ON operator - you just seem to want it to work automatically, which won't happen.
If you're saying Row 1 of tableOne maps to Row 1 of tableTwo, then you need to add a row column to each table and then join on it.
If you don't specify a join condition, you'll do a cross join that joins every row from tableOne to every row in tableTwo, which obviously isn't what you're looking for.
So do something like this:
select * from
(select *, row_number() over (order by 1) as RN from tableOne) a
inner join (select *, row_number() over (order by 1) as RN from tableTwo) b
on a.RN = b.RN