Column Grouping in SSRS Report - sql

WITH Members as (
select Members, ReportingMonth from Members ) ,
Calls as (
select Received, Answered, Abandoned,ReportingMonth from Calls ) ,
Rate as (
select Rate from Rate ) ,
Complaints as (
select ComplaintsReceived, ComplaintsProcessed,ReportingMonth from Complaints )
select M.Members,
M.ReportMonth,
R.Rate,
C.Received,
C.Answered,
C.Abandoned,
CO.ComplaintsReceived,
CO.ComplaintsProcessed
from Members as M
left join Calls C ON M.ReportMonth = C.ReportMonth
left join Rate R ON M.ReportMonth = R.ReportMonth
LEFT JOIN Complaints CO ON M.ReportMonth = CO.ReportMonth
Hello friends, i have above query as a dataset to my monthly SSRS report. I have to group the report by ReportingMonth. I tried to group it by column grouping but it's not giving me the result i want. I want to group all the data in the select list by reporting month which is used in column group in my SSRS report. I see in select list it's coming from Members cte so the column group only populates the members data.
Is there any way to group all the fields in the select list by reporting month?

Related

How to make SQL Subqueries?

Make a list of employees whose remuneration is available from the average remuneration in their branch and display the average remuneration in the employee's branch. The list should be ordered by branch identifier.
My code looks like this but I can't add it to display the average salary in the employee ward.
SELECT nazwisko, wynagrodzenie, oddzial
FROM pracownicy p
WHERE wynagrodzenie> (SELECT AVG(wynagrodzenie)
FROM pracownicy
WHERE p.oddzial=oddzial);
However, this code displays the average wage but for all employees:
SELECT nazwisko,
(SELECT AVG(wynagrodzenie) FROM pracownicy
WHERE oddzial = p.oddzial) as srednia
FROM pracownicy p LEFT JOIN oddzialy z ON p. oddzial = z.oddzial;
If you want to both filter with and display the average remuneration, then a join is a better option than a subquery.
But here, I would simply recommend window functions; you can do a window average of wynagrodzenie across all rows having the same oddzial, and then use that information for filtering and display as follows:
select nazwisko, wynagrodzenie, oddzial, avg_wynagrodzenie_oddzial
from (
select p.*, avg(wynagrodzenie) over(partition by oddzial) avg_wynagrodzenie_oddzial
from pracownicy p
) t
where wynagrodzenie > avg_wynagrodzenie
You can create a simple join query for that case, so that could reduce implicit (sub query) or explicit (to be displayed) result size that come from persistent table scanning.
SELECT nazwisko, wynagrodzenie, p.oddzial, avg_wynagrodzenie
FROM pracownicy p
INNER JOIN (
SELECT oddzial, AVG(wynagrodzenie) as avg_wynagrodzenie
FROM pracownicy
GROUP BY oddzial
) t
ON p.oddizial = t.oddizial
AND p.wynagrodzenie > t.avg_wynagrodzenie;

SQL tables joined with max

I'm working with two tables. I have a full list of groups in table A, and a list of each group member that has been reviewed in table B. So table B is a log of all review records for those members for each group.
select a.Group_Name, Max(b.Request_Review_Date)
From GroupTable a
Left Outer Join GroupReviews b ON a.Group_Name = b.Group_Name
Group By a.Group_Name
What I am trying to return is the full list of groups from table A, and find the latest review date from table B for each of those groups.
I have researched and tried all or most of the inner & outer joins, apply methods....but its just not giving me the results. Can anyone point me in the right direction? Or am I having to bring back two result sets and compare in my ASP code-behind?
Try a CTE then join back to it
WITH Recent AS
(
select group_name, max(Request_Review_Date) AS 'MaxReviewDate'
from GroupReviews
group by group_name
)
select a.group_name, MaxReviewDate
from GroupTable a left join Recent
on group_name = a.group_name
if you need the value for max for all the a.group name rows the ypu should join the subquery for max date
select a.Group_Name, t.max_date
left join (
select b.Group_Name, Max(b.Request_Review_Date) max_date
from GroupReviews b
Group By b.Group_Name
) t on t.Group_Name = a.Group_Name

SQL function to replicate SUMIF in my Excel

I am new to SQL Server 2012 and want to minimize data handling in Excel.
I have my main data set and then I want to do a SUMIF from those data, and return only the rows that sumif not equal to zero.
I use following function and it returns different results at different times.
Possible reason for error is that there are some rows that AccID is blank.
, main_data2 as
(
select
md.*
from main_data md
left join
(
select
AccID
,sum(amount) as total_amount
from main_data
group by AccID
having sum(amount) <> 0
) md2 on md.AccID = md2.AccID
where md2.AccID is null
)
If you want to retain all accounts in the final result set, then a LEFT JOIN would seem to be appropriate. The subquery you already have finds those accounts with a non zero sum. You can left join main_data to this subquery and then optionally label each record as being empty should its corresponding account sum be zero.
SELECT
md.*,
CASE WHEN md2.AccID IS NULL THEN 'Empty' END AS label
FROM main_data md
LEFT JOIN
(
SELECT AccID, SUM(amount) AS total_amount
FROM main_data
GROUP BY AccID
HAVING SUM(amount) <> 0
) md2
ON md.AccID = md2.AccID

Pivot table in SQL Server results in error

Currently I'm trying to learn on pivot table, here is my table diagram.
I want to generate data row in branch name and column with month with sum total in sales.
SELECT *
FROM
(SELECT
BRANCH.NAME, SALES.TOTAL, TIME.MONTH
FROM
SALES
INNER JOIN
BRANCH ON SALES.BRANCH_ID = BRANCH.BRANCH_ID
INNER JOIN
TIME ON SALES.TIME_ID = TIME.TIME_ID
) AS TABLE1
PIVOT (
SUM(SALES.TOTAL) FOR TIME.MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE
it shows an error:
The column prefix 'SALES' does not match with a table name or alias name used in the query.
Is it my table structure got problem or just my query are wrong?
Remove Sales and Time prefix or use TABLE1 instead:
PIVOT (
SUM(TOTAL) FOR MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE
Try this:
SELECT * FROM
(
SELECT BRANCH.NAME,SALES.TOTAL,TIME.MONTH
FROM SALES
INNER JOIN BRANCH
ON SALES.BRANCH_ID=BRANCH.BRANCH_ID
INNER JOIN TIME
ON SALES.TIME_ID=TIME.TIME_ID
)AS TABLE1
PIVOT (
SUM(TABLE1.TOTAL) FOR TABLE1.MONTH IN ([APR],[MAY],[JUN])
) PIVOTTABLE

sql count statement leads to wrong value

select count(d.Games_played),count(d.No_ofgames) from
(
SELECT COUNT(UserGamePlayed.intID) AS 'Games_played',games.vchCompetency,b.No_Games as 'No_ofgames'
FROM UserGamePlayed
inner join games on games.intGameID=UserGamePlayed.intGameID
inner join
(
select COUNT(Games.intGameID) AS 'No_Games',vchCompetency,intGradeID from Games
WHERE intGradeID=3
GROUP BY vchCompetency,intGradeID
) as b on b.vchCompetency=games.vchCompetency
WHERE intUserID=403 and UserGamePlayed.intGradeID=3
GROUP BY games.vchCompetency,b.No_Games
)as d
the table which i get from d is:
As per the table d i want to get a count of played,when exicute a full i am getting
You should replace COUNT with SUM (in your outer select only).
COUNT only counts (as the name indicates ;)) the rows while SUM will add up the values that are passed to it.