How to merge 2 tables together? - sql

I have 2 a table with this structure:
[Database1].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 book1
2 book2
3 book2
4 tv1
[Database2].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 nick1
2 nick2
3 book2
4 tv2
5 tv3
6 tv1
I have 2 tables with this structure where the UserID column is linked to the Users_Detail
[Database1].[dbo].[Sys_User_Ghost]
ID UserID Value
-------------------------------
1 3 Value1
2 3 Value2
3 3 Value3
4 4 Value4
[Database2].[dbo].[Sys_User_Ghost]
ID UserID NickName
-------------------------------
1 3 Value12
2 2 Value13
3 4 Value14
4 4 Value15
5 6 Value16
6 6 Value16
I want to convert this table to:
[Database1].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 book1
2 book2
3 book2
4 tv1
5 tv3
6 nick1
7 nick2
8 book2
9 tv2
10 tv1
[Database1].[dbo].[Sys_User_Ghost]
ID UserID Value
-------------------------------
1 3 Value1
2 3 Value2
3 3 Value3
4 4 Value4
5 8 Value12
6 7 Value13
7 9 Value14
8 9 Value15
9 10 Value16
10 10 Value16
I want the duplicate userid columns to be able to change to another userid without duplicates and add.
I have a Sys_User_Ghost table with a userid column associated with the userid at Users_Detail. I want it to sync with the duplicate userids changed in the question above.

For User_Details table use the following query:
INSERT INTO [Database1].[dbo].[Users_Detail] (UserID, NickName)
SELECT UserID, NickName
FROM [Database2].[dbo].[Users_Detail]
For Sys_User_Ghost table use this query:
INSERT INTO [Database1].[dbo].[Sys_User_Ghost] (ID, UserID, Value)
SELECT ID, UserID, Value
FROM [Database2].[dbo].[Sys_User_Ghost]
Using these queries from database2 tables data insert into database1 tables, you can check using following query
SELECT * FROM [Database1].[dbo].[Users_Detail]
SELECT * FROM [Database1].[dbo].[Sys_User_Ghost]

You can do it in below approach,
For Users_Detail Table,
select * from [Database1].[dbo].[Users_Detail]
union all
select * from [Database2].[dbo].[Users_Detail]
For Sys_User_Ghost table,
select * from [Database1].[dbo].[Sys_User_Ghost]
union all
select * from [Database2].[dbo].[Sys_User_Ghost]
Note that if you dont want to take duplicates then use union instead of unionall.
See here details about sql union and union all .
https://www.sqlshack.com/sql-union-vs-union-all-in-sql-server/

Related

Generate a serial number based on quantity column in sql

Hi Experts I have a table like this
T1
Order_no
Qty
1
3
2
5
3
1
4
3
I need to generate a column 'serial no' having values based on 'qty'
Output needed
OrderNo
Qty
SerailNo
1
3
1
1
3
2
1
3
3
2
5
1
2
5
2
2
5
3
2
5
4
2
5
5
3
1
1
4
3
1
4
3
2
4
3
3
Any suggestions?
Thanks in advance!!
You don't mention the specific database so I'll assume you are using PostgreSQL, aren't you?
You can use a Recursive CTE to expand the rows. For example:
with recursive
n as (
select order_no, qty, 1 as serial_no from t1
union all
select order_no, qty, serial_no + 1
from n
where serial_no < qty
)
select * from n order by order_no, serial_no
Result:
order_no qty serial_no
--------- ---- ---------
1 3 1
1 3 2
1 3 3
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
3 1 1
4 3 1
4 3 2
4 3 3
See running example at DB Fiddle.
EDIT FOR ORACLE
If you are using Oracle the query changes a bit to:
with
n (order_no, qty, serial_no) as (
select order_no, qty, 1 from t1
union all
select order_no, qty, serial_no + 1
from n
where serial_no < qty
)
select * from n order by order_no, serial_no
Result:
ORDER_NO QTY SERIAL_NO
--------- ---- ---------
1 3 1
1 3 2
1 3 3
2 5 1
2 5 2
2 5 3
2 5 4
2 5 5
3 1 1
4 3 1
4 3 2
4 3 3
See running example at db<>fiddle.
You should first provide the database you're using. Whether it's oracle, Sql Server, PostGreSQL will determine which procedural language to use. It's very likely that you'll need to do this in two steps:
1st: Duplicate the number of rows based on the column Qty using a decreasing loop
2nd: You'll need to create a sequential partionned column based on the Qty column

Select from same column but multiples values and number of records with one specific value

ID Customer Status
1 ABC 1
2 ABC 2
3 ABC 3
4 ABC 1
5 PQR 1
6 PQR 2
7 PQR 3
8 XYZ 1
9 XYZ 3
I want to select customer who has both values "status=1" and "Status=2' and also total number of entry of same customer with Status=1.
So the result will be,
Customer totalEntryStatus1
ABC 2
PQR 1
How can I do this.
Thankyou !
select Customer, count(case when status = 1 then 1 end) totalEntryStatus1
from table
where Status in (1,2)
group by Customer
having count(distinct Status) = 2

Finding Distinct values across multiple columns and rows in SQL

I have data like this
Id code1 code2 code3 code4 code5 code6
1 2 3 4 5 6 7
1 4 5 2 3 7 6
1 7 6 5 2 3 4
1 5 7 6 4 3 2
1 7 5 6 3 2 4
I need to identify the distinct codes from this set of 6 codes across 5 rows and 6 columns and display them in any order of 6 rows with ID and code
OUTPUT
ID Code
1 7
1 6
1 2
1 3
1 5
1 4
enter image description here
One method is to use union :
select id, code
from (select id, code1 as code
from table t
union
select id, code2
from table t
. . .
select id, code6
from table t
) t;

Retrieve data from Table

I have a table as follows,
Id Form_Id Form_Layout_Txt
-------------- ----------- -----------------
2 1 aa
3 1 bb
5 2 dddd
6 2 eeeee
7 3 fffff
8 3 gggg
i need to retreive the rows which has same Form_id as follows,
2 1 aa
3 1 bb
How to write a query for the above data?
Thanks
Here is what you need at most basic level, unless there's a hidden immunity to find out more :)
SELECT * FROM YOUR TABLE
WHERE FORM_ID = 1
Select *
from nameoftable
where Form_Id = 1;

Make a recursive function in SQL Server 2005

cat_id prod_name parent_cat_id
------ ---------- ------------
1 prod_1 2
2 prod_2 5
3 prod_3 1
4 prod_4 3
5 prod_5 7
6 prod_6 5
In a recursive function, make a table and by using these, if cat_id = 1 and parent_cat_id = 1 take that product name and if that product category id and parent category id is same then take that record also..
ANS IS LIKE :::
1 prod_1 2
2 prod_2 5
5 prod_5 7
WITH rows AS
(
SELECT cat_id, prod_name, parent_cat_id
FROM mytable
WHERE cat_id = 1
UNION ALL
SELECT m.cat_id, m.prod_name, m.parent_cat_id
FROM mytable m
JOIN rows r
ON r.parent_cat_id = m.cat_id
)
SELECT *
FROM rows