Showing posts with label alli. Show all posts
Showing posts with label alli. Show all posts

Friday, March 30, 2012

How Can I retrieve data from a self joined table

hi all
i have one table with the format
PKEmployeeID PrimaryKey
FKDeptID
Salary
EmployeeName
FKEmployeeID
here I have to retrieve the max salary by dept wise
thats ok by writing
Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
How can i retrieve the employeename for the max salary
Thanks and Regards
Sarada V
It may be possible in same FKDeptID two or more employee have same
salary which is max in that FKDeptID.
Select max(salary), employeename, FKDeptID from employee_sarda group by
employeename,FKDeptID.
or
if you want only one name then
Select max(salary), max(employeename), FKDeptID from employee_sarda
group by FKDeptID.
Please post ddl with data to get better idea.
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***
|||Hi
Can you provide us with your table's stucture?
--Written by Itzik Ben-Gan
CREATE TABLE Employees
(
empid int NOT NULL,
mgrid int NULL,
empname varchar(25) NOT NULL,
salary money NOT NULL,
CONSTRAINT PK_Employees_empid PRIMARY KEY(empid),
CONSTRAINT FK_Employees_mgrid_empid
FOREIGN KEY(mgrid)
REFERENCES Employees(empid)
)
CREATE INDEX idx_nci_mgrid ON Employees(mgrid)
INSERT INTO Employees VALUES(1 , NULL, 'Nancy' , $10000.00)
INSERT INTO Employees VALUES(2 , 1 , 'Andrew' , $5000.00)
INSERT INTO Employees VALUES(3 , 1 , 'Janet' , $5000.00)
INSERT INTO Employees VALUES(4 , 1 , 'Margaret', $5000.00)
INSERT INTO Employees VALUES(5 , 2 , 'Steven' , $2500.00)
INSERT INTO Employees VALUES(6 , 2 , 'Michael' , $2500.00)
INSERT INTO Employees VALUES(7 , 3 , 'Robert' , $2500.00)
INSERT INTO Employees VALUES(8 , 3 , 'Laura' , $2500.00)
INSERT INTO Employees VALUES(9 , 3 , 'Ann' , $2500.00)
INSERT INTO Employees VALUES(10, 4 , 'Ina' , $2500.00)
INSERT INTO Employees VALUES(11, 7 , 'David' , $2000.00)
INSERT INTO Employees VALUES(12, 7 , 'Ron' , $2000.00)
INSERT INTO Employees VALUES(13, 7 , 'Dan' , $2000.00)
INSERT INTO Employees VALUES(14, 11 , 'James' , $1500.00)
GO

> Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
> How can i retrieve the employeename for the max salary
CREATE FUNCTION dbo.ufn_GetSubtreeSalary
(
@.mgrid AS int
)
RETURNS int
AS
BEGIN
RETURN (SELECT Salary
FROM Employees WHERE empid = @.mgrid) +
CASE
WHEN EXISTS(SELECT * FROM Employees WHERE mgrid = @.mgrid) THEN
(SELECT SUM(dbo.ufn_GetSubtreeSalary(empid))
FROM Employees
WHERE mgrid = @.mgrid)
ELSE 0
END
END
GO
SELECT dbo.ufn_GetSubtreeSalary(3)
GO
If it doesnot help , pls show us an expected result
"pandu" <saradabhanuv@.gmail.com> wrote in message
news:1135835404.968676.316380@.g14g2000cwa.googlegr oups.com...
> hi all
> i have one table with the format
> PKEmployeeID PrimaryKey
> FKDeptID
> Salary
> EmployeeName
> FKEmployeeID
> here I have to retrieve the max salary by dept wise
> thats ok by writing
> Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
> How can i retrieve the employeename for the max salary
> Thanks and Regards
> Sarada V
>
|||hi Amish
thanks for ur reply
but,for ur first query
Select max(salary), employeename, FKDeptID from employee_sarda group by
employeename,FKDeptID
its just listing all the records
and for ur second query
Select max(salary), max(employeename), FKDeptID from employee_sarda
group by FKDeptID
but whats it meant by max(employeename) its giving the salary field
correctly
but it only selects the first employeename not the required employeename
the required empname is the employeename corresponding to max salary in
that particular dept
Thanks and regards
Sarada v
*** Sent via Developersdex http://www.codecomments.com ***
|||hi
this is the table structure
PKEmployeeID bigint
EmployeeName varchar
FKDeptID bigint
Salary decimal
PhoneNumber varchar
FKEmployeeID bigint
and i dont know anything about creating functions and all those stuff
i am loooking for a simple query
Can u help me plz
Thanks and Regards
Sarada V
*** Sent via Developersdex http://www.codecomments.com ***
|||Try this: ("nested table" techniqe)
select emp.employeename, maxsal.salary1, maxsal.fkdeptid from
employee_sarda,
( Select max(salary) as salary1, FKDeptID from employee_sarda
group by FKDeptID ) as maxsal
where
emp.FKDeptid = maxsal.FKDeptid
emp.salary = maxsal.salary
Note: If 2 or more guys of a dept. earn the same maximum, you'll get
back all of them.
Is this from some exam :-) ?
|||Or this ("correlated subquery")
select emp.employeename, emp.salary, emp.fkdeptid from
employee_sarda emp
where salary =
(select max(salary) from employee_sarda maxsal
where emp.fkdeptid = maxsal.fkdeptid)
|||sorry other option may be like this
select salary , employeename, fkdeptid from employee_sarda e where
salary in (select max(salary) from employee_sarda t where e.fkdeptid =
t.fkdeptid )
or
select salary , employeename, fkdeptid from employee_sarda e where
exists (select max(salary),fkdeptid from employee_sarda t group by
fkdeptid having max(t.salary)= e.salary and e.fkdeptid = t.fkdeptid )
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***
|||Sorry other option may be like this
select salary , employeename, fkdeptid from employee_sharda e where
salary in (select max(salary) from employee_sharda t1 where t1.fkdeptid
= e.fkdeptid )
or
select salary , employeename, fkdeptid from employee_sharda e where
exists (select max(salary),fkdeptid from employee_sharda t1 group by
fkdeptid having max(t1.salary)= e.salary and t1.fkdeptid = e.fkdeptid )
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***
|||Hello Amish,
Great ones, too.
Shalll we now elaborate on the "query plans" of the optimizer for all
these
to determine which is the "quickest" (best ?) :-))
sql

How Can I retrieve data from a self joined table

hi all
i have one table with the format
PKEmployeeID PrimaryKey
FKDeptID
Salary
EmployeeName
FKEmployeeID
here I have to retrieve the max salary by dept wise
thats ok by writing
Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
How can i retrieve the employeename for the max salary
Thanks and Regards
Sarada VIt may be possible in same FKDeptID two or more employee have same
salary which is max in that FKDeptID.
Select max(salary), employeename, FKDeptID from employee_sarda group by
employeename,FKDeptID.
or
if you want only one name then
Select max(salary), max(employeename), FKDeptID from employee_sarda
group by FKDeptID.
Please post ddl with data to get better idea.
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***|||Hi
Can you provide us with your table's stucture?
--Written by Itzik Ben-Gan
CREATE TABLE Employees
(
empid int NOT NULL,
mgrid int NULL,
empname varchar(25) NOT NULL,
salary money NOT NULL,
CONSTRAINT PK_Employees_empid PRIMARY KEY(empid),
CONSTRAINT FK_Employees_mgrid_empid
FOREIGN KEY(mgrid)
REFERENCES Employees(empid)
)
CREATE INDEX idx_nci_mgrid ON Employees(mgrid)
INSERT INTO Employees VALUES(1 , NULL, 'Nancy' , $10000.00)
INSERT INTO Employees VALUES(2 , 1 , 'Andrew' , $5000.00)
INSERT INTO Employees VALUES(3 , 1 , 'Janet' , $5000.00)
INSERT INTO Employees VALUES(4 , 1 , 'Margaret', $5000.00)
INSERT INTO Employees VALUES(5 , 2 , 'Steven' , $2500.00)
INSERT INTO Employees VALUES(6 , 2 , 'Michael' , $2500.00)
INSERT INTO Employees VALUES(7 , 3 , 'Robert' , $2500.00)
INSERT INTO Employees VALUES(8 , 3 , 'Laura' , $2500.00)
INSERT INTO Employees VALUES(9 , 3 , 'Ann' , $2500.00)
INSERT INTO Employees VALUES(10, 4 , 'Ina' , $2500.00)
INSERT INTO Employees VALUES(11, 7 , 'David' , $2000.00)
INSERT INTO Employees VALUES(12, 7 , 'Ron' , $2000.00)
INSERT INTO Employees VALUES(13, 7 , 'Dan' , $2000.00)
INSERT INTO Employees VALUES(14, 11 , 'James' , $1500.00)
GO

> Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
> How can i retrieve the employeename for the max salary
CREATE FUNCTION dbo.ufn_GetSubtreeSalary
(
@.mgrid AS int
)
RETURNS int
AS
BEGIN
RETURN (SELECT Salary
FROM Employees WHERE empid = @.mgrid) +
CASE
WHEN EXISTS(SELECT * FROM Employees WHERE mgrid = @.mgrid) THEN
(SELECT SUM(dbo.ufn_GetSubtreeSalary(empid))
FROM Employees
WHERE mgrid = @.mgrid)
ELSE 0
END
END
GO
SELECT dbo.ufn_GetSubtreeSalary(3)
GO
If it doesnot help , pls show us an expected result
"pandu" <saradabhanuv@.gmail.com> wrote in message
news:1135835404.968676.316380@.g14g2000cwa.googlegroups.com...
> hi all
> i have one table with the format
> PKEmployeeID PrimaryKey
> FKDeptID
> Salary
> EmployeeName
> FKEmployeeID
> here I have to retrieve the max salary by dept wise
> thats ok by writing
> Select max(salary),FKDeptID from Employee_Sarada group by FKDeptID
> How can i retrieve the employeename for the max salary
> Thanks and Regards
> Sarada V
>|||hi Amish
thanks for ur reply
but,for ur first query
Select max(salary), employeename, FKDeptID from employee_sarda group by
employeename,FKDeptID
its just listing all the records
and for ur second query
Select max(salary), max(employeename), FKDeptID from employee_sarda
group by FKDeptID
but whats it meant by max(employeename) its giving the salary field
correctly
but it only selects the first employeename not the required employeename
the required empname is the employeename corresponding to max salary in
that particular dept
Thanks and regards
Sarada v
*** Sent via Developersdex http://www.codecomments.com ***|||hi
this is the table structure
PKEmployeeID bigint
EmployeeName varchar
FKDeptID bigint
Salary decimal
PhoneNumber varchar
FKEmployeeID bigint
and i dont know anything about creating functions and all those stuff
i am loooking for a simple query
Can u help me plz
Thanks and Regards
Sarada V
*** Sent via Developersdex http://www.codecomments.com ***|||Try this: ("nested table" techniqe)
select emp.employeename, maxsal.salary1, maxsal.fkdeptid from
employee_sarda,
( Select max(salary) as salary1, FKDeptID from employee_sarda
group by FKDeptID ) as maxsal
where
emp.FKDeptid = maxsal.FKDeptid
emp.salary = maxsal.salary
Note: If 2 or more guys of a dept. earn the same maximum, you'll get
back all of them.
Is this from some exam :-) '|||Or this ("correlated subquery")
select emp.employeename, emp.salary, emp.fkdeptid from
employee_sarda emp
where salary =
(select max(salary) from employee_sarda maxsal
where emp.fkdeptid = maxsal.fkdeptid)|||sorry other option may be like this
select salary , employeename, fkdeptid from employee_sarda e where
salary in (select max(salary) from employee_sarda t where e.fkdeptid =
t.fkdeptid )
or
select salary , employeename, fkdeptid from employee_sarda e where
exists (select max(salary),fkdeptid from employee_sarda t group by
fkdeptid having max(t.salary)= e.salary and e.fkdeptid = t.fkdeptid )
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***|||Sorry other option may be like this
select salary , employeename, fkdeptid from employee_sharda e where
salary in (select max(salary) from employee_sharda t1 where t1.fkdeptid
= e.fkdeptid )
or
select salary , employeename, fkdeptid from employee_sharda e where
exists (select max(salary),fkdeptid from employee_sharda t1 group by
fkdeptid having max(t1.salary)= e.salary and t1.fkdeptid = e.fkdeptid )
Regards
Amish
*** Sent via Developersdex http://www.codecomments.com ***|||Hello Amish,
Great ones, too.
Shalll we now elaborate on the "query plans" of the optimizer for all
these
to determine which is the "quickest" (best ?) :-))

