简体   繁体   中英

How to create a loop in a case statement

i have the following SQL statement:

DECLARE @time datetime;       
SELECT @time = (select min(CreationDate) from TABLE);

DECLARE @time2 int;       
SELECT @time2 = 15;

select ColumnA, 
(case when CreationDate between @time and DATEADD(MINUTE,@time2***1**,@time) 
          then cast (@time2*1 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***2**,@time) 
          then cast (@time2*2 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***3**,@time) 
          then cast (@time2*3 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***4**,@time) 
          then cast (@time2*4 as int)
      else 0
end) as 'interval', count(1)
from TABLE
 group by 
ColumnA, 
(case when CreationDate between @time and DATEADD(MINUTE,@time2***1**,@time) 
          then cast (@time2*1 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***2**,@time) 
          then cast (@time2*2 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***3**,@time) 
          then cast (@time2*3 as int)
      when CreationDate between @time and DATEADD(MINUTE,@time2***4**,@time) 
          then cast (@time2*4 as int)
      else 0
end)
  1. How can i write the case statement in a loop so the bold number will be a parameter 2.i need that the loop/function will be able to write as many case row as needed according to the parameter in Q.1

thanks a lot !

Hello everyone,

Thanks for your response and comments. I realized that maybe I did not explain my questions properly. Let me rephrase my questions again.

I have an ETL process that runs and fills a table consisting of column called "ColumnA" which displayed codes, and creation time column called "CreationDate". I want to divide the results by time of creation. Sometimes by 15 minutes, sometimes by 20 minutes or any other time interval. So I established a variable called "@time" that say what is the interval length. The first problem: I do not know how long the ETL process will run, so I do not know how many lines to produce in the CASE statement. The second problem: the number of CASE statement lines also depends on the interval length in which I choose in the variable "@time". That is, if the process takes an hour and "@time" selected intervals is 15 minutes then I must produce 4 CASE statement lines but if I select "@time" to be 10 minutes then I must produce 6 CASE statement lines… Who can I do it with parameters?

Thanks in advance for your time and efforts. Regards, Alan B.

Don't think of loops when working with SQL. Think of results you want to see.

If I interprete your request correctly, you are selecting all rows with a creation date between the given @time and the following hour. For these you determine the time slice. In your case it's 15 minutes and you want to know, whether a creation date is within the first 15 minutes (then you output 15), or second (then you output 30) and so on. Records with another creation date, no matter whether before or after the hour in question, are given an output 0.

So the only problem is to find the correct formula, which is more or less: get the time difference in minutes, divide by the minute slice, and the resulting full integer will tell you which slice it is, starting with 0 for the first slice in the hour.

That should be more or less:

select columna, interval_end_minute, count(*)
from
(
  select
    columna,
    case when creationdate >= @time and creationdate < dateadd(hour,1,@time) then
      (truncate((time_to_sec(datediff(creationdate, @time)) / 60) / @time2, 0) + 1) * @time2
    else 
      0
    end as interval_end_minute
  from table
) data
group by columna, interval_end_minute;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM