| |

VerySource

 Forgot password?
 Register
Search
View: 1848|Reply: 13

Get the data of each day in the SQL table in the SQL table. If there is no record data of this day in the table, it will

[Copy link]

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-22 17:20:01
| Show all posts |Read mode
Table A
id value date
1 aa 2017-01-02
2 bb 2017-01-01
3 cc 2017-01-04

Now want to get the following records from 2017-01-02 to 2017-01-05

value date
aa 2017-01-02
0 2017-01-03
cc 2017-01-04
o 2017-01-05

Can the SQL query be completed? Thank you for your advice.
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-2 07:54:01
| Show all posts
select * from table where convert (varchar (8), date, 112) between '20170102'and' '20170105'
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-2 08:27:01
| Show all posts
select * from table where convert (varchar (10), date, 120) between '2017-01-02' and '' 2017-01-05 '
Or add hours
select * from table where date between '2017-01-02 00: 00: 00'and' '2017-01-05 23:59:59'
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-2-2 10:36:01
| Show all posts
If he wants to have no date, also list,恶魔右指did not see clearly. .
Reply

Use magic Report

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-2-2 13:27:02
| Show all posts
Yes, the dates that do n’t have to be listed
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-2-2 14:18:01
| Show all posts
create table T (id int, value varchar (10), [date] datetime)
insert T select 1, 'aa', '2017-01-02'
union all select 2, 'bb', '2017-01-01'
union all select 3, 'cc', '2017-01-04'

select isnull (B.value, '0') as value, A. * from
(
select [Date] = '2017-01-02'
union all select '2017-01-03'
union all select '2017-01-04'
union all select '2017-01-05'
) as A
left join T as B on A. [Date] = B. [Date]

--result
value Date
---------- ----------
aa 2017-01-02
0 2017-01-03
cc 2017-01-04
0 2017-01-05

(4 row (s) affected)
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-3 18:00:01
| Show all posts
create table ta (id int, value varchar (5), date datetime)
insert ta
select 1, 'aa', '2017-01-02'
union all select 2, 'bb', '2017-01-01'
union all select 3, 'cc', '2017-01-04'

Generate a time period column:
declare @ta table (t_date datetime)
declare @i datetime
set @ i = '2017-01-01 00:00:00'
while @i!> '2017-1-31 00:00:00'
begin
insert @ta select convert (varchar (10), @ i, 120)
select @ i = @ i + 1
end

select time = convert (varchar (10), t_date, 120), value = isnull (value, 0) from @ta a, ta
where convert (varchar (10), t_date, 120) * = convert (varchar (10), date, 120)
and convert (varchar (10), t_date, 120) between '2017-01-02' and '2017-01-05'

Time value
---------- -----
2017-01-02 aa
2017-01-03 0
2017-01-04 cc
2017-01-05 0

(4 rows affected)
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 United States

Post time: 2020-2-3 20:30:01
| Show all posts
@ta generates a month's date table for the table variable, used with the following statement, there is no need to generate a temporary table
Reply

Use magic Report

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-2-4 10:30:02
| Show all posts
Thanks anyway! What I want is any time period.
Reply

Use magic Report

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-2-4 11:30:01
| Show all posts
Oh, thanks! !! !! !!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list