SQL multiple Count

Hi Guys ,
 I need help with one query, i need to get multiple count and amount from the same table for a report e.g

 Loans table.


Status, Amount  , date
 approved , 1000 , 2018 03 16
approved , 2000 , 2018 01 16
 rejected , 10000 , 2018 03 16
rejected , 30000  , 2018 01 16
pending, 10000  , 2018 03 16
pending, 10000 , 2018 01 16

now the result I am looking for is to give me the count of how many cases I have for reach status for today, this month and this year with total amount e.g...
Approved                                             Rejected                                               Pending
Today ,     This MOnth , This Year      Today      , This MOnth   , This Year          Today        , This MOnth , This Year
1 , 1000  , 2 , 3000         ,2 , 3000        1 , 10000  , 2 , 40000      , 2 , 40000           1 , 10000   , 2 , 20000      ,2 , 20000    

Open in new window


 


Thanks
 Ali
Syed Ali Shah RashdiT24/Insight Technical ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Let me know if this floats your boat...
CREATE TABLE #tmp (Status VARCHAR(10), Amount int)

INSERT INTO #tmp (Status, Amount)
VALUES
	('approved', 1000), ('approved', 2000),
	('rejected' , 10000), ('rejected' , 30000),
	('pending', 10000), ('pending', 10000)

SELECT 
	SUM(CASE WHEN Status='approved' THEN 1 ELSE 0 END) AS approved_count, 
	SUM(CASE WHEN Status='approved' THEN Amount ELSE 0 END) AS approved_sum, 
	SUM(CASE WHEN Status='rejected' THEN 1 ELSE 0 END) AS rejected_count, 
	SUM(CASE WHEN Status='rejected' THEN Amount ELSE 0 END) AS rejected_sum, 
	SUM(CASE WHEN Status='pending' THEN 1 ELSE 0 END) AS pending_count, 
	SUM(CASE WHEN Status='pending' THEN Amount ELSE 0 END) AS pending_sum
FROM #tmp 

Open in new window

0
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
Hi Jim,
 Yes, but I need the count and total amount for today, for this month and for this year.

Approved                                             Rejected                                               Pending
Today ,     This MOnth , This Year      Today      , This MOnth   , This Year          Today        , This MOnth , This Year
1 , 1000  , 1 , 1000         ,2 , 3000        1 , 10000  , 1 , 10000      , 2 , 40000           1 , 10000   , 1 , 10000      ,2 , 20000    

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Ok, it looks like you changed the question after I had already answered it as it was originally asked, which is not real professional.

Looking at the new expected result set it looks like you can still pull this off by taking my above code, adding the date, then adding the date logic to the SUM(CASE WHEN... ) statements to handle the time component.
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
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
I guess i edited and it was posted after you reply, i guess it's my mistake. anyways how will i get it today, month and year count and total.

i tried but failing to do it can you help please
0
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
this works gives me result but it will change report format !
CREATE TABLE #tmp (Status VARCHAR(10), Amount int , dates date)

INSERT INTO #tmp (Status, Amount , dates)
VALUES
	('approved', 1000, '2018-03-16'), ('approved', 2000 , '2018-01-16'),
	('rejected' , 10000 , '2018-03-16'), ('rejected' , 30000 , '2018-03-16'),
	('pending', 10000 , '2018-03-16'), ('pending', 10000, '2018-03-16')

SELECT 
	SUM(CASE WHEN Status='approved' THEN 1 ELSE 0 END) AS approved_count, 
	SUM(CASE WHEN Status='approved' THEN Amount ELSE 0 END) AS approved_sum,
--	CASE WHEN Status='approved' and DAY ( dates )  = DAY(getdate())   THEN 'today'  when Status='approved' and month ( dates )  = month(getdate())   THEN 'month' when Status='approved' and year ( dates )  = year(getdate())   THEN 'year' ELSE '' END  AS approved_date,  
	SUM(CASE WHEN Status='rejected' THEN 1 ELSE 0 END) AS rejected_count, 
	SUM(CASE WHEN Status='rejected' THEN Amount ELSE 0 END) AS rejected_sum, 
--    CASE WHEN Status='rejected' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS rejected_date, 
	SUM(CASE WHEN Status='pending' THEN 1 ELSE 0 END) AS pending_count, 
	SUM(CASE WHEN Status='pending' THEN Amount ELSE 0 END) AS pending_sum
--	CASE WHEN Status='pending' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS pending_date
FROM #tmp  where dates   = CONVERT(date, getdate())



union all

SELECT 
	SUM(CASE WHEN Status='approved' THEN 1 ELSE 0 END) AS approved_count, 
	SUM(CASE WHEN Status='approved' THEN Amount ELSE 0 END) AS approved_sum,
--	CASE WHEN Status='approved' and DAY ( dates )  = DAY(getdate())   THEN 'today'  when Status='approved' and month ( dates )  = month(getdate())   THEN 'month' when Status='approved' and year ( dates )  = year(getdate())   THEN 'year' ELSE '' END  AS approved_date,  
	SUM(CASE WHEN Status='rejected' THEN 1 ELSE 0 END) AS rejected_count, 
	SUM(CASE WHEN Status='rejected' THEN Amount ELSE 0 END) AS rejected_sum, 
--    CASE WHEN Status='rejected' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS rejected_date, 
	SUM(CASE WHEN Status='pending' THEN 1 ELSE 0 END) AS pending_count, 
	SUM(CASE WHEN Status='pending' THEN Amount ELSE 0 END) AS pending_sum
--	CASE WHEN Status='pending' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS pending_date
FROM #tmp where month ( dates )  = month(getdate())



union all

SELECT 
	SUM(CASE WHEN Status='approved' THEN 1 ELSE 0 END) AS approved_count, 
	SUM(CASE WHEN Status='approved' THEN Amount ELSE 0 END) AS approved_sum,
--	CASE WHEN Status='approved' and DAY ( dates )  = DAY(getdate())   THEN 'today'  when Status='approved' and month ( dates )  = month(getdate())   THEN 'month' when Status='approved' and year ( dates )  = year(getdate())   THEN 'year' ELSE '' END  AS approved_date,  
	SUM(CASE WHEN Status='rejected' THEN 1 ELSE 0 END) AS rejected_count, 
	SUM(CASE WHEN Status='rejected' THEN Amount ELSE 0 END) AS rejected_sum, 
--    CASE WHEN Status='rejected' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS rejected_date, 
	SUM(CASE WHEN Status='pending' THEN 1 ELSE 0 END) AS pending_count, 
	SUM(CASE WHEN Status='pending' THEN Amount ELSE 0 END) AS pending_sum
--	CASE WHEN Status='pending' and DAY ( dates )  = DAY(getdate()) THEN 'today' ELSE '' END AS pending_date
FROM #tmp where year ( dates )  = year(getdate()) 

Open in new window

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
Microsoft SQL Server

From novice to tech pro — start learning today.