如何在 SQL 服务器中的 case 语句内的 then 子句中传递 2 个参数
How to pass 2 parameters in then clause inside case statement in SQL Server
我正在开发一个 scholl 项目,我创建了一个存储过程,需要在 THEN 子句中传递两个参数,当变量 @tempo
是天时,我想 select 月和日.
CREATE OR ALTER PROCEDURE tentativa1
@tempo as nvarchar(30),
@ap_id int
AS
SELECT
L_AccessPoint.ap_id, ap_name,
COUNT(*) AS NumeroAcessos, year,
CASE
WHEN @tempo = 'month' THEN month
ELSE NULL
END AS 'MES',
CASE
WHEN @tempo = 'day' THEN day
ELSE NULL
END AS 'DIA'
FROM
L_AccessPoint
INNER JOIN
F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE
L_AccessPoint.ap_id = @ap_id
GROUP BY
L_AccessPoint.ap_id, ap_name, year,
CASE WHEN @tempo = 'month' THEN month ELSE NULL END,
CASE WHEN @tempo ='day' THEN day ELSE NULL END
ORDER BY
L_AccessPoint.ap_id, year,
CASE WHEN @tempo ='month' THEN month ELSE NULL END,
CASE WHEN @tempo ='day' THEN day ELSE NULL END
是否可以做出类似的东西:
CASE
WHEN @tempo = 'day' THEN month, day
ELSE NULL
END AS 'DIA'
如果 @tempo
是日,我必须按年、月和日分组,否则如果 @tempo
是年,我只需要按年分组。如果我传递 @tempo
等于天,结果是按年和天分组,将一年内每个月的所有第 1 天分组在同一行中。像这样Wrong result。有什么方法可以正确做到这一点??
因此,经过一些测试后,我可以通过将 case when 替换为 if 和 else if 来正确地执行该过程,结果保持这样:
CREATE or alter PROCEDURE testeSessoes
@tempo as nvarchar(30),
@data_ini as date,
@data_fin as date
AS
BEGIN
IF @tempo = 'year'
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year
order by L_AccessPoint.ap_id ASC, year DESC
END
ELSE
IF @tempo='month'
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year,month
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year,month
order by L_AccessPoint.ap_id ASC, year DESC,month DESC
END
else
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year,month,day
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year,month,day
order by L_AccessPoint.ap_id ASC, year DESC,month DESC,day DESC
END
END
我创建了 3 个查询 returns 并按照我想要的方式进行分组。
感谢所有试图帮助我的人。
我正在开发一个 scholl 项目,我创建了一个存储过程,需要在 THEN 子句中传递两个参数,当变量 @tempo
是天时,我想 select 月和日.
CREATE OR ALTER PROCEDURE tentativa1
@tempo as nvarchar(30),
@ap_id int
AS
SELECT
L_AccessPoint.ap_id, ap_name,
COUNT(*) AS NumeroAcessos, year,
CASE
WHEN @tempo = 'month' THEN month
ELSE NULL
END AS 'MES',
CASE
WHEN @tempo = 'day' THEN day
ELSE NULL
END AS 'DIA'
FROM
L_AccessPoint
INNER JOIN
F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE
L_AccessPoint.ap_id = @ap_id
GROUP BY
L_AccessPoint.ap_id, ap_name, year,
CASE WHEN @tempo = 'month' THEN month ELSE NULL END,
CASE WHEN @tempo ='day' THEN day ELSE NULL END
ORDER BY
L_AccessPoint.ap_id, year,
CASE WHEN @tempo ='month' THEN month ELSE NULL END,
CASE WHEN @tempo ='day' THEN day ELSE NULL END
是否可以做出类似的东西:
CASE
WHEN @tempo = 'day' THEN month, day
ELSE NULL
END AS 'DIA'
如果 @tempo
是日,我必须按年、月和日分组,否则如果 @tempo
是年,我只需要按年分组。如果我传递 @tempo
等于天,结果是按年和天分组,将一年内每个月的所有第 1 天分组在同一行中。像这样Wrong result。有什么方法可以正确做到这一点??
因此,经过一些测试后,我可以通过将 case when 替换为 if 和 else if 来正确地执行该过程,结果保持这样:
CREATE or alter PROCEDURE testeSessoes
@tempo as nvarchar(30),
@data_ini as date,
@data_fin as date
AS
BEGIN
IF @tempo = 'year'
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year
order by L_AccessPoint.ap_id ASC, year DESC
END
ELSE
IF @tempo='month'
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year,month
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year,month
order by L_AccessPoint.ap_id ASC, year DESC,month DESC
END
else
Begin
Select L_AccessPoint.ap_id, ap_name, COUNT(*) AS NumeroAcessos, year,month,day
FROM L_AccessPoint INNER JOIN F_Session ON L_AccessPoint.ap_id = F_Session.ap_id
WHERE F_Session.ts between @data_ini and @data_fin
Group by L_AccessPoint.ap_id, ap_name, year,month,day
order by L_AccessPoint.ap_id ASC, year DESC,month DESC,day DESC
END
END
我创建了 3 个查询 returns 并按照我想要的方式进行分组。
感谢所有试图帮助我的人。