Insert trigger on a table with extra constant column in to new table? - sql

I've been working on a database which consists of two schemas names as front and backup. Where in one table name:
front.Details
studID SemID GPA
100 1 4
200 2 3
Another table name is:
backup.DetailsV
studID DEPT SemID GPA
The output in Table backup.DetailsV should look like below:
studID DEPT SemID GPA
100 1 1 4
200 1 2 3
100 2 1 4
200 2 2 3
How can I create trigger on table Details to insert in to table DetailsV twice with dept id 1 and 2?

To continue Damien's thought, if the only reason to have the DetailsV table is to generate that output, you can easily do that with a view. If it is just reading data, the stored procedure doesn't know or care if the source is a table or a view.
Select studID, 1 as Dept, SemID, GPA
From front.Details
UNION ALL
Select studID, 2 as Dept, SemID, GPA
From front.Details
You would only keep a backup table if you needed to keep a history of the data flowing through the front.Details table, or if you needed to manipulate that data before reporting it out. If that is really what you want, the trigger query is very similar, but instead of addressing the table, you use the special [inserted] table to get the new values.
Select studID, 1 as Dept, SemID, GPA
From inserted
UNION ALL
Select studID, 2 as Dept, SemID, GPA
From inserted

Related

How to use Max while taking other values from another column?

I am new in SQL and have problem picking the biggest value of a column for every manager_id and also other information in the same row.
Let me show the example - consider this table:
name
manager_id
sales
John
1
100
David
1
80
Selena
2
26
Leo
1
120
Frank
2
97
Sara
2
105
and the result I am expecting would be like this:
name
manager_id
top_sales
Leo
1
120
Sara
2
105
I tried using Max but the problem is that I must group it with manager_id and not being able to take name of the salesPerson.
select manager_id, max(sales) as top_sales
from table
group by manager_id ;
This is just an example and the actual query is very long and I am taking the information from different tables. I know that I can use the same table and join it again but the problem is as I mentioned this query is very long as I am extracting info from different tables with multiple conditions. And I don't want to make a temporary table to save it. It should be done in one single query and I actually did solve this but the query is super long due to the inner join that I used and made original table twice.
My question is that can I use Max and have the value in the name column or is there other method to solve this?
Appreciate all help
You can use row_number() with CTE to get the highest sales for each manager as below:
with MaxSales as (
select name, manager_id, sales,row_number() over (partition by manager_id order by sales desc) rownumber from table
)
select name , manager_id ,sales from MaxSales where rownumber=1

Order by result of subquery in PostgreSQL

Assuming I have one table Employees with the columns id, name, salary and manager_id
and another table fields with the column field which can be any of the fields in the Employees table.
How can I sort the employees by the rows in the fields table?
For example: when fields contains the values 'salary', 'manager_id', the employees will be sorted by salary and then by manager_id.
I tried something like this but it didn't work:
SELECT * FROM employees ORDER BY (SELECT field FROM fields)
Edit: The original question was a simplified example of my goal.
I want that the employees will be sorted by their super manager id, then by the second super manager id...and in the end by their direct manager’s id.
Given the employees(id, name, salary, manager_id):
1 Alex 1000 NULL
2 Mor 2000 1
3 John 3000 NULL
4 Chris 4000 1
5 Michael 5000 4
6 Matt 6000 2
The query result will be:
1 Alex 1000 NULL
2 Mor 2000 1
6 Matt 6000 2
4 Chris 4000 1
5 Michael 5000 4
3 John 3000 NULL
You cannot do that in a single query.
First you have to query fields, then construct an SQL statement with the proper ORDER BY clause and run that.
Beware of SQL injection — use the format function to construct the query.
If you can tell us the error that it gives you it may help us helping you
what i think is that ' symbols are what making the issue so it will be as if you're writing:
SELECT * FROM employees ORDER BY 'salary', 'parent_id'
try replacing it with a blank character using Replace()
There is a way how this can be accomplished. But i strongly advice to change your design.
To support this you must add SEQ field in your Fields table to decide order of fields in Fields table. First field have SEQ 1, second 2 ...
SELECT
*
FROM
Employees E
ORDER BY
CASE
(SELECT
F.NAME
FROM
Fields F ORDER BY F.SEQ LIMIT 1)
WHEN 'salary' THEN E.salary
WHEN 'parent_id' THEN E.parent_id
ELSE 0 END
,
CASE
(SELECT
F.NAME
FROM
Fields F ORDER BY F.SEQ LIMIT 1 OFFSET 1)
WHEN 'salary' THEN E.salary
WHEN 'parent_id' THEN E.parent_id
ELSE 0 END
sample on sql fiddle to demonstrate. There are two tables FieldsA and FieldsB to make testing easier without need for delete from table Fields and new records to see if it is working.
http://sqlfiddle.com/#!15/64df6e/2/0

