Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help in query !!!!! Help!!

Author  Topic 

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-09 : 09:23:26
Hi,

I have one history table with those information:

Emp Dept Change_date (the date when the employee changed his department)
2899 632 200901
2899 637 200903
2899 632 200908
2899 723 200908

But I need to know for each month on 2009, in which dept this employee was working.

How can I do this with query? I only have the date when the employee changed.

I would like to have this:


Emp Dept Change_date Date
2899 632 200901 200901
2899 632 200901 200902
2899 637 200903 200903
2899 637 200903 200904
2899 637 200903 200905
2899 637 200903 200906
2899 637 200903 200907
2899 632 200908 ->I Dont need this,only the max in the month. The employee changed twice in the same month.
2899 723 200908 200908
2899 723 200908 200909
2899 723 200908 200910
2899 723 200908 200911
2899 723 200908 200912

Could you help me, please?

Thanks.

Regards,

Santana

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-09 : 10:20:06
quote:
Originally posted by santana

Hi,

I have one history table with those information:

Emp Dept Change_date (the date when the employee changed his department)
2899 632 200901
2899 637 200903
2899 632 200908
2899 723 200908

But I need to know for each month on 2009, in which dept this employee was working.

How can I do this with query? I only have the date when the employee changed.

I would like to have this:


Emp Dept Change_date Date
2899 632 200901 200901
2899 632 200901 200902
2899 637 200903 200903
2899 637 200903 200904
2899 637 200903 200905
2899 637 200903 200906
2899 637 200903 200907
2899 632 200908 ->I Dont need this,only the max in the month. The employee changed twice in the same month.
2899 723 200908 200908
2899 723 200908 200909
2899 723 200908 200910
2899 723 200908 200911
2899 723 200908 200912

Could you help me, please?

Thanks.

Regards,

Santana




Select * from
(
select emp,dept,change_date,row_number()over(partition by emp,dept order by change_date desc)as seq from tblanme

)as t where t.seq <=2

first execute the above qry.
Then apply the where condition based on your expecteation please
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-10 : 08:26:31
Hi,

could you help me again, please?
Because I dont know what I did wrong.

I run this query:

