April 29, 2013 at 11:39 am
Hi Friends...
I Having Table For The Following Structure..
create Table EmployeeDet (EmpID int,EmpName Varchar(100),Department varchar(100),Age int,salary int)
insert into EmployeeDet values (1,'Arun','Accounts',25,25000),(1,'Arun','Accounts',23,21000),(1,'Arun','Accounts',26,22000),(1,'Arun','Accounts',29,27000)
insert into EmployeeDet values (2,'kumar','Production',23,18000),(2,'kumar','Production',27,30000),(2,'kumar','Production',31,19000)
insert into EmployeeDet values (3,'saran','Sales',22,38000)
insert into EmployeeDet values (4,'Vidya','Purchase',18,20000),(4,'Vidya','Purchase',27,36000),(4,'Vidya','Purchase',23,35000)
select * from EmployeeDet
EmpIDEmpNameDepartmentAgesalary
1ArunAccounts2525000
1ArunAccounts2321000
1ArunAccounts2622000
1ArunAccounts2927000
2kumar Production2318000
2kumar Production2730000
2kumar Production3119000
3saranSales 2238000
4VidyaPurchase1820000
4VidyaPurchase2736000
4VidyaPurchase2335000
Below I have mentioned The my Required Table Data
EmpIDEmpNameDepartmentAgesalary
1ArunAccounts2525000
2kumarProduction2318000
3saranSales 2238000
4VidyaPurchase1820000
My Requirement :
1) EmpID,EmpName,Department Field Should not be Duplicate Record...
2) You Take First Record or Last Record or Middle Record of the Particular EmpID
but EmpID,EmpName,Department Should be Unique...
( Here I Mentioned First Record of The EmpID )...
Thanks & Regards,
Saravanan.D
April 29, 2013 at 12:05 pm
sarwaanmca (4/29/2013)
Hi Friends...I Having Table For The Following Structure..
create Table EmployeeDet (EmpID int,EmpName Varchar(100),Department varchar(100),Age int,salary int)
insert into EmployeeDet values (1,'Arun','Accounts',25,25000),(1,'Arun','Accounts',23,21000),(1,'Arun','Accounts',26,22000),(1,'Arun','Accounts',29,27000)
insert into EmployeeDet values (2,'kumar','Production',23,18000),(2,'Arun','Production',27,30000),(2,'Arun','Production',31,19000)
insert into EmployeeDet values (3,'saran','Sales',22,38000)
insert into EmployeeDet values (4,'Vidya','Purchase',18,20000),(4,'Vidya','Purchase',27,36000),(4,'Vidya','Purchase',23,35000)
select * from EmployeeDet
EmpIDEmpNameDepartmentAgesalary
1ArunAccounts2525000
1ArunAccounts2321000
1ArunAccounts2622000
1ArunAccounts2927000
2kumarProduction2318000
2ArunProduction2730000
2ArunProduction3119000
3saranSales 2238000
4VidyaPurchase1820000
4VidyaPurchase2736000
4VidyaPurchase2335000
Below I have mentioned The my Required Table Data
EmpIDEmpNameDepartmentAgesalary
1ArunAccounts2525000
2kumarProduction2318000
3saranSales 2238000
4VidyaPurchase1820000
My Requirement :
1) EmpID,EmpName,Department Field Should not be Duplicate Record...
2) You Take First Record or Last Record or Middle Record of the Particular EmpID
but EmpID,EmpName,Department Should be Unique...
( Here I Mentioned First Record of The EmpID )...
Thanks & Regards,
Saravanan.D
how do we determine record order...to find first/last/middle
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
April 29, 2013 at 12:08 pm
what Ever You Take its ur wish...
thats not a problem...
you take it first record of the EmpID....
April 29, 2013 at 12:15 pm
sarwaanmca (4/29/2013)
what Ever You Take its ur wish...thats not a problem...
you take it first record of the EmpID....
ok...so what do you say is the first record for Arun?
ordered by salary or by age?
its ur db not mine
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
April 29, 2013 at 12:21 pm
you pls show the First Record of the EmpID
April 29, 2013 at 12:22 pm
This looks an awful lot like homework, so I'll frame my answer with that in mind.
It looks like the purpose of this exercise is to learn how to use the GROUP BY statement.
The group by will return one unique record for the columns defined by the GROUP BY;
I think you've already determined what you think you want to avoid duplicates of, so use that for your GROUP BY definition.
Read up on how to use GROUP BY , create a query that features it and the columns you are trying to uniquely identify;
let us know how that works for you.
Lowell
April 29, 2013 at 12:32 pm
This is a test for normalization and, I agree. It looks a lot like homework.
First, you should never ever store age in a database. It can go out of date the second you enter it. Always use a DOB instead. If you need age, create a calclulated column. Note that this cannot be a persisted calculated column because it needs to change when you look at it, not when you update the DOB which should never happen.
Never store other information other than EmployeeID with Salary. It should be in a second table as a Type II Slowly Changing Dimension. Since this is likely homework, I'm not going to tell you how to work that. Please Google it.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 29, 2013 at 12:58 pm
hi Friends..
i got the Result ...
Select EmpID,EmpName,Department,max(Age),MAX(salary) from
EmployeeDet
group by EmpID,EmpName,Department
Thanks for Your valuable Guidance
Regards,
Saravanan.D
April 29, 2013 at 1:01 pm
excellent! glad we could guide you in the right direction; I for one am very glad you were able to solve this with just a gentle nudge!
Lowell
April 29, 2013 at 1:03 pm
but stili i having a doubt ..
how to show the fist Record of the EmpID
Thanks & Regards,
Saravanan.D
April 29, 2013 at 1:07 pm
sarwaanmca (4/29/2013)
hi Friends..i got the Result ...
Select EmpID,EmpName,Department,max(Age),MAX(salary) from
EmployeeDet
group by EmpID,EmpName,Department
Thanks for Your valuable Guidance
Regards,
Saravanan.D
consider this
create Table EmployeeDet (EmpID int,EmpName Varchar(100),Department varchar(100),Age int,salary int)
insert into EmployeeDet values (1,'Arun','Accounts',25,25000),(1,'Arun','Accounts',23,21000),(1,'Arun','Accounts',26,22000),(1,'Arun','Accounts',29,27000), (1,'Arun','Accounts',35,20000)
insert into EmployeeDet values (2,'kumar','Production',23,18000),(2,'kumar','Production',27,30000),(2,'kumar','Production',31,19000)
insert into EmployeeDet values (3,'saran','Sales',22,38000)
insert into EmployeeDet values (4,'Vidya','Purchase',18,20000),(4,'Vidya','Purchase',27,36000),(4,'Vidya','Purchase',23,35000)
select * from EmployeeDet
Select EmpID,EmpName,Department,max(Age),MAX(salary) from
EmployeeDet
group by EmpID,EmpName,Department
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
April 29, 2013 at 1:17 pm
sarwaanmca (4/29/2013)
but stili i having a doubt ..
how to show the fist Record of the EmpID
Thanks & Regards,
Saravanan.D
One of the things you need to understand, there is no concept of first or last in SQL Server table in and of itself. You have to define what you mean by first and last and then you need a means to identify it. For instance, first record entered. How, based solely on the data would you identify that? With your sample data and table structure, you can't. What change(s) do you think you would have to make to know which record was inserted first?
April 29, 2013 at 4:39 pm
sarwaanmca (4/29/2013)
hi Friends..i got the Result ...
Select EmpID,EmpName,Department,max(Age),MAX(salary) from
EmployeeDet
group by EmpID,EmpName,Department
Thanks for Your valuable Guidance
Regards,
Saravanan.D
That will give you single rows but it will not give you the latest value for Salary. It will only give you the value for max salary.The correct value for the latest salary would be the one with the oldest age for each employee. The ROW_NUMBER() OVER method would likely be the best bet for this.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply