補日期數據!

xiye_jfb 2014-05-08 07:52:45
數據:條件是2014-04-01 到 2014-04-04 這樣要根據emp_id補上缺少的日期數據
條件是動態的可能是2014-04-01 到 2014-04-30,動態的補上缺少的日期
emp_id y_year m_month d_day
42 2014 4 1
42 2014 4 3
42 2014 4 4
55 2014 4 2
55 2014 4 3
66 2014 4 1
66 2014 4 2
66 2014 4 3
66 2014 4 4
結果:
emp_id y_year m_month d_day
42 2014 4 1
42 2014 4 2
42 2014 4 3
42 2014 4 4
55 2014 4 1
55 2014 4 2
55 2014 4 3
55 2014 4 4
66 2014 4 1
66 2014 4 2
66 2014 4 3
66 2014 4 4
...全文
119 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiye_jfb 2014-05-09
  • 打赏
  • 举报
回复
還是解決不了。 emp_id y_year m_month d_day cause 42 2014 4 1 a 42 2014 4 3 b 42 2014 4 4 c 55 2014 4 2 test 55 2014 4 3 a 66 2014 4 1 66 2014 4 2 b 66 2014 4 3 aa 66 2014 4 4 我還有一個列有數據的,這樣日期補上了,但是其他列的數據沒對應上。
引用 2 楼 yoan2014 的回复:
/************************************************************
 * Code formatted by SoftTree SQL Assistant ?v6.5.278
 * Time: 2014/5/8 20:09:38
 ************************************************************/

IF EXISTS(
       SELECT NAME
       FROM   sys.objects AS o
       WHERE  NAME = 'test'
   )
    DROP TABLE test
GO
CREATE TABLE test
(
	emp_id      INT,
	y_year      CHAR(4),
	m_month     CHAR(02),
	d_day       CHAR(02)
)
GO
INSERT INTO test
SELECT 42,       '2014',       '4',       '1' UNION ALL
SELECT 42,       '2014',       '4',       '3' UNION ALL
SELECT 42,       '2014',       '4',       '4' UNION ALL
SELECT 55,       '2014',       '4',       '2' UNION ALL
SELECT 55,       '2014',       '4',       '3' UNION ALL
SELECT 66,       '2014',       '4',       '1' UNION ALL
SELECT 66,       '2014',       '4',       '2' UNION ALL
SELECT 66,       '2014',       '4',       '3' UNION ALL
SELECT 66,       '2014',       '4',       '4'
GO
SELECT t.emp_id,
       DATEPART(YEAR, b.day1),
       DATEPART(MONTH, b.day1),
       DATEPART(DAY, b.day1)
FROM   (
           SELECT DATEADD(DAY, s.[number], '2014-04-01') AS day1
           FROM   [MASTER]..spt_values AS s
           WHERE  s.[type] = 'P'
                  AND s.[number] <= DATEDIFF(DAY, '2014-04-01', '2014-04-04')
       )  AS b,
       (
           SELECT DISTINCT emp_id
           FROM   test
       )  AS t 
*/
xiye_jfb 2014-05-09
  • 打赏
  • 举报
回复
還是解決不了。 emp_id y_year m_month d_day cause 42 2014 4 1 a 42 2014 4 3 b 42 2014 4 4 c 55 2014 4 2 test 55 2014 4 3 a 66 2014 4 1 66 2014 4 2 b 66 2014 4 3 aa 66 2014 4 4 我還有一個列有數據的,這樣日期補上了,但是其他列的數據沒對應上。
引用 1 楼 HEROWANG 的回复:
--> 测试数据: [ta]
if object_id('[ta]') is not null drop table [ta]
 go
 create table [ta] ([emp_id] int,[y_year] int,[m_month] int,[d_day] int)
insert into [ta]
select 42,2014,4,1 union all
select 42,2014,4,3 union all
select 42,2014,4,4 union all
select 55,2014,4,2 union all
select 55,2014,4,3 union all
select 66,2014,4,1 union all
select 66,2014,4,2 union all
select 66,2014,4,3 union all
select 66,2014,4,4

declare @startdate datetime,@enddate datetime
set @startdate='2014-4-1'
;with
wang as (select dd=DATEADD(day,number,@startdate) from master..spt_values where type='p' and number<30)

select emp_id,y_year,month(dd),DAY(dd) from wang cross join ta 

42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
55	2014	4	1
55	2014	4	2
55	2014	4	3
55	2014	4	4
55	2014	4	5
55	2014	4	6
55	2014	4	7
55	2014	4	8
55	2014	4	9
55	2014	4	10
55	2014	4	11
55	2014	4	12
55	2014	4	13
55	2014	4	14
55	2014	4	15
55	2014	4	16
55	2014	4	17
55	2014	4	18
55	2014	4	19
55	2014	4	20
55	2014	4	21
55	2014	4	22
55	2014	4	23
55	2014	4	24
55	2014	4	25
55	2014	4	26
55	2014	4	27
55	2014	4	28
55	2014	4	29
55	2014	4	30
55	2014	4	1
55	2014	4	2
55	2014	4	3
55	2014	4	4
55	2014	4	5
55	2014	4	6
55	2014	4	7
55	2014	4	8
55	2014	4	9
55	2014	4	10
55	2014	4	11
55	2014	4	12
55	2014	4	13
55	2014	4	14
55	2014	4	15
55	2014	4	16
55	2014	4	17
55	2014	4	18
55	2014	4	19
55	2014	4	20
55	2014	4	21
55	2014	4	22
55	2014	4	23
55	2014	4	24
55	2014	4	25
55	2014	4	26
55	2014	4	27
55	2014	4	28
55	2014	4	29
55	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
jayzhihui 2014-05-09
  • 打赏
  • 举报
回复
declare @n int =10 select distinct [emp_id],y_year,m_month,n as d_day from ta cross join (select top @n ROW_NUMBER()over(order by getdate()) n from sysobjects )b 动态指定参数吧
jayzhihui 2014-05-09
  • 打赏
  • 举报
回复
if object_id('[ta]') is not null drop table [ta] go create table [ta] ([emp_id] int,[y_year] int,[m_month] int,[d_day] int) insert into [ta] select 42,2014,4,1 union all select 42,2014,4,3 union all select 42,2014,4,4 union all select 55,2014,4,2 union all select 55,2014,4,3 union all select 66,2014,4,1 union all select 66,2014,4,2 union all select 66,2014,4,3 union all select 66,2014,4,4 select distinct [emp_id],y_year,m_month,n as d_day from ta cross join (select top 4 ROW_NUMBER()over(order by getdate()) n from sysobjects )b
yoan2014 2014-05-08
  • 打赏
  • 举报
回复
/************************************************************
 * Code formatted by SoftTree SQL Assistant ?v6.5.278
 * Time: 2014/5/8 20:09:38
 ************************************************************/

IF EXISTS(
       SELECT NAME
       FROM   sys.objects AS o
       WHERE  NAME = 'test'
   )
    DROP TABLE test
GO
CREATE TABLE test
(
	emp_id      INT,
	y_year      CHAR(4),
	m_month     CHAR(02),
	d_day       CHAR(02)
)
GO
INSERT INTO test
SELECT 42,       '2014',       '4',       '1' UNION ALL
SELECT 42,       '2014',       '4',       '3' UNION ALL
SELECT 42,       '2014',       '4',       '4' UNION ALL
SELECT 55,       '2014',       '4',       '2' UNION ALL
SELECT 55,       '2014',       '4',       '3' UNION ALL
SELECT 66,       '2014',       '4',       '1' UNION ALL
SELECT 66,       '2014',       '4',       '2' UNION ALL
SELECT 66,       '2014',       '4',       '3' UNION ALL
SELECT 66,       '2014',       '4',       '4'
GO
SELECT t.emp_id,
       DATEPART(YEAR, b.day1),
       DATEPART(MONTH, b.day1),
       DATEPART(DAY, b.day1)
FROM   (
           SELECT DATEADD(DAY, s.[number], '2014-04-01') AS day1
           FROM   [MASTER]..spt_values AS s
           WHERE  s.[type] = 'P'
                  AND s.[number] <= DATEDIFF(DAY, '2014-04-01', '2014-04-04')
       )  AS b,
       (
           SELECT DISTINCT emp_id
           FROM   test
       )  AS t 
