create a table out of another table - sql

I want to create a table whose columns are populated using existing tables.The table I want is
tbl_selection
Name of guide No of trainees selected No of trainees alotted
A 2 1
B 1 1
C 1 1
The column Name of guide is populated using tbl_registration , No of trainees selected using tbl_pencil and No of trainees alotted using allot_guide
tbl_registration
Guide_Name PIN_NO Designation
A 1 SC-C
B 2 SC-D
C 3 SC-E
tbl_pencil
TL_NO Student_name College Guide_Name
TOO1 P PQR A
T002 Q XYZ A
T003 R RST B
T004 S UVW C
tbl_alotguide
TL_NO Student_name Guide_Name
T001 P A
TOO3 R B
TOO4 S C
Please help me find an appropriate query for the above

You can update the second and third columns using the UPDATE statement.
UPDATE tbl_selection
SET No_of_trainees_selected =
(SELECT count(*)
FROM tbl_pencil
WHERE tbl_selection.name_of_guide = tbl_pencil.guide_name
GROUP BY tbl_pencil.guide_name)
You can view UPDATE statement documentation in the next link:
https://www.techonthenet.com/oracle/update.php

Related

How to join a column to another column based on two conditions

I am at a bit of a loss why this is not working/updating. I have a 'sites' table and a 'visit' table. The 'sites' table contains all the unique 'sites' with in the DB and the 'visit' table contains all the unique visits to all the sites.
Site
site_id
region
site
1
a
a
2
a
b
3
b
a
4
b
b
Visit
visit_id
region
site
date
site_id
1
a
a
1
2
a
b
2
3
b
a
3
4
b
b
2
5
c
a
3
6
c
b
1
7
d
a
2
8
d
b
2
When region and site equal each other I want the site_id to populate in the 'visit' table. Here is what I have tried. It does not throw and error but nothing populates in the visit.site_id.
update visit a
set site_id =
(select s.site_id
from site s
where (s.region, s.site) = (a.region, a.site));
Your way has a problem and if even it works, it does not work correctly because if there are the same tuples in both tables your query will update all of them.
update visit a
set site_id =
(select s.site_id
from sites s
where (s.region, s.site) = (a.region, a.site) and a.region = a.site);
rewriting in another way:
update visit a
set site_id =
(select s.site_id
from sites s
where s.region = a.region and s.site = a.site and a.region = a.site);
Here's the result(my PostgreSQL version is 13):

SQL Server update and replace values with values from another table

I have two tables:
Connections:
id user_id connection_info
------------------------------
1 1 ...
2 1 ...
3 2 ...
Lists:
id connection_id name
-----------------------
1 1 ...
2 2 ...
3 1 ...
I currently have user_id's in the lists.connection_id column. I would like to join the lists table with the connections table by connections.user_id = lists.connection_id and then replace the lists.connection_id with the corresponding id from the connections table.
You could use UPDATE FROM like this:
update l
set l.connection_id = c.id
from connections c join lists l on c.user_id = l.connection_id
Initially you would want to test what you are going to update, running a SELECT statement:
select l.connection_id as con_old
, c.id as con_new
, ... (other cols you might want to check)
from connections c join lists l on c.user_id = l.connection_id

How to Update common column values from different values in SQL server?

I have two tables. My first table A contains
Tran_Particular | Dr_Tran_Amt | BeneficiaryName | PRNNo
columns.
My second table B contains
BeneficiaryName | Dr_Tran_Amt | PRNNo
columns.
I want to update table A.PRNNo but When I am updating this on BeneficiaryName and Dr_Tran_Amt that time it updates only first value. But In table B there are two PRNNO's are exist and In table A also two different Tran_Particular are exist. I want to update unique PRNNO wrt Tran_Particular, BeneficiaryName and Dr_Tran_Amt.
Query.
update A
set a.PRNNo = b.PRNNo
from A a
inner join B b
on a.Dr_Tran_Amt= b.Amount
and a.BeneficiaryName = b.BeneficiaryName;
HoW to update this in SQL server.
The code below should get you only the first matching row (the lowest PRNNo value):
UPDATE A
SET a.PRNNo = b.PRNNo
FROM A a
INNER join B b
ON a.Dr_Tran_Amt = b.Amount
AND a.BeneficiaryName = b.BeneficiaryName
AND b.PRNNo = (SELECT MIN(c.PRNNo) FROM B c WHERE c.BeneficiaryName = B.BeneficiaryName AND c.Dr_Tran_Amt = b.Dr_Tran_Amt);

Display Summary Result in SQL Server

I have the following table structure also I have mention my expected output please help me with query as I don't know much about SQL query
Table 1 : Category
Name CatId
A 1
B 2
C 3
Table 2 : Emp Details
FName Id Dob CatId
Pratik 1 1958-04-06 2
Praveen 3 1972-05-12 1
Nilesh 2 1990-12-12 2
So far I have tried to get all result with:
SELECT A.Code,A.EmpName,A.DOB,B.cname
FROM EMPMASTER A
JOIN CATMASTER B ON A.cCode = B.ccode AND A.Compcode = B.CompCode
WHERE A.compcode = 'C0001' AND month(A.DOB) >= 1
AND MONTH(A.DOB) <= 12 AND A.termflag='L'
ORDER BY A.DOB
But my problem is, I also want summary results to be displayed
Expected Summary Output :
Grouping No Of Employees
A 1
B 2
C 0
I think you can use LEFT JOIN, GROUP BY and COUNT as follows:
SELECT [Grouping] = c.Name,
[No Of Employees] = COUNT(e.ID)
FROM Category AS c
LEFT JOIN EmpDetails AS e
ON e.CatId = c.CatId
GROUP BY c.Name;
TRY THIS:
SELECT A.NAME,
(SELECT COUNT(*) FROM #EMP B WHERE A.CATID = B.CATID) AS COUNT
FROM #TEMP A

SQL select column equivalence

I have a table that saves the possible states of other tables (entities).
But now I need to find equivalence of states between two entities.
The table structure is something like this
ID TableID StateValue StateDefinition StateDescription
================================================================
1 1 1 Created Just created
2 1 2 Dropped Just Dropped
3 2 1 Created Just Created
4 2 2 Aproved Passed the revision
5 2 3 Dropped Just dropped
I want to get equivalent (comparing text of state) which as a result get this:
TableID1 StateValue1 TableID2 StateValue2 StateDefinition
=============================================================================
1 1 2 1 Created
1 2 2 3 Dropped
My question is, how can it be done??
Do a self join on the table.
A general case might look like:
SELECT A.TableID as TableId1,
A.StateValue as StateValue1,
B.TableId as TableId2,
B.StateValue as StateValue2,
A.StateDefinition
FROM
Table A
INNER JOIN Table B
ON (A.TableId <> B.TableId and A.StateDefiniton = B.StateDefinition)
select t1.TableID as TableID1,
t1.StateValue as StateValue1,
t2.TableID as TableID2,
t2.StateValue as StateValue2,
t1.StateDefinition
from MyTable t1
inner join MyTable t2 on t1.TableID = 1 and t2.TableID = 2
where t1.StateValue = t2.StateValue
and t1.StateDefinition = t2.StateDefinition