Groups | Blog | Home
all groups > sql server programming > march 2005 >

sql server programming : What is the query for this problem ?



Kristanto
3/3/2005 7:40:54 PM
Hi All,
I have to count the total placement of a particular employee to each
branches in one year.

The expected resultset is like this :

Employee BranchCode Total_Placement :
--------------------------------------
1001 100 2
1001 200 1
.... etc..etc

From the data below :
Create Table Summary (
EmpID Int,
MonthID TinyInt,
BranchCode SmallInt)

Insert Into Summary Values (1001,1,100)
Insert Into Summary Values (1001,2,100)
Insert Into Summary Values (1001,3,100)
Insert Into Summary Values (1001,4,100)
Insert Into Summary Values (1001,5,100)
Insert Into Summary Values (1001,6,100)
Insert Into Summary Values (1001,7,200)
Insert Into Summary Values (1001,8,200)
Insert Into Summary Values (1001,9,200)
Insert Into Summary Values (1001,10,100)
Insert Into Summary Values (1001,11,100)
Insert Into Summary Values (1001,12,100)

What is the query statement to get the above result-set ?

Thank you for your help,
Krist

*** Sent via Developersdex http://www.developersdex.com ***
Vishal Khajuria
3/3/2005 9:05:04 PM
Hi,
Try this
select EmpId,BranchCode,count(*) as Total_Placements from summary group by
EmpId,BranchCode
Vishal Khajuria
SUNGARD SCT

[quoted text, click to view]
CBretana
3/3/2005 10:37:01 PM
Select EmpID, BranchCode, Count(*)
From Summary S
Where Not Exists (Select * From Summary
Where EmpID = S.EmpID
And BranchCode = S.BranchCode
And MonthID = S.MonthID - 1)
Group By EmpID, BranchCode



[quoted text, click to view]
Baiju
3/4/2005 11:37:50 AM
Hi krist
Your question is not clear. Please describe with all fields and other
details

Thanks
Baiju
[quoted text, click to view]

Krist
3/4/2005 12:30:16 PM
Hi,
Thanks, but that is not the case of simple grouping.
From the data I supply here, the expected 'placement value' for employee
1001 is :
2 for branch 100
1 for branch 200.
It means :
he has been placed Twice during the year in branch 100 (i.e : january-june
and october-december),
once in branch 200 (i.e: july-september).

Is this possible without using T-SQL LOOPING ?

Thank you for any help,
Krist



"Vishal Khajuria" <VishalKhajuria@discussions.microsoft.com> wrote in
message news:3F2DA3FB-7674-4296-BE2D-55024CF3EFB9@microsoft.com...
[quoted text, click to view]

Krist
3/4/2005 1:15:36 PM
Hi,
I am sorry, may be its because of my english ..
Here is the case :
During a year, am employee (e.g employee ID = 1001) may move from one branch
to another. E.g :
- From January to June, he is placed at branch 100.
- From July to September, he is placed at branch 200
- From October to December, back to branch 100.
So as the summary for Employee ID = 1001 is :
Branch 100, 2 placement
Branch 200, 1 placement.

What is the query for to get result ?

Here is the data :

Create Table Summary (
EmpID Int,
MonthID TinyInt,
BranchCode SmallInt)

Insert Into Summary Values (1001,1,100)
Insert Into Summary Values (1001,2,100)
Insert Into Summary Values (1001,3,100)
Insert Into Summary Values (1001,4,100)
Insert Into Summary Values (1001,5,100)
Insert Into Summary Values (1001,6,100)
Insert Into Summary Values (1001,7,200)
Insert Into Summary Values (1001,8,200)
Insert Into Summary Values (1001,9,200)
Insert Into Summary Values (1001,10,100)
Insert Into Summary Values (1001,11,100)
Insert Into Summary Values (1001,12,100)

Thank you,
Krist




[quoted text, click to view]

Krist
3/4/2005 2:07:04 PM
YES, :)
Thats it.
Thank you for your help,
Krist


[quoted text, click to view]

AddThis Social Bookmark Button