/* 结果
emp_id                              
----------- ----------- ----------- -----------
42          2014        4           1
42          2014        4           2
42          2014        4           3
42          2014        4           4
55          2014        4           1
55          2014        4           2
55          2014        4           3
55          2014        4           4
66          2014        4           1
66          2014        4           2
66          2014        4           3
66          2014        4           4

(12 行受影响)

*/
  • 打赏
  • 举报
回复
--> 测试数据: [ta]
if object_id('[ta]') is not null drop table [ta]
 go
 create table [ta] ([emp_id] int,[y_year] int,[m_month] int,[d_day] int)
insert into [ta]
select 42,2014,4,1 union all
select 42,2014,4,3 union all
select 42,2014,4,4 union all
select 55,2014,4,2 union all
select 55,2014,4,3 union all
select 66,2014,4,1 union all
select 66,2014,4,2 union all
select 66,2014,4,3 union all
select 66,2014,4,4

declare @startdate datetime,@enddate datetime
set @startdate='2014-4-1'
;with
wang as (select dd=DATEADD(day,number,@startdate) from master..spt_values where type='p' and number<30)

select emp_id,y_year,month(dd),DAY(dd) from wang cross join ta 

42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
42	2014	4	1
42	2014	4	2
42	2014	4	3
42	2014	4	4
42	2014	4	5
42	2014	4	6
42	2014	4	7
42	2014	4	8
42	2014	4	9
42	2014	4	10
42	2014	4	11
42	2014	4	12
42	2014	4	13
42	2014	4	14
42	2014	4	15
42	2014	4	16
42	2014	4	17
42	2014	4	18
42	2014	4	19
42	2014	4	20
42	2014	4	21
42	2014	4	22
42	2014	4	23
42	2014	4	24
42	2014	4	25
42	2014	4	26
42	2014	4	27
42	2014	4	28
42	2014	4	29
42	2014	4	30
55	2014	4	1
55	2014	4	2
55	2014	4	3
55	2014	4	4
55	2014	4	5
55	2014	4	6
55	2014	4	7
55	2014	4	8
55	2014	4	9
55	2014	4	10
55	2014	4	11
55	2014	4	12
55	2014	4	13
55	2014	4	14
55	2014	4	15
55	2014	4	16
55	2014	4	17
55	2014	4	18
55	2014	4	19
55	2014	4	20
55	2014	4	21
55	2014	4	22
55	2014	4	23
55	2014	4	24
55	2014	4	25
55	2014	4	26
55	2014	4	27
55	2014	4	28
55	2014	4	29
55	2014	4	30
55	2014	4	1
55	2014	4	2
55	2014	4	3
55	2014	4	4
55	2014	4	5
55	2014	4	6
55	2014	4	7
55	2014	4	8
55	2014	4	9
55	2014	4	10
55	2014	4	11
55	2014	4	12
55	2014	4	13
55	2014	4	14
55	2014	4	15
55	2014	4	16
55	2014	4	17
55	2014	4	18
55	2014	4	19
55	2014	4	20
55	2014	4	21
55	2014	4	22
55	2014	4	23
55	2014	4	24
55	2014	4	25
55	2014	4	26
55	2014	4	27
55	2014	4	28
55	2014	4	29
55	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30
66	2014	4	1
66	2014	4	2
66	2014	4	3
66	2014	4	4
66	2014	4	5
66	2014	4	6
66	2014	4	7
66	2014	4	8
66	2014	4	9
66	2014	4	10
66	2014	4	11
66	2014	4	12
66	2014	4	13
66	2014	4	14
66	2014	4	15
66	2014	4	16
66	2014	4	17
66	2014	4	18
66	2014	4	19
66	2014	4	20
66	2014	4	21
66	2014	4	22
66	2014	4	23
66	2014	4	24
66	2014	4	25
66	2014	4	26
66	2014	4	27
66	2014	4	28
66	2014	4	29
66	2014	4	30

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