Wednesday, March 28, 2012

How can I recover suspect Database , SQL 2005

Hi all
I have e big problem , I have a database in SQL 2005 which is mark as
suspect. I think the main reason for this is that someone have stopped tha
database engine , while it has been working. Now the database is shown as
suspect. I try to execute several queries, but nothing helped. I think that
the database log file is broken , when I try to Attach the database on
another server I get message that there is a missing record in the database.
Can someone help to recover the database , or to get the information from it
?
Plamen,
you might want PSS to work through this with you, but the essential points
are listed here: http://www.myitforum.com/articles/18/view.asp?id=7381
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .
|||Hi
http://www.karaszi.com/SQLServer/info_corrupt_suspect_db.asp
"Plamen Assenov" <Plamen Assenov@.discussions.microsoft.com> wrote in message
news:88E41664-0265-4066-8FE9-450546FBCAAB@.microsoft.com...
> Hi all
> I have e big problem , I have a database in SQL 2005 which is mark as
> suspect. I think the main reason for this is that someone have stopped tha
> database engine , while it has been working. Now the database is shown as
> suspect. I try to execute several queries, but nothing helped. I think
> that
> the database log file is broken , when I try to Attach the database on
> another server I get message that there is a missing record in the
> database.
> Can someone help to recover the database , or to get the information from
> it
> ?

How can I recover suspect Database , SQL 2005

Hi all
I have e big problem , I have a database in SQL 2005 which is mark as
suspect. I think the main reason for this is that someone have stopped tha
database engine , while it has been working. Now the database is shown as
suspect. I try to execute several queries, but nothing helped. I think that
the database log file is broken , when I try to Attach the database on
another server I get message that there is a missing record in the database.
Can someone help to recover the database , or to get the information from it
?Plamen,
you might want PSS to work through this with you, but the essential points
are listed here: http://www.myitforum.com/articles/18/view.asp?id=7381
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .|||Hi
http://www.karaszi.com/SQLServer/in..._suspect_db.asp
"Plamen Assenov" <Plamen Assenov@.discussions.microsoft.com> wrote in message
news:88E41664-0265-4066-8FE9-450546FBCAAB@.microsoft.com...
> Hi all
> I have e big problem , I have a database in SQL 2005 which is mark as
> suspect. I think the main reason for this is that someone have stopped tha
> database engine , while it has been working. Now the database is shown as
> suspect. I try to execute several queries, but nothing helped. I think
> that
> the database log file is broken , when I try to Attach the database on
> another server I get message that there is a missing record in the
> database.
> Can someone help to recover the database , or to get the information from
> it
> ?

Monday, March 12, 2012

How Can i know how many USERS are in agroup.

