| |

VerySource

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

Difficult for help, can't figure out the idea, let's look at it

[Copy link]

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 14:30:01
| Show all posts |Read mode
Don't simply talk about using dynamic SQL statements. The previous elder brother has already told me and I understand, but I still haven't figured out a way to solve this problem. Please master the following carefully:

There is a stored procedure with 3 input parameters, which are @companname, @startdate, @enddate; representing the company name, start time, end time, the current requirements are:
Each of these three parameters has two states: empty and non-empty. When combined, there are N kinds of results. If you write them one by one, it should be more troublesome. Ask the master to see how to write this stored procedure.

The following is all not empty
——————————————————————————————————
            select company id, company name, sum (amount) as total from
                  (
                   select * from 1 where request time <@enddate and request time> @startdate
                   union all
                   select * from 2 where request time <@enddate and request time> @startdate
                  ) t where approval opinion = 'agree' and company name = @companyname
           group by company id, company name order by company id
——————————————————————————————————


The following is all empty
——————————————————————————————————
            select company id, company name, sum (amount) as total from
                  (
                   select * from 1
                   union all
                   select * from 2) t where approval opinion = 'agree' group by company id, company name order by company id
——————————————————————————————————

May also need to pay attention to determine whether the start time and end time are correct, please help me!
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Belgium

Post time: 2020-1-4 14:42:01
| Show all posts
where request time between @startdate and @enddate
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-4 15:00:02
| Show all posts
Introduce usage:
In this way, you need to add hours, such as: 2007-01-01 00:00:00 —2007-01-01 23:59:59
Variable type is datetime
set @ startdate = '2007-01-01 00:00:00'
set @ enddate = '2007-01-01 23:59:59
where request time between @startdate and @enddate
Variable type is varchar
set @startdate = '20070101'-take days as the unit
set @enddate = '20070101'
where convert (varchar (8), request time, 112) between @startdate and @enddate
Reply

Use magic Report

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-4 16:00:02
| Show all posts
Thanks to the elder brother upstairs for the explanation, but the question now is how to write a dynamic sentence to judge whether these three fields are empty or not?
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-4 17:06:01
| Show all posts
create oric test
(
@companyname varchar (100) = null,
@enddate datetime = null,
@startdate datetime = null)
as
begin

select company id, company name, sum (amount) as total from
(
                   select * from [1]
                   union all
                   select * from [2]
) t
where approval opinion = 'agree'
and company name = (case when @companyname is null then company name else @companyname end)
and request time <(case when @enddate is null then request time + 1 else @enddate end)
and request time> (case when @startdate is null then request time-1 else @startdate end)
group by company id, company name order by company id

end

This way it doesn't matter whether you pass a few variables, empty or not.

If only one parameter is passed:

exec test @ companyname = 'Microsoft Corporation'
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-4 17:15:01
| Show all posts
create oric test ---> create proc test
Reply

Use magic Report

0

Threads

48

Posts

30.00

Credits

Newbie

Rank: 1

Credits
30.00

 China

Post time: 2020-1-5 13:15:01
| Show all posts
Man, not many combinations
declare @sql varchar (8000)
declare @f varchar (400)
set @ f = (case when @enddate is null then '' else '
where request time <'' '+ @ enddate +' '' 'end) +
(case when @startdate is null then '' else 'and request time>' '' + @ startdate + '' '' end) + ''
set @ sql = 'select company id, company name, sum (amount) as total from
                  (
                   select * from 1 '+ @ f +
'union all
                   select * from 2 '+ @ f +
                  ') t where approval opinion = `` agree' '' +
(case when isnull (@corpname, '') = '' then '' else 'and company name =' '' + @ companyname + '' '' end) + '
           group by company id, company name order by company id '
exec (@sql)
Reply

Use magic Report

0

Threads

48

Posts

30.00

Credits

Newbie

Rank: 1

Credits
30.00

 China

Post time: 2020-1-5 13:21:01
| Show all posts
pupupu01is good
Reply

Use magic Report

0

Threads

48

Posts

30.00

Credits

Newbie

Rank: 1

Credits
30.00

 China

Post time: 2020-1-5 13:24:01
| Show all posts
9988 Good
Reply

Use magic Report

3

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 Singapore

Post time: 2020-1-5 17:48:01
| Show all posts
Thank you guys upstairs, go back and study carefully, reply tomorrow
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