Append tables in SQL - sql

If I have the two following tables,
Table1
ItemNo Desc Order Number Qty S_Date Location
AA AA AAA A AA/AA/AAAA AAAA
BB BB BBB B BB/BB/BBBB BBBB
CC CC CCC C CC/CC/CCCC CCCC
Table 2
M_Order Item M_Date Total
XXX X XX/XX/XXXX XX
YYY Y YY/YY/YYYY YY
Can anyone advice me how to get the following table please.
Result Table
ItemNo Desc Order Number Qty S_Date Location M_Date Total
AA AA AAA A AA/AA/AAAA AAAA
BB BB BBB B BB/BB/BBBB BBBB
CC CC CCC C CC/CC/CCCC CCCC
X XXX XX/XX/XXXX XX
Y YYY YY/YY/YYYY YY
Thanks

You can do this with a full outer join trick:
select t1.*, t2.*
from table1 t1 full outer join
table2 t2
on 1 = 0;
This will give you the columns in both tables. Each row will be populated with values from only one of the tables.

Similar to Gordons answer but the use of stars in SQL is one of the biggest NO NO's there is.
Also for the actual join part itself you need to do tablename.column = tablename.column on the unique columns. 1 = 0 is a bit ambiguous. but you get the picture

Use the below query join those two tables.
I think you wish to combine the values of two [(ItemNo and Item) and (Desc and M_Desc)] columns in same column and all the other columns separately.
SELECT CAST(ItemNo AS VARCHAR(MAX)) AS ItemNo, CAST(Desc AS VARCHAR(MAX)) AS Desc, OrderNumber, Qty,
S_Date, Location, NULL AS M_Date, NULL AS Total
FROM Table1
UNION ALL
SELECT CAST(Item AS VARCHAR(MAX)) AS ItemNo, CAST(M_Order AS VARCHAR(MAX)) AS Desc, NULL AS OrderNumber, NULL AS Qty,
NULL AS S_Date, NULL AS Location, M_Date, Total
FROM Table2

Related

How to count result values from join query in another table?

I have two tables like so:
table1(user, id, sex) table2(user, name, sex)
jjj 123 m jjj John m
jjj 124 m bbb Bob m
jjj 125 m ppp Pete f
bbb 126 m
bbb 127 f
ppp 128 f
ppp 129 m
ppp 130 m
I want result table where it displays all the users with their names and sex from table 2 who changed their sex at some point along with a count of how many users each name has. So this would be the result table:
(user, name, sex, count)
bbb Bob m 2
ppp Pete f 3
Currently im using this query:
select table2.user, table2.name, table2.sex, count(*)
from table1
join table2 on table1.user = table2.user
where table1.sex <> table2.sex
group by table2.user, table2.name, table2.sex
order by user
However the count column just counts from the resulting join table and not from original table1. Any ideas? thanks.
If I follow this correctly, one option use a lateral join and filtering:
select t2.*, t1.cnt
from table2 t2
inner join lateral (
select count(*) as cnt, min(sex) as minsex, max(sex) as maxsex
from table1 t1
where t1.user = t2.user
) t1 on t1.minsex <> t1.maxsex or t1.minsex <> t2.sex
Basically this filters table1 on users that have different sex or whose sex is different than in table2.

Oracle query eliminate duplicates not considering Null

I've performed a full outer join where I get the following:
ColumnFromTable1 ColumnFromTable2
AAA ABA
AAA Null <- remove
AAA ACC
BBB Null
CCC CDC
Null EFE
DDD FFF
Null FFF <- remove
GGG FFF
What I really want is to squash down the rows to remove duplicates such that my results would be:
ColumnFromTable1 ColumnFromTable2
AAA ABA
AAA ACC
BBB Null
CCC CDC
Null EFE
DDD FFF
GGG FFF
Basically I need to eliminate
AAA Null
Null FFF
since I have an AAA or FFF with a non-null. But I need to keep
BBB Null
Null EFE
because there is no BBB or EFE with a non-null
I've tried modifying my full outer join (which I can post if need be) and also tried wrapping these results in a sub query.
EDIT here is the query simplified for this post
select ColumnFromTable1, ColumnFromTable2 from Table1 t1
full outer join Table2 t2 on t1.common_code = t2.common_code
group by ColumnFromTable1, ColumnFromTable2
row_number() looks promising:
select c1, c2
from (
select c1, c2,
row_number() over (partition by c1 order by c2) r1,
row_number() over (partition by c2 order by c1) r2
from t)
where not ((c1 is null and r2 > 1) or (c2 is null and r1 > 1))
demo
It eliminates null values when they are not first in order.
Placing the full outer join you already computed in a CTE, you can filter it further as you want by doing:
with f (c1, c2) as (
... -- full outer join you already computed here
)
a (x) as ( -- repeated c1 with non-null c2
select c1
from f
group by c1
having count(c2) > 0
),
b (x) as ( -- repeated c2 with non-null c1
select c2
from f
group by c2
having count(c1) > 0
)
select *
from f
where not(c1 in (select x from a) and c2 is null)
and not(c2 in (select x from b) and c1 is null)

Oracle 11g - Creating a side by side comparison of table comlumn data

