SQL Server Management Studio Express
Please help me with this:
CREATE procedure [dbo].[test] @Year int as
DECLARE @DateStart datetime
DECLARE @DateEnd datetime
SET @DateStart = cast(@Year-1 as varchar(4))+'0101'
-- Set date start to be 01/01/year-1 00:00:00
SET @DateEnd = cast(@Year+1 as varchar(4))+'0101'
-- Set date end to be 01/01/year+1 00:00:00
IF @Year= YEAR(GETDATE())
-- Current year, must get last full month
BEGIN
SET @DateEnd = CAST(CONVERT(varchar(8),DateAdd(dd,-DAY(GetDate()), GetDate()), 112) as datetime) + 1
END
SET NOCOUNT ON;
SELECT
@Year cy,
@Year-1 py,
---jan @year
case when year(FM.ReportingDate)=@year and month(FM.ReportingDate)=1
then count(FM.JobNumber) else 0 end cyJanJobs,
case when year(FM.ReportingDate)=@year and month(FM.ReportingDate)=1
then round(sum(FM.Sales),0) else 0 end cyJanSales,
…..
---feb @year
…
---jan @year-1
case when year(FM.ReportingDate)=@year-1 and month(FM.ReportingDate)=1
then count(FM.JobNumber) else 0 end pyJanJobs,
case when year(FM.ReportingDate)=@year-1 and month(FM.ReportingDate)=1
then round(sum(FM.Sales),0) else 0 end pyJanSales,
…
FROM
FM
WHERE
…
FM.Type in (‘Import’, ‘Exports’)
AND (FM.ReportingDate >= @DateStart AND FM.ReportingDate<@DateEnd)
group by year(FM.ReportingDate),
month(FM.ReportingDate)
and I develop a report based on this.
Now I have to develop a report where when they are imports I have to get the idate from the FM, and when they are exports I have to get edate, instead of ReportingDate. As well, I have to get the year(idate) or year(edate) instead to get the year(ReportingDate); parameter should be the same @year
Please help me with this:
CREATE procedure [dbo].[test] @Year int as
DECLARE @DateStart datetime
DECLARE @DateEnd datetime
SET @DateStart = cast(@Year-1 as varchar(4))+'0101'
-- Set date start to be 01/01/year-1 00:00:00
SET @DateEnd = cast(@Year+1 as varchar(4))+'0101'
-- Set date end to be 01/01/year+1 00:00:00
IF @Year= YEAR(GETDATE())
-- Current year, must get last full month
BEGIN
SET @DateEnd = CAST(CONVERT(varchar(8),DateAdd(dd,-DAY(GetDate()), GetDate()), 112) as datetime) + 1
END
SET NOCOUNT ON;
SELECT
@Year cy,
@Year-1 py,
---jan @year
case when year(FM.ReportingDate)=@year and month(FM.ReportingDate)=1
then count(FM.JobNumber) else 0 end cyJanJobs,
case when year(FM.ReportingDate)=@year and month(FM.ReportingDate)=1
then round(sum(FM.Sales),0) else 0 end cyJanSales,
…..
---feb @year
…
---jan @year-1
case when year(FM.ReportingDate)=@year-1 and month(FM.ReportingDate)=1
then count(FM.JobNumber) else 0 end pyJanJobs,
case when year(FM.ReportingDate)=@year-1 and month(FM.ReportingDate)=1
then round(sum(FM.Sales),0) else 0 end pyJanSales,
…
FROM
FM
WHERE
…
FM.Type in (‘Import’, ‘Exports’)
AND (FM.ReportingDate >= @DateStart AND FM.ReportingDate<@DateEnd)
group by year(FM.ReportingDate),
month(FM.ReportingDate)
and I develop a report based on this.
Now I have to develop a report where when they are imports I have to get the idate from the FM, and when they are exports I have to get edate, instead of ReportingDate. As well, I have to get the year(idate) or year(edate) instead to get the year(ReportingDate); parameter should be the same @year