我的存储过程是:
ALTER PROCEDURE [dbo].[chart_OutPatientDetailsOPD_monthwise]
@year varchar(5)
AS
BEGIN
SET NOCOUNT ON;
SELECT
p.MName, SUM(p.New) New, SUM(p.Renew) Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, datetime) MName,
0 New, 0 Renew
FROM
PatientRenew
WHERE
YEAR(datetime) BETWEEN @year and @year+1
GROUP BY
DATENAME(YEAR, datetime),
MONTH(DateTime), DATENAME(MONTH, datetime)
UNION ALL
SELECT
t.monthcn, t.MName, 0 New, SUM(t.NoofPatient) Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, prn.datetime) MName,
COUNT(prn.MRNO) NoofPatient
FROM
PatientRenew prn
WHERE
YEAR(PRN.datetime) BETWEEN @year AND @year + 1
GROUP BY
DATENAME(YEAR, prn.datetime),
DATENAME(MONTH, prn.datetime),
MONTH(DateTime), prn.MRNO) t
WHERE
t.NoofPatient > 1
GROUP BY
t.MName, t.monthcn
UNION ALL
SELECT
t.monthcn, t.MName,
SUM(t.NoofPatient) New, 0 Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, prn.datetime) MName,
COUNT(prn.MRNO) NoofPatient
FROM
PatientRenew prn
WHERE
YEAR(PRN.datetime) BETWEEN @year AND @year + 1
GROUP BY
DATENAME(YEAR, prn.datetime),
DATENAME(MONTH, prn.datetime),
MONTH(DateTime), prn.MRNO) t
WHERE
t.NoofPatient = 1
GROUP BY
t.MName, t.monthcn) p
GROUP BY
p.MName, p.monthcn
ORDER BY
CHARINDEX(LEFT(p.MName, 3), 'AprMayJunJulAugSepOctNovDecJanFebMar')
END
当我像这样执行时:
EXEC chart_OutPatientDetailsOPD_monthwise '2016'
我得到了这个结果表:
mname new renew Apr-2017 566 467 Apr-2016 392 247 May-2017 671 521 May-2016 396 233 Jun-2016 380 297 Jun-2017 571 488
Jul-2016 440 244 Jul-2017 679 543 Aug-2017 612 435 Aug-2016 451 333
Sep-2017 628 504 Sep-2016 452 283 Oct-2017 467 271 Oct-2016 399 319
Nov-2016 368 308 Dec-2016 491 319 Jan-2017 486 411 Jan-2016 3 0
Feb-2017 645 533 Feb-2016 13 6 Mar-2016 117 16 Mar-2017 599 506
但我想要2016年4月到2017年3月的数据
答案 0 :(得分:0)
在所有选择的where子句中添加过滤,应选择这些日期。你可以修改现有程序吗?
Where convert ( varchar(10),prn.datetime,120) between '2016-04-01' and '2017-03-30'
答案 1 :(得分:0)
您可以将SP更新为:
ALTER PROCEDURE [dbo].[chart_OutPatientDetailsOPD_monthwise]
@StartDate smalldatetime,
@EndDate smalldatetime
AS
BEGIN
SET NOCOUNT ON;
SELECT
p.MName, SUM(p.New) New, SUM(p.Renew) Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, datetime) MName,
0 New, 0 Renew
FROM
PatientRenew
WHERE
PRN.datetime BETWEEN @StartDate AND @EndDate
GROUP BY
DATENAME(YEAR, datetime),
MONTH(DateTime), DATENAME(MONTH, datetime)
UNION ALL
SELECT
t.monthcn, t.MName, 0 New, SUM(t.NoofPatient) Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, prn.datetime) MName,
COUNT(prn.MRNO) NoofPatient
FROM
PatientRenew prn
WHERE
PRN.datetime BETWEEN @StartDate AND @EndDate
GROUP BY
DATENAME(YEAR, prn.datetime),
DATENAME(MONTH, prn.datetime),
MONTH(DateTime), prn.MRNO) t
WHERE
t.NoofPatient > 1
GROUP BY
t.MName, t.monthcn
UNION ALL
SELECT
t.monthcn, t.MName,
SUM(t.NoofPatient) New, 0 Renew
FROM
(SELECT
MONTH(DateTime) monthcn,
SUBSTRING(DATENAME(MONTH, datetime), 0, 4) + '-' +
DATENAME(YEAR, prn.datetime) MName,
COUNT(prn.MRNO) NoofPatient
FROM
PatientRenew prn
WHERE
PRN.datetime BETWEEN @StartDate AND @EndDate
GROUP BY
DATENAME(YEAR, prn.datetime),
DATENAME(MONTH, prn.datetime),
MONTH(DateTime), prn.MRNO) t
WHERE
t.NoofPatient = 1
GROUP BY
t.MName, t.monthcn) p
GROUP BY
p.MName, p.monthcn
ORDER BY
CHARINDEX(LEFT(p.MName, 3), 'AprMayJunJulAugSepOctNovDecJanFebMar')
END
请注意,我已将参数更改为接受@StartDate
和@EndDate
,并修改WHERE
条款,例如PRN.datetime BETWEEN @StartDate AND @EndDate
所以现在你可以像这样执行你的SP:
EXEC chart_OutPatientDetailsOPD_monthwise '04-01-2016', '03-30-2017'