SQL Insert with value from different table

I have 2 tables storing information. For example:
Table 1 contains persons:
ID NAME CITY
1 BOB 1
2 JANE 1
3 FRED 2
The CITY is a id to a different table:
ID NAME
1 Amsterdam
2 London
The problem is that i want to insert data that i receive in the format:
ID NAME CITY
1 PETER Amsterdam
2 KEES London
3 FRED London
Given that the list of Cities is complete (i never receive a city that is not in my list) how can i insert the (new/received from outside)persons into the table with the right ID for the city?
Should i replace them before I try to insert them, or is there a performance friendly (i might have to insert thousands of lines at one) way to make the SQL do this for me?
The SQL server i'm using is Microsoft SQL Server 2012
First, load the data to be inserted into a table.
Then, you can just use a join:
insert into persons(id, name, city)
select st.id, st.name, c.d
from #StagingTable st left join
cities c
on st.city = c.name;
Note: The persons.id should probably be an identity column so it wouldn't be necessary to insert it.
insert into persons (ID,NAME,CITY) //you dont need to include ID if it is auto increment
values
(1,'BOB',(select Name from city where ID=1)) //another select query is getting Name from city table
if you want to add 1000 rows at a time that'd be great if you use stored procedure like this link

select sql statement... error with from clause

Table 1: Students
Name Class PK
John Mike 1
Andrei Tom 2
Table 2: Grades
Disciplne Grade PK_student
math 2 1
math 10 1
math 8 2
math 5 2
What it's the correct statement to delete rows in table 2 grades by PK from students. My point it's to delete a studente from databse and his grades. Thanks
i try to delete in c# grades for a student. I read name and second name, and it should remove his grades from table
OleDbCommand comanda1 = new OleDbCommand("DELETE FROM GRADES WHERE PK_student=SELECT PK FROM Students WHERE Name=#p0 AND Class=#p1 ; ", conex);
comanda1.Parameters.Add(new OleDbParameter("p0", v[0]));
omanda1.Parameters.Add(new OleDbParameter("p1", v[1]));
it gives me an error in FROM clause.
If you want to delete the data rfom table 2 by table 1 then you can use following query:
DELETE FROM GRADES WHERE PK_student IN (SELECT PK FROM STUDENTS);
Hope this is the desired query you want to achieve.

Relational table SQL Plus

(1) Create a relational table TOTAPPS(anum,totap) that contains information
about the numbers of applicants (anum) and the total number
of applications (totap) submitted by each applicant. If an applicant submitted
no application then a value ofattribute
totap should be equal to zero. Load data to a relational table TOTAPPS.
So i just want to understand that the relational table isn't using following query
CREATE TABLE test_table ( anum INTEGER, totap INTEGER );
and how do i point the anum to count the number of applicants using the relational table
example my applicants table like this:
//Applicant
A#
---------
1
2
3
4
5
//Application
A# POSITION
--------------------
1 CEO
1 GM
2 DIRECTOR
3 MANAGER
so when i SELECT * FROM TOTAPPS, the following result should be like this:
//TOTAPPS
ANUM TOTAP
-------------------
1 2
2 1
3 1
4 0
5 0
i must insert manually or when i create the relational table i can straight count it?