HI all
I just wana know that is there is any TSQL Statement which will give me
the list of all user groups,names of user in each group and thier
Permssions in any database/server.
Actually I have to create a report in wich i have to show that how
many groups we have any whi is the member of wich group with such
permissions.I ckeck through Interprise Manager but i guess it will take
alot of time to note every user group then users and their permissions.
So please any HELP will be greatly Appriciated.

Thanks

Arman(ch.adilaziz@.gmail.com) writes:

Quote:

Originally Posted by

I just wana know that is there is any TSQL Statement which will give me
the list of all user groups,names of user in each group and thier
Permssions in any database/server.
Actually I have to create a report in wich i have to show that how
many groups we have any whi is the member of wich group with such
permissions.I ckeck through Interprise Manager but i guess it will take
alot of time to note every user group then users and their permissions.
So please any HELP will be greatly Appriciated.


Before I try anything, I would like some clarification. With "groups" do
you mean what is normally called "roles" in SQL Server? That is grouping
of users within a database?

Or do you mean Windows groups as in DOMAIN\OFFICEUSERS?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:

Quote:

Originally Posted by

Before I try anything, I would like some clarification. With "groups" do
you mean what is normally called "roles" in SQL Server? That is grouping
of users within a database?
>
Or do you mean Windows groups as in DOMAIN\OFFICEUSERS?


Even if Arman meant roles, I'd like to ask you similar question
according to Windows groups. Is it possible to list Windows group
members using T-SQL? Let's assume that I'm a sysadmin, but not domain
admin.

--
Best regards,
Marcin Guzowski
http://guzowski.info|||Marcin A. Guzowski wrote:

Quote:

Originally Posted by

Erland Sommarskog wrote:

Quote:

Originally Posted by

Before I try anything, I would like some clarification. With "groups" do
you mean what is normally called "roles" in SQL Server? That is grouping
of users within a database?


Question #1:

Answer : Yes

sp_helpuser

Quote:

Originally Posted by

Quote:

Originally Posted by

Or do you mean Windows groups as in DOMAIN\OFFICEUSERS?


>
>
Even if Arman meant roles, I'd like to ask you similar question
according to Windows groups. Is it possible to list Windows group
members using T-SQL? Let's assume that I'm a sysadmin, but not domain
admin.
>
>


Question #2:

Answer: Yes, set up a linked server with your pdc server / active
directory and use open query to query this information from the linked
server.

http://www.fits-consulting.de/blog/...2e73b00faa.aspx

Quote:

Originally Posted by

--
Best regards,
Marcin Guzowski
http://guzowski.info

|||jebuskrust@.gmail.com wrote:

Quote:

Originally Posted by

Question #2:
>
Answer: Yes, set up a linked server with your pdc server / active
directory and use open query to query this information from the linked
server.
>
http://www.fits-consulting.de/blog/...2e73b00faa.aspx


I found much easier way:
exec master.dbo.xp_logininfo 'DOMAIN\GROUP','members'

It will list all members of desired Windows group*, without the need
of setting up a linked server.
[*] only groups granted access to SQL Server are concerned

--
Best regards,
Marcin Guzowski
http://guzowski.info

Friday, February 24, 2012

How Can I get hte return_Value?

Hi all~~~

I had used sqlDataSource + SQLserver procedure

The procedure like this:

...................

BEGIN

SELECT * FROM xxx

RETURN 1

END

er~~~How can I get the return_value with sqlDataSource ??

Create a parameter, and set the Direction property to ReturnValue|||Create a parameter??Would you make an example? Thanks~~|||

SqlConnection cn =newSqlConnection(MyConnectionString);

SqlCommand cmd =newSqlCommand("spName", cn);

cmd.CommandType =CommandType.StoredProcedure;

SqlParameter prm=new SqlParameter("@.ReturnValue");

prm.Direction=ReturnValue;

cmd.Parameters.Add(prm);

// Now .ExecuteNonQuery, ExecuteReader, etc. on the command.

|||Hidouglas.reilly ~It without SqlDataSourc......