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.
| Author |
Topic |
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-08 : 20:44:29
|
| hi need help to this problemi have table employeei need to change to the employee Once a month the number from 1 to 3 to 9 (once a month)in the first day of the month evry monthand loop back to 1 .3 .9evry month next number from 1 to 3 3 to 9 9 to 1 and loop this is my table empid unit unit_date-----------------------------------------------------------11111 1 01/07/20082222 3 01/07/20083333 9 01/07/2008 i need to see it like this (on this month) empid unit unit_date new_unit--------------------------------------------------------------------------------11111 1 01/07/2008 12222 3 01/07/2008 33333 9 01/07/2008 9---------------------------------------------------------------------------------------------------------------now next month '01/08008' empid unit unit_date new_unit--------------------------------------------------------------------------------11111 1 01/08/2008 32222 3 01/08/2008 93333 9 01/08/2008 1how tnx |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-07-08 : 22:57:11
|
| I would just derrive it from the starting values and the date. No need to update anything. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-08 : 23:15:20
|
[code]DECLARE @TABLE TABLE( empid int, unit int, unit_date datetime)INSERT INTO @TABLESELECT 1111, 1, '20080701' UNION ALLSELECT 2222, 3, '20080701' UNION ALLSELECT 3333, 9, '20080701'DECLARE @unit TABLE( id int, unit int)INSERT INTO @unitSELECT 1, 1 UNION ALLSELECT 2, 3 UNION ALLSELECT 3, 9DECLARE @today datetimeSELECT @today = '2008-07-09'SELECT t.empid, t.unit, t.unit_date, new_unit = power(3, (u.id + DATEDIFF(MONTH, unit_date, @today) - 1) % 3)FROM @TABLE t INNER JOIN @unit u ON t.unit = u.unit/*empid unit unit_date new_unit ----------- ----------- ----------- ----------- 1111 1 2008-07-01 1 2222 3 2008-07-01 3 3333 9 2008-07-01 9 */SELECT @today = '2008-08-08'SELECT t.empid, t.unit, t.unit_date, new_unit = power(3, (u.id + DATEDIFF(MONTH, unit_date, @today) - 1) % 3)FROM @TABLE t INNER JOIN @unit u ON t.unit = u.unit/*empid unit unit_date new_unit ----------- ----------- ----------- ----------- 1111 1 2008-07-01 3 2222 3 2008-07-01 9 3333 9 2008-07-01 1 */SELECT @today = '2008-09-19'SELECT t.empid, t.unit, t.unit_date, new_unit = power(3, (u.id + DATEDIFF(MONTH, unit_date, @today) - 1) % 3)FROM @TABLE t INNER JOIN @unit u ON t.unit = u.unit/*empid unit unit_date new_unit ----------- ----------- ----------- ----------- 1111 1 2008-07-01 9 2222 3 2008-07-01 1 3333 9 2008-07-01 3 */[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-09 : 02:42:11
|
| TNX TNXquestion how can i do it with the same logic with only 1,3tnx for all |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 02:44:42
|
1, 3 is easier. You can just simply usecase when unit = 1 then 3 when unit = 3 then 1 end KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-09 : 04:19:36
|
please whare to put itand howi must to use %2it only tow numbers 1,3SELECT @today = '2008-07-09'SELECT t.empid, t.unit, t.unit_date, new_unit = power(2????, (u.id + DATEDIFF(MONTH, unit_date, @today) - 1) % 2 ????)FROM @TABLE t INNER JOIN @unit u ON t.unit = u.unit TNX foe the help |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 04:25:49
|
if you only have 1 and 3 why must you make your query so complicated ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-09 : 05:01:52
|
| tnxbut how can i know what be in the future 1 OR 3 ?for examplewhat be the value in 01/11/2008 or 01/12/2008 ???TNX for all |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 07:19:54
|
for only 2 value 1 or 3, just find the number of month in-between and if odd then . .. if even then . . . KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 07:36:38
|
Changed the script a bit. Making use of a @unit table. Change the value of @no_units and run and see the result
DECLARE @TABLE TABLE( empid int, unit int, unit_date datetime)INSERT INTO @TABLESELECT 1111, 1, '20080701' UNION ALLSELECT 2222, 3, '20080701' UNION ALLSELECT 3333, 9, '20080701'DECLARE @unit TABLE( id int, unit int)INSERT INTO @unitSELECT 0, 1 UNION ALLSELECT 1, 3 UNION ALLSELECT 2, 9 UNION ALLSELECT 3, 11 UNION ALLSELECT 4, 13DECLARE @no_units intSELECT @no_units = 2SELECT t.empid, t.unit, t.unit_date, new_date = today, new_unit = n.unitFROM @TABLE t CROSS JOIN ( SELECT today = '2008-07-09' UNION ALL SELECT today = '2008-08-03' UNION ALL SELECT today = '2008-09-23' UNION ALL SELECT today = '2008-10-27' UNION ALL SELECT today = '2008-11-15' UNION ALL SELECT today = '2008-12-31' ) d INNER JOIN @unit u ON t.unit = u.unit INNER JOIN @unit n ON n.id = (u.id + DATEDIFF(MONTH, unit_date, today)) % @no_unitsORDER BY empid, unit, unit_date, new_date/*empid unit unit_date new_date new_unit ----------- ----------- ----------- ---------- ----------- 1111 1 2008-07-01 2008-07-09 1 1111 1 2008-07-01 2008-08-03 3 1111 1 2008-07-01 2008-09-23 1 1111 1 2008-07-01 2008-10-27 3 1111 1 2008-07-01 2008-11-15 1 1111 1 2008-07-01 2008-12-31 3 2222 3 2008-07-01 2008-07-09 3 2222 3 2008-07-01 2008-08-03 1 2222 3 2008-07-01 2008-09-23 3 2222 3 2008-07-01 2008-10-27 1 2222 3 2008-07-01 2008-11-15 3 2222 3 2008-07-01 2008-12-31 1 3333 9 2008-07-01 2008-07-09 1 3333 9 2008-07-01 2008-08-03 3 3333 9 2008-07-01 2008-09-23 1 3333 9 2008-07-01 2008-10-27 3 3333 9 2008-07-01 2008-11-15 1 3333 9 2008-07-01 2008-12-31 3 (18 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-09 : 09:54:56
|
wow you are genius TNX!please can you remove the do i must this "CROSS JOIN" ?CROSS JOIN ( SELECT today = '2008-07-09' UNION ALL SELECT today = '2008-08-03' UNION ALL SELECT today = '2008-09-23' UNION ALL SELECT today = '2008-10-27' UNION ALL SELECT today = '2008-11-15' UNION ALL SELECT today = '2008-12-31' ) d i don't need this field "new_date"i need to put your code in a view tnx for your help and support |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 10:00:59
|
yes. You may removed it and replace today with the actual var or column name. The cross join is just to test few months in a single query rather than one by one as post previously. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
midan1
Starting Member
39 Posts |
Posted - 2008-07-09 : 10:14:16
|
like this ?DECLARE @unit TABLE( id int, unit int)INSERT INTO @unitSELECT 0, 1 UNION ALLSELECT 1, 3 UNION ALLSELECT 2, 9 UNION ALLSELECT 3, 11 UNION ALLSELECT 4, 13DECLARE @no_units intSELECT @no_units = 2SELECT t.empid, t.unit, t.unit_date, new_date = today, new_unit = n.unitFROM [nili8].[dbo].[v_emp9] t CROSS JOIN ( SELECT today = '01/08/2008' ---- do i must this ??????????????????????? ) d INNER JOIN @unit u ON t.unit = u.unit INNER JOIN @unit n ON n.id = (u.id + DATEDIFF(MONTH, unit_date, today)) % @no_unitsORDER BY empid, unit, unit_date, new_date TNX |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-09 : 19:31:49
|
[code]declare @today datetimeselect @today = '20080801' -- or use getdate() if you are using current dateSELECT t.empid, t.unit, t.unit_date, new_date = @today, new_unit = n.unitFROM [nili8].[dbo].[v_emp9] t CROSS JOIN ( SELECT today = '01/08/2008' ---- do i must this ??????????????????????? ) d INNER JOIN @unit u ON t.unit = u.unit INNER JOIN @unit n ON n.id = (u.id + DATEDIFF(MONTH, unit_date, @today)) % @no_unitsORDER BY empid, unit, unit_date, new_date[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|