Need T-SQL Help for SSRS and .net code.

CREATE TABLE Report
(
    YearMonth        INT,
    ReportDateKey    INT,
    Admints          INT,
    AdmisionType     VARCHAR(200)
)
GO

INSERT INTO Report VALUES
(201701,20170131,10,'Acute Care')
 ,(201703,20170330,10,'Acute Care')
  ,(201704,20170430,70,'Acute Care')
,(201706,20170630,10,'Acute Care')
,(201702,20170228,80,'Acute Care')
,(201701,20170228,20,'LTE')
,(201702,20170228,30,'LTE')
,(201701,20170131,50,'RHEAB')
,(201702,20170228,100,'RHEAB')

Open in new window


EXEPECTED OUTPUT:-

AdmisionType	ADK	PreviousMonthADK	YTDADK"
Acute Care	80	10	90
LTE	30	20	50
RHEAB	100	50	150

Open in new window

Mayank TripathiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Is Pivot fine with you??

;WITH CTE AS
(
     SELECT * , ROW_NUMBER() OVER (PARTITION BY AdmisionType ORDER BY ReportDateKey) rnk FROM Report    
)
SELECT AdmisionType , ISNULL(MAX([2]),0) [ADK], ISNULL(MAX([1]),0) [PreviousMonthADK] ,ISNULL(MAX([2]),0) + ISNULL(MAX([1]),0)  YTDADK FROM CTE
PIVOT 
(
     MAX(Admints) FOR RNK IN ([1],[2])
)t
GROUP BY AdmisionType

Open in new window

0
Pawan KumarDatabase ExpertCommented:
Output

for my last comment

/*------------------------

;WITH CTE AS
(
     SELECT * , ROW_NUMBER() OVER (PARTITION BY AdmisionType ORDER BY ReportDateKey) rnk FROM Report    
)
SELECT AdmisionType , ISNULL(MAX([2]),0) [ADK], ISNULL(MAX([1]),0) [PreviousMonthADK] ,ISNULL(MAX([2]),0) + ISNULL(MAX([1]),0)  YTDADK FROM CTE
PIVOT 
(
     MAX(Admints) FOR RNK IN ([1],[2])
)t
GROUP BY AdmisionType
------------------------*/
AdmisionType                    ADK         PreviousMonthADK YTDADK
------------------------------- ----------- ---------------- -----------
Acute Care                      80          10               90
LTE                             30          20               50
RHEAB                           100         50               150


(3 row(s) affected)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Will this solution work for you?
WITH CTE_ADK
AS (
	SELECT AdmisionType, MAX(Admints) ADK
	FROM report
	GROUP BY AdmisionType
	),
CTE_PreviousMonthADK
AS (
	SELECT r.AdmisionType, r.Admints
	FROM report r
	WHERE r.YearMonth = (
		SELECT CAST(CAST(r2.YearMonth AS INTEGER)-1 AS CHAR(6))
		FROM report r2  
			INNER JOIN CTE_ADK ca ON r2.AdmisionType = ca.AdmisionType AND r2.Admints = ca.ADK
		WHERE r2.AdmisionType = r.AdmisionType
		)
	)
SELECT a.AdmisionType, a.ADK, p.Admints as PreviousMonthADK, a.ADK + p.Admints as YTDADK
FROM CTE_ADK a
	INNER JOIN CTE_PreviousMonthADK p ON a.AdmisionType=p.AdmisionType

Open in new window

1
Mayank TripathiAuthor Commented:
Thanks !! It works
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.