I have a query where I need to extract data in a regular table and put two rows of data into a single row.
I have rows that consist of
StudentID AUDIT_ACTN Audit_Date .....
aaa A 01/01/2010
aaa A 03/04/2011
aaa A 02/02/2013
aaa D 09/10/2010
aaa D 05/06/2011
aaa D 06/07/2013
aaa A 11/12/2014~
bbb A 01/01/2010
bbb A 03/04/2011
bbb A 02/02/2013
bbb D 09/10/2010
bbb D 05/06/2011
bbb D 06/07/2013
bbb A 11/12/2014~
I want output like this
StudentID AUDIT_ACTN Audit_Date StudentID AUDIT_ACTN Audit_Date
aaa A 01/01/2010 aaa D 09/10/2010
aaa A 03/04/2011 aaa D 05/06/2011
aaa A 02/02/2013 aaa D 06/07/2013
aaa A 11/12/2014 NULL NULL NULL
bbb A 01/01/2010 bbb D 09/10/2010
bbb A 03/04/2011 bbb D 05/06/2011
bbb A 02/02/2013 bbb D 06/07/2013
bbb A 11/12/2014 NULL NULL NULL
The A & D data rows are related, a= add the something to the record and d = delete something from the record (something is an indicator). These is logical in that you must add something before you delete it and you cannot add it twice, without deleting it first.
My current script is probably going down the wrong track but here goes;
select a.StudentId,a.Audit_Date,a.AUDIT_ACTN,d.StudentId,Audit_Date,d.AUDIT_ACTN,
from table a
join
(select *
from
(Select StudentId, Audit_Date,AUDIT_ACTN
from table b
Where b.AUDIT_ACTN='D'
order by Audit_Date
)
where rownum=1
) d on a.StudentId = d.StudentId
and a.AUDIT_ACTN='A'
and Select * from (Select Audit_Date
Order by a.StudentId, a.Audit_Date
I know this is wrong but where do I go from here. If anyone can help and point me in the right direction. It would be appreciated.
My current attempts bring me zero rows, when I take out the rownum it brings me a x join returning 12 rows in this case.
thanks
Roger
I think you need aggregate function like below
select
A.StudentId,A.Audit_Date,A.AUDIT_ACTN,
D.StudentId,D.Audit_Date,D.AUDIT_ACTN
from
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='D'
) D
FULL OUTER JOIN
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='A'
) A
on A.rn = D.rn and A.StudentId = B.StudentId
I hope this will work

Select all items that match in field 'A' where one of the matching fields contains a separate field

My question in brief is as follows...
Sales order Lines (different items on a sales order)
Sales Order Number Item Number Category
1001 aaa Z
1001 bbb X
1001 ccc Y
1002 bbb X
1002 eee W
1003 ttt S
1003 rrr P
1004 bbb X
1005 eee W
I would like to show all sales order lines where the sales order contains an item with the category X
so the results may appear something like this;
Sales Order Number Item Number Category
1001 aaa Z
1001 bbb X
1001 ccc Y
1002 bbb X
1002 eee W
1004 bbb X
as 1003 and 1005 contain no items with category X
An Exists clause should be fine.
select *
from <yourTable> t1
where exists (select null
from <yourTable>
where t1.salesordernumber = salesordernumber
and category = 'X')
Quite an easy one with EXISTS:
SELECT *
FROM orders o
WHERE EXISTS (
SELECT 1
FROM ORDERS o2
WHERE o.[Sales Order Number] = o2.[Sales Order Number]
AND o2.Category = 'X'
)
Here's an option without Exists, if you prefer:
SELECT * FROM MY_TABLE
WHERE [Sales Order Number] in
(SELECT DISTINCT [Sales Order Number] from MY_TABLE where [Category] = 'X')
I don't tend to like correlated subqueries because I see their performance degrade in some situations more than others, but YMMV. All the responses so far should work.
Try this:
SELECT *
FROM SalesOrders
WHERE SalesOrderNumber IN
(SELECT DISTINCT SalesOrderNumber
FROM SalesOrders
WHERE Category = 'X')

How to select only those rows from the table, two columns of which aren't duplicated in that table

Here's rough example of what I need to accomplish for better understanding:
SampleTable
PrimaryKey SpecialCol1 RandomCol SpecialCol2
1 aaa sample1 111
2 aaa sample2 222
3 bbb sample3 444
4 aaa sample4 111
5 ccc sample5 444
6 bbb sample6 444
Final result should look like this:
PrimaryKey SpecialCol1 RandomCol SpecialCol2
--------------------------------------------
2 aaa sample2 222
5 ccc sample5 444
So I just need to select those rows for which combination of SpecialCol1 and SpecialCol2 is unique in that table. I've spent a lot of time trying to figure out how it can be done but so far no luck.
SELECT MIN(primarykey), specialcol1, MIN(randomcol), specialcol2
FROM sampletable
GROUP BY specialcol1, specialcol2
HAVING COUNT(*) = 1;
Do it in stages.
Find the rows where the combination of columns is unique:
SELECT SpecialCol1, SpecialCol2
FROM SampleTable -- Thank you for invcluding the tablename
GROUP BY SpecialCol1, SpecialCol2
HAVING COUNT(*) = 1;
Find the other details for the matching rows:
SELECT s.*
FROM SampleTable AS s
JOIN (SELECT SpecialCol1, SpecialCol2
FROM SampleTable -- Thank you for invcluding the tablename
GROUP BY SpecialCol1, SpecialCol2
HAVING COUNT(*) = 1
) AS t
ON t.SpecialCol1 = s.SpecialCol1 AND t.SpecialCol2 = s.SpecialCol2;
Try this
SELECT PrimaryKey
,SpecialCol1
,RandomCol
,SpecialCol2
FROM (( SELECT SpecialCol1+SpecialCol2 AS test
,PrimaryKey
,SpecialCol1
,RandomCol
,SpecialCol2
FROM table)) AS temp
WHERE test IN (SELECT test
FROM ( SELECT SpecialCol1+SpecialCol2 AS test
FROM table) AS temp
GROUP BY test
HAVING COUNT(test)=1 )