add column in table1 derived calculation from table2 's column - c#

I have two tables
Employee
Finance
Employee contains 3 columns
id
Name
BaseSalary
Finance table contains 2 columns
id
BonusPercentage
I want to add TotalBonus column in Employee table. For calculation I am using BaseSalary from Employee table and BonusPercentage from Finance table.
TotalBonus = BaseSalary + (BaseSalary * BonusPercentage)
I can use this with stored procedure but i don't want. However, I created scalar function (calculateBonus) which returns TotalBonus but the problem is that i can't add column in table as i have dependency of BonusPercentage of Finance table.
In general i want employee table to have following:
id
Name
BaseSalary
TotalBonus
I know there are many ways but want in Employee table as I am using this table on C#.
Thanks

Assuming that there can be a maximum of one row in finance for each employee, you want a (left) join:
SELECT e.id,
e.name,
e.basesalary,
coalesce(f.bonuspercentage, 0) * e.basesalary totalbonus
FROM employee e
LEFT JOIN finance f
ON f.id = e.id;

Related

How to loop through tables from SQL in c# without hardcoding

I'm creating a desktop application. I have 3 table (part time employees table, full time employees table and department table). The ID on department table is the foreign key.
I have created a connection string in my class. I'm trying to loop through the tables to get all departments that are assigned to employees, if a department is not assigned (delete it). I have the code in sql, but I want it in c#. Below is the sql code.
SELECT DepartmentName, DepartmentAddress
FROM Department
WHERE ID IN
(SELECT DISTINCT(ID)
FROM
PartTimeEmployees
);
If you simply want to DELETE departments that don't have a related employee, you can use an EXISTS:
DELETE D
FROM dbo.Department D
WHERE NOT EXISTS(SELECT 1
FROM dbo.Employee E
WHERE E.DepartmentID = D.ID);
There's no need to go any kind of "looping", as SQL is far better at doing these operations in a set-based way.
If deleting department not in part time employees table or full time employees table then you can get the department list as -
var departmentToDelete = departments.Where(d => !PartTimeEmployees.Any(emp => emp.DepartmentId == d.Id) && !FullTimeEmployees.Any(emp => emp.DepartmentId == d.Id)).ToList();

Linq to Entity use relation of tables fill gridview

I have 3 Tables Customer,CustomerTicket,Ticket
Customer-->ID primary key
CustomerTicket-->ID,TicketNo where ID,TicketNo are foreign-key
Ticket-->TicketNo,Subject where TicketNo primary key
I am using linq to entity and want to show columns like this,
ID TicketNo Subject
1 12 Car
1 18 Home
2 23 Plane
Every unique ID can have Many TicketNo and every TicketNo has one unique Subject
Gridview.DataSource=from customer in entity.Customer
join custicket in entity.CustomerTicket on customer.ID equals custicket.ID .....
I tried code like the above but in the end couldn't understand how to make the table as I want.How will code continue or is there any better way? Also note that entity framework took my CustomerTicket table and add it as navigation property ...
Do the joins, create an anonymous collection with select, create a binding datasource with the collection and set the datasource
var cusList=from customer in entity.Customer
join custicket in entity.CustomerTicket on customer.ID equals custicket.ID
select new
{
custicket.ID,
custicket.ticketno,
ticket.subject
};
var bs = new BindingSource();
bs.DataSource=cusList;
Gridview.DataSource=bs;

how do select from table 1 based on table2 conditions

I have 2 tables :
Teachers :
teacher_id
teacher_name
=============
Timetable:
tt_id
tt_teacher_id
tt_term_id
tt_day_id
tt_hour_id
=====================================
I want 2 type SQL select query :
1)(this query for select teachers and set into combobox) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
teacher names comes from teachers table based on above conditions in timetable table .
2) select teachers_name that not exists in records with special parameters in timetable
tt_term_id=parameter1
tt_day_id =parameter2
tt_hour_id=parameter3
tt_teacher_id !=parameter4
teacher names comes from teachers table based on above conditions in timetable table .
Thanks a lot
You can join the tables based on the teacher id and use whatever conditions you want.
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
INNER JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_term_id = parameter1
AND tt_day_id = parameter2
AND tt_hour_id = parameter3
ORDER BY teacher_name
The other one you will be able to come up with yourself :-)
If you want teachers who do not have any timetable records then you can do:
SELECT DISTINCT teacher_id, teacher_name
FROM Teachers
LEFT JOIN Timetable
ON teacher_id = tt_teacher_id
WHERE tt_id is NULL
ORDER BY teacher_name

How to remove the duplicate rows by summing up the required columns in a datatable

Hi all I am having my data table which is from my database is as follows
Name Total
XYZ 20
XYZ 20
ABC 20
Now I would like to have my data table as follows
Name Total
XYZ 40
ABC 20
I tried this linq from here Find duplicate and merge record into single datatable c# which works fine but as I am having my values from database I don't know the type of the variable so can some one help me and give me the solution in non-linq way
If you have two tables and you want to combine them all then the below is what you are after
SELECT bothTables.Name, SUM(total) FROM
(
SELECT Name, SUM(total) as total FROM Table_1 GROUP BY Name
UNION ALL
SELECT Name, SUM(total) as total FROM Table_2 GROUP BY Name
) AS bothTables
GROUP BY bothTables.Name
ORDER BY bothTables.Name desc
or if you want to do it using your Data Table (dt in this example)
var summedValues = from table in dt.AsEnumerable()
group table by table.Field<string>("Name")
into groupedTable
select new
{
Name = groupedTable.Key,
Total = groupedTable.Sum(x => x.Field<int>("Total"))
};
SQL version of the solution would be:
select Name, sum(Total) group by Name

Retrieve Integer value from string in a Gridview

I have a gridview, when inserting data into the table dept(100, 1, 'IT') and when fetch the data from two tables emp and dept the records look like below grid:
EMpNo DeptNo DeptName
Harshal 1 IT
My problem is that, when I click on a grid view all records shows in respected text boxes
but issue is, I got all data except empno. Here I get as Name of employee instead of name I want to Empno.
you can try like this
select empno,deptno,deptname from emp inner join dept on emp.dept.id=dept.id

Categories