Select * from
(
select emp,dep,changedate,row_number()over(partition by emp,dep
order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE'

)as t where t.seq <=2 and emp in( '2899','3023')

and I receive this result:

emp dep changedate seq
2899 632 2009-08 1
2899 637 2009-03 2
2899 632 2009-01 1
2899 723 2009-08 1

How can I use this seq?

Sorry I don't understand!!!

Thanks a lot!

Regards
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-10 : 09:11:20
quote:
Originally posted by santana

Hi,

could you help me again, please?
Because I dont know what I did wrong.

I run this query:

Select * from
(
select emp,dep,changedate,row_number()over(partition by emp,dep
order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE'

)as t where t.seq <=2 and emp in( '2899','3023')

and I receive this result:

emp dep changedate seq
2899 632 2009-08 1
2899 637 2009-03 2
2899 632 2009-01 1
2899 723 2009-08 1

How can I use this seq?

Sorry I don't understand!!!

Thanks a lot!

Regards




Please run this portion alone and see first and then apply the filter condtion in seq..



select * from
(
select emp,dep,changedate,row_number()over(partition by emp,dep
order by changedate desc)as seq from [HISTORY] where fieldname = 'COSTCODE'

)as t where emp in( '2899','3023')
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-11 : 08:18:45
Hi,

Maybe there are no way to have what I need by query.

Are there any way that I could populate this table?
In the table I have only the record when I insert / change the department by employee - the date when the department has changed by employee.
I would like to populate this table with the months between the alterations.


eg.: I know that the employee has department 632 at 200901, 633 after 200903 and 723 after 200908.
I would like to populate the department between 200901 and 200902 with 632, between 200903 and 200907 with 633, and 200908 to now with with 723.
Is this possible? Can I do this? How?

I appreciate any help! Please!!!!!
Regards,

SANTANA

Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-11 : 08:24:07
quote:
Originally posted by santana

Hi,

Maybe there are no way to have what I need by query.

Are there any way that I could populate this table?
In the table I have only the record when I insert / change the department by employee - the date when the department has changed by employee.
I would like to populate this table with the months between the alterations.


eg.: I know that the employee has department 632 at 200901, 633 after 200903 and 723 after 200908.
I would like to populate the department between 200901 and 200902 with 632, between 200903 and 200907 with 633, and 200908 to now with with 723.
Is this possible? Can I do this? How?

I appreciate any help! Please!!!!!
Regards,

Renata





iam extremely sorry..honestly,faling to understand this.
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-11 : 08:36:30
Hi,

I have one table with

employee number ----- date when changed the department ------- new department
1010 200901 632 -> in 200901 the employee 1010 changed to department 632
1010 200903 633 -> in 200903 the employee 1010 changed to department 633
1010 200908 723 -> in 200908 the employee 1010 changed to department 723

But I need another table or one query, what show me which department the employee have for all months, and not only the month when he chaged.
I need to know for example, in 200902 he was in the departemt 632, and in 200904 in the departemtn 633.
I need to know for all months in 2009, in which department he was.
Like this:

employee number ----- date when changed the department ------- new departemtn
1010 200901 632 -> in 200901 the employee 1010 changed to department 632
1010 200902 632 -> in 200902 the employee 1010 changed to department 632

1010 200903 633 -> in 200903 the employee 1010 changed to department 633
1010 200904 633 -> in 200904 the employee 1010 changed to department 633
1010 200905 633 -> in 200905 the employee 1010 changed to department 633
1010 200906 633 -> in 200906 the employee 1010 changed to department 633
1010 200907 633 -> in 200907 the employee 1010 changed to department 633

1010 200908 723 -> in 200908 the employee 1010 changed to department 723
1010 200909 723 -> in 200909 the employee 1010 changed to department 723
1010 200910 723 -> in 200910 the employee 1010 changed to department 723
1010 200911 723 -> in 200911 the employee 1010 changed to department 723
1010 200912 723 -> in 200912 the employee 1010 changed to department 723

How can I do this??

I need help, PLEASE!!!!!PLEASE!!!!!PLEASE!!!!!PLEASE!!!!!

Thanks a lot!

Regards
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-11 : 11:22:55
[code]
DECLARE @Year int

SELECT @year=2009--your passed value

DECLARE @Calendar table
(
Period int
)

INSERT INTO @Calendar
SELECT (100 * y.[Year]) +m.[Month]
FROM (SELECT @year AS [Year]) y
CROSS JOIN (SELECT 1 AS [Month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
...
SELECT 12)m

SELECT r.Emp,c.Period,r.Dept
FROM @CALENDAR c
CROSS JOIN (SELECT t.Emp,t.Dept,t.Change_date as start,t1.Change_date AS End
FROM YourTable t
OUTER APPLY (SELECT TOP 1 Change_Date
FROM YourTable
WHERE Emp=t.Emp
AND Dept <> t.Dept
ORDER BY Change_Date ASC)t1
)r
WHERE c.Period BETWEEN r.start AND COALESCE(r.End,r.start)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-17 : 09:37:46
Hi,

thanks for your help.
I created the query below, but didn't return any value.
What did I do wrong? Can I see the table Calendar or only the query result?

newvalue = dept
empref = emp

DECLARE @Year int

SELECT @year=2009--your passed value

DECLARE @Calendar table
(
Period int
)

INSERT INTO @Calendar
SELECT (100 * y.[Year]) +m.[Month]
FROM (SELECT @year AS [Year]) y
CROSS JOIN (SELECT 1 AS [Month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12)m

SELECT r.Empref,c.Period,r.[NEWVALUE]
FROM @CALENDAR c
CROSS JOIN (SELECT t.Empref,t.[NEWVALUE],t.CHANGEDATE as start,t1.CHANGEDATE AS [End]
FROM [tmsdata].[tmsuser].[HISTORY] t --where t.empref = '2899' and t.fieldname = 'COSTCODE'
OUTER APPLY (SELECT TOP 1 CHANGEDATE
FROM [tmsdata].[tmsuser].[HISTORY]
WHERE Empref=t.Empref
AND [NEWVALUE] <> t.[NEWVALUE]
and empref = '2899'
and fieldname = 'COSTCODE'
ORDER BY CHANGEDATE ASC)t1
)r
WHERE c.Period BETWEEN r.start AND COALESCE(r.[End],r.start)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 10:01:51
do you have records for empref = '2899'
and fieldname = 'COSTCODE'?
if not comment the conditions inside APPLY subquery also

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-17 : 10:49:44
Hi,

I run that:

DECLARE @Year int

SELECT @year=2009--your passed value

DECLARE @Calendar table
(
Period int
)

INSERT INTO @Calendar
SELECT (100 * y.[Year]) +m.[Month]
FROM (SELECT @year AS [Year]) y
CROSS JOIN (SELECT 1 AS [Month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12)m

SELECT r.Empref,c.Period,r.[NEWVALUE]
FROM @CALENDAR c
CROSS JOIN (SELECT t.Empref,t.[NEWVALUE],t.fieldname,
CONVERT(char(4),DATEPART(year,t.CHANGEDATE)) +
CASE WHEN DATEPART(month, t.CHANGEDATE) < 10
THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, t.CHANGEDATE)) as start,
t1.CHANGEDATE AS [End]

FROM [tmsdata].[tmsuser].[HISTORY] t
OUTER APPLY (SELECT TOP 1 CONVERT(char(4),DATEPART(year,CHANGEDATE)) +
CASE WHEN DATEPART(month, CHANGEDATE) < 10
THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, CHANGEDATE)) as CHANGEDATE

FROM [tmsdata].[tmsuser].[HISTORY]

WHERE Empref=t.Empref
AND [NEWVALUE] <> t.[NEWVALUE]
and t.fieldname = fieldname
and empref = '2899'
and fieldname = 'COSTCODE'
ORDER BY CONVERT(char(4),DATEPART(year,CHANGEDATE)) +
CASE WHEN DATEPART(month, CHANGEDATE) < 10
THEN ('0')ELSE ('')END + CONVERT(char(2),DATEPART(month, CHANGEDATE)) ASC)t1
)r
WHERE c.Period BETWEEN r.start AND COALESCE(r.[End],r.start)
and empref = '2899' and fieldname = 'COSTCODE

and I received only one line into the result:

empref period Dept
2899 200901 632

But I know that there are more that for this employee:

SELECT distinct [tmsdata].[tmsuser].[HISTORY].[EMPREF],[NEWVALUE],
CONVERT(char(4),DATEPART(year,CHANGEDATE)) +
CASE WHEN DATEPART(month, CHANGEDATE) < 10
THEN ('0')ELSE ('')END
+ CONVERT(char(2),DATEPART(month, CHANGEDATE)) as changedate
FROM [tmsdata].[tmsuser].[HISTORY]
where [tmsdata].[tmsuser].[HISTORY].empref = '2899'
and fieldname = 'COSTCODE'

empref newvalue changedate
2899 637 200901
2899 632 200908
2899 723 200908

What I did wrong?

Thank you for all!
Regards,
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-17 : 11:23:56
I need to use the fieldname because this is a history table, and I want to populate date only for dept = costcode.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 11:37:36
then remove TOP 1 inside subquery

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-17 : 12:12:01
If I remove the TOP 1 , I received this message:
Msg 1033, Level 15, State 1, Line 46
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.


And If I remove the ORDER BY, I received this result:

EMPREF DATE DEPT
3077 200903 639 --> WHERE ARE THE MONTHS BEFORE MARCH?
3077 200904 639
3077 200905 639
3077 200906 639
3077 200907 639
3077 200908 639
3077 200909 639
3077 200910 639
3077 200911 639 --> THE CORRECT FOR 200911 IS 642

I changed the example (see below). I chose the employee 3077. This guy only change once and is easy understand what happen in the query.

empref newvalue changedate
3077 639 200903
3077 642 200911

Thanks!

Regards,
Santana
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 13:10:59
do you mean you need months before first appearance in history table also?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-17 : 13:47:34
Hi,

first thank you for your help.

My period start in 2008 until now.

When the employee start in the company, the manager include into the history table your department, and If the employee change the department, the manager include into the History table, the new department and the data_change.
Into the history table I have for each employee, the department and when included that.
But I need that information for all month since the employee start in the company until now.

For example: for employee 3077.
He started in 200903 in the department 639, but in 200911 the employee change for department 642.

I need to see this:

between 200903 and 200910 -> 639
after 200911 until now -> 642

like:

200903 --- 639
200904 --- 639
200905 --- 639
200906 --- 639
200907 --- 639
200908 --- 639
200909 --- 639
200910 --- 639
200911 --- 642
200912 --- 642
201001 --- 642
201002 --- 642
201003 --- 642

Can I do this? Is it possible?

Regards,

santana
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 13:57:35
then wont this be enough?

DECLARE @Year int

SELECT @year=2009--your passed value

DECLARE @Calendar table
(
Period int
)

INSERT INTO @Calendar
SELECT (100 * y.[Year]) +m.[Month]
FROM (SELECT @year AS [Year]) y
CROSS JOIN (SELECT 1 AS [Month] UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
...
SELECT 12)m

SELECT r.Emp,c.Period,r.Dept
FROM @CALENDAR c
CROSS JOIN (SELECT t.Emp,t.Dept,t.Change_date as start,t1.Change_date AS End
FROM YourTable t
OUTER APPLY (SELECT TOP 1 Change_Date
FROM YourTable
WHERE Emp=t.Emp
AND Dept <> t.Dept
ORDER BY Change_Date ASC)t1
)r
WHERE c.Period BETWEEN r.start AND COALESCE(r.End-1,r.start)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-18 : 05:19:18
Didn't work.

Emp Date Dept
3077 200903 639
3077 200904 639
3077 200905 639
3077 200906 639
3077 200907 639
3077 200908 639
3077 200909 639
3077 200910 639

The only difference: this query excluded the last line/row/register (3077 200911 639 --> THE CORRECT FOR 200911 IS 642)

I don't have any idea How can I do this......

Thank you a lot for your help...

How can I do this? I need to populate this table or other with those information. Maybe something with LOOP.

Regards,
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-18 : 06:28:06
I need something like this:

While date for greater or equal to 200903 (change_date) then dept receive 639
else
while date for greater or equal to 200911 (change_date) then department receive 642
...

This for the example

empref dept changedate
3077 639 200903
3077 642 200911

??????

Thanks
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-18 : 06:52:04
quote:
Originally posted by santana

I need something like this:

While date for greater or equal to 200903 (change_date) then dept receive 639
else
while date for greater or equal to 200911 (change_date) then department receive 642
...

This for the example

empref dept changedate
3077 639 200903
3077 642 200911

??????

Thanks



try something like this,
dept=case when date >='200903' then 639
else
when date >='200911' then 642 end
Go to Top of Page

santana
Yak Posting Veteran

72 Posts

Posted - 2010-03-18 : 07:07:51
But in this case, I need to fixed the date and the department in the query?

Because that for me is only one example. I have more dates, employees and departments into my table.

Go to Top of Page
    Next Page

- Advertisement -