Visual Studio Creating Bar of Pie - reporting-services

Is there a way to create a "Bar of Pie" pie chart in Visual Studios like excel? I'm trying to re-create this pie chart in Visual Studios but I'm not sure how to get the "Other, 18" category to expand to a bar chart.
/*Total by Behavior (Pivot Tab)*/
SELECT
CASE WHEN t.EventGroup = 'Less Than 4 Events' THEN SUM(t.Total) ELSE t.Total END AS TotalCount
, EventGroup
, EventLocation
FROM
(
SELECT
COUNT(IncidentNumber) AS Total
, CASE WHEN COUNT(IncidentNumber) <=4 THEN 'Less Than 5 Events' ELSE
CASE WHEN [Event Location] = 'HLMED' AND COUNT(IncidentNumber) >= 4 THEN 'MED'
WHEN [Event Location] = 'HLRAD' AND COUNT(IncidentNumber) >= 4 THEN 'Imaging'
WHEN [Event Location] IN ('HLOR','HLPACU') AND COUNT(IncidentNumber) >= 4 THEN 'OR/RR'
WHEN [Event Location] IN ('HLNR','HLOB','HLLD') AND COUNT(IncidentNumber) >= 4 THEN 'OB/LD/NR'
WHEN [Event Location] = 'HLCV' AND COUNT(IncidentNumber) >= 4 THEN 'PCU'
WHEN [Event Location] IN ('HLICU','HLPACUOV') AND COUNT(IncidentNumber) >= 4 THEN 'ICU'
WHEN [Event Location] = 'HLLAB' AND COUNT(IncidentNumber) >= 4 THEN 'LAB'
WHEN [Event Location] = 'HLSUR' AND COUNT(IncidentNumber) >= 4 THEN 'Surg-Ped'
WHEN [Event Location] = 'HLRT' AND COUNT(IncidentNumber) >= 4 THEN 'RT'
WHEN [Event Location] = 'HLFANS' AND COUNT(IncidentNumber) >= 4 THEN 'FANS'
WHEN [Event Location] = 'HLBUSOFF' AND COUNT(IncidentNumber) >= 4 THEN 'BIZ OFFICE'
WHEN [Event Location] = 'HLPHA' AND COUNT(IncidentNumber) >= 4 THEN 'PHARM'
WHEN [Event Location] = 'HLPSYCH' AND COUNT(IncidentNumber) >= 4 THEN 'BH'
ELSE [Event Location] END
END AS EventGroup
,CASE WHEN [Event Location] = 'HLMED' THEN 'MED'
WHEN [Event Location] = 'HLRAD' THEN 'Imaging'
WHEN [Event Location] IN ('HLOR','HLPACU') THEN 'OR/RR'
WHEN [Event Location] IN ('HLNR','HLOB','HLLD') THEN 'OB/LD/NR'
WHEN [Event Location] = 'HLCV' THEN 'PCU'
WHEN [Event Location] IN ('HLICU','HLPACUOV') THEN 'ICU'
WHEN [Event Location] = 'HLLAB' THEN 'LAB'
WHEN [Event Location] = 'HLSUR' THEN 'Surg-Ped'
WHEN [Event Location] = 'HLRT' THEN 'RT'
WHEN [Event Location] = 'HLFANS' THEN 'FANS'
WHEN [Event Location] = 'HLBUSOFF' THEN 'BIZ OFFICE'
WHEN [Event Location] = 'HLPHA' THEN 'PHARM'
WHEN [Event Location] = 'HLPSYCH' THEN 'BH'
ELSE [Event Location] END AS EventLocation
FROM vw
WHERE Facility_MisFacID = 'HMC'
AND [Event Location] NOT IN ('HLECFE','HLECFN','HLECFS','HLED','HLSECURITY','HLEOC','HLIT','HLMSSNF','HLHPOC','HLED')
--AND EventDateTime >= '2017-01-01'
AND EventDateTime BETWEEN #StartDate AND #EndDate
AND [Order] = 1
AND IncidentType_RmIncidentTypeID = 'BEH'
GROUP BY [Event Location]
) t
GROUP BY t.Total, t.EventGroup, t.EventLocation
OPTION (OPTIMIZE FOR UNKNOWN)

Related

EMBEDDED DATEDIFF EXCLUD WEEKENDS + CASE STATEMENT

I have the below query that utilizes a case statement. I would like to datediff two dates but exclude weekend days.
I have the below that excutes but now I would like to exclude Sat and Sunday from this... AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*EXCLUDE Sat and Sunday from the calculation*/
Full Query
SELECT
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*Business Days*/
--DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) + 1
---(DATEDIFF(WK,A.ALERTS_CREATE_DT,S.CreatedDate) * 2)
---(CASE WHEN DATENAME(DW,A.ALERTS_CREATE_DT) = 'SUNDAY' THEN 1 ELSE 0 END)
---(CASE WHEN DATENAME(DW,S.CreatedDate) = 'SATURADAY' THEN 1 ELSE 0 END)
)
THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER'
THEN ''
ELSE 'N'
END 'Count towards Alerts'
FROM A
FULL S ON A.id= S.id
WHERE 1=1
This should give you the required result by excluding the Saturdays and Sundays.
SELECT A.ALERT_DESC,A.ALERTS_CREATE_DT,S.Name,S.CreatedDate,
CASE WHEN
S.Name IN ('Assessment','Survey') AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
(( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate)+1
- (datediff(wk,A.ALERTS_CREATE_DT,S.CreatedDate)*2)
- case when datepart(dw,A.ALERTS_CREATE_DT)=1 then 1 else 0 end
- case when datepart(dw,S.CreatedDate)=7 then 1 else 0 end
)) <=2 THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER' THEN ''
ELSE 'N' END 'Count towards Alerts'
FROM A,S

write MySQL searching for more than one value in a column

SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status = 6
ORDER BY EffectiveDate ASC
;
i need to return all the values with a status of 6 OR 2
i tried writing AND Status = 6 or 2 but that doesnt work =/
is this accomplished by joining tables or is there a simpler way?
thanks
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND ( Status = 6 OR Status = 2 )
ORDER BY EffectiveDate ASC
You were close, you just need a OR statement with Status = 2.
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND (Status = 2 OR Status = 6)
ORDER BY EffectiveDate ASC
;
Here is some more info.
You want either IN or OR -
AND Status IN (6, 2)
AND Status = 6 OR Status = 2
You can use IN in your query. Tyr this :
SELECT
ID,
Division,
EffectiveDate,
PM,
case Status
when 0 then 'Dead'
when 1 then 'Active'
when 2 then 'Job'
when 3 then 'Pending'
when 4 then 'Sales Lead'
when 5 then 'Budget'
when 6 then 'Change Order'
end as Status,
Name,
Address,
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status IN (6,2)
ORDER BY EffectiveDate ASC

MySQL - Select new entries only for every day

I'm trying to find the new tasks assigned to employees per day. The fiddle is here
An employee may cycle between say 4 tasks A,B,C & D. On 1st Jan, he may get assigned task 'A', on 2nd Jan he may get assigned task 'B', 5th Jan task 'C', 6th Jan task 'D' & say on Jan 14th he gets assigned task 'A' again. I'd like to see the date & the newly assigned task compared to the previous day with respect to any particular day.
The output of the SQL I need should show me only the new task he is assigned on a given day. The definition of new is with respect to the previous day or the last present entry if data is not present for a given date.
Here's how it should ideally look -
UPDATED SQL
FIDDLE HERE
SELECT task_date,
employee_name,
Group_concat(task_name)
FROM (
SELECT DISTINCT a.task_date,
a.employee_name,
CASE
WHEN b.employee_name IS NOT NULL
AND c.employee_name IS NULL THEN NULL
ELSE a.task_name
END AS task_name
FROM forgerock AS a
LEFT OUTER JOIN forgerock AS b
ON a.employee_name = b.employee_name = 'A'
AND a.task_date >= '2015-01-02'
AND a.task_date <= '2015-01-04'
AND b.task_date >= '2015-01-02'
AND b.task_date <= '2015-01-04'
AND a.task_date - 1 = b.task_date
AND a.region = b.region = 'USA'
LEFT OUTER JOIN forgerock AS c
ON a.employee_name = c.employee_name = 'A'
AND a.task_date >= '2015-01-02'
AND a.task_date <= '2015-01-04'
AND c.task_date >= '2015-01-02'
AND c.task_date <= '2015-01-04'
AND a.task_date - 1 = c.task_date
AND a.task_name <> c.task_name
AND a.region = b.region = 'USA'
ORDER BY a.task_date,
a.employee_name,
a.task_name) AS temp
GROUP BY task_date,
employee_name
select task_date,employee_name,GROUP_CONCAT(task_name) from(
select distinct a.task_date,a.employee_name
,case when b.employee_name is not null and c.employee_name is null
then null
else a.task_name end as task_name
from ForgeRock as a left outer join ForgeRock as b
on a.employee_name = b.employee_name and a.task_date-1 = b.task_date
left outer join ForgeRock as c
on a.employee_name = c.employee_name and a.task_date-1 = c.task_date
and a.task_name <> c.task_name
order by a.task_date,a.employee_name,a.task_name) as temp
group by task_date,employee_name
Adding Condition :
select task_date,employee_name,GROUP_CONCAT(task_name) from(
select distinct a.task_date,a.employee_name
,case when b.employee_name is not null and c.employee_name is null
then null
else a.task_name end as task_name
from ForgeRock as a left outer join ForgeRock as b
on a.employee_name = b.employee_name and a.task_date-1 = b.task_date and a.task_date between '2015-01-02' AND '2015-01-04' and b.task_date between '2015-01-02' AND '2015-01-04'
left outer join ForgeRock as c
on a.employee_name = c.employee_name and a.task_date-1 = c.task_date and a.task_date between '2015-01-02' AND '2015-01-04' and c.task_date between '2015-01-02' AND '2015-01-04'
and a.task_name <> c.task_name
where a.region = 'USA' and a.task_date between '2015-01-02' AND '2015-01-04'
order by a.task_date,a.employee_name,a.task_name) as temp
group by task_date,employee_name

Mysql case not working

SELECT
SQL_CALC_FOUND_ROWS a.* ,
zn.`name` AS zone_name,
c.`name` AS carrier_name,
CASE type
WHEN type=1 THEN 'General day'
ELSE 'Special date' END AS type_changed,
CASE week_day
WHEN week_day = -1 THEN 'notset'
WHEN week_day = 1 THEN 'monday'
WHEN week_day = 2 THEN 'tuesday'
WHEN week_day = 3 THEN 'wednesday'
WHEN week_day = 4 THEN 'thursday'
WHEN week_day = 5 THEN 'friday'
WHEN week_day = 6 THEN 'saturday'
WHEN week_day = 7 THEN 'sunday' END AS week_day_mod ,
IF(date = '0001-01-01 00:00:0', '--', DATE(date)) AS date_mod, IF(is_working_day = 1 ,'working day', 'day off') AS is_working_day_mod
FROM `ps_deliverytime_table` a
LEFT JOIN ps_zone AS zn ON(a.`id_zone` = zn.`id_zone`)
LEFT JOIN ps_carrier AS c ON(a.`id_carrier` = c.`id_carrier`)
WHERE 1 ORDER BY a.`id_time_table` ASC LIMIT 0,50
When week day is equal 1 it works fine but in other case it do not working
When you put column name after CASE, you shouldn't use WHEN column = value, just use WHEN value, because it automatically compares the column to each value in the WHEN clauses.
CASE type
WHEN 1 THEN 'General day'
ELSE 'Special date'
END AS type_changed,
CASE week_day
WHEN -1 THEN 'notset'
WHEN 1 THEN 'monday'
WHEN 2 THEN 'tuesday'
WHEN 3 THEN 'wednesday'
WHEN 4 THEN 'thursday'
WHEN 5 THEN 'friday'
WHEN 6 THEN 'saturday'
WHEN 7 THEN 'sunday'
END AS week_day_mod ,
When you do both, you're testing week_day = (week_day = -1), week_day = (week_day = 1), etc. It works on Monday because 1 = (1 = 1) is equivalent to 1 = 1, which is true; but on Tuesday, it's 2 = (2 = 2), which is equivalent to 2 = 1, which is false.

Getting collections of consecutive dates

I have a database which stores employees leave. For each day an employee is on leave a new record is entered to the database. What I want to achieve is for someone to enter an employee id and a date range and for each period of absence a record is returned stating date from, date to, duration and also whether it was AM or PM (for half days).
It should look something like (for employee 9999 and dates 2011-08-08 to 2011-09-01):
employee_id | Start | start_am_pm | End | end_am_pm | Duration
9999 | 2011-08-10 | PM | 2011-08-12 | AM | 2
9999 | 2011-09-01 | | 2011-09-01 | | 1
Note: first duration above is 2 because 10th and 12th are both half days and 11th is a full.
Anyway. The query I have works exactly as I would expect, if the From date is not a date that the employee has leave on. Eg, in the above example if I set the from date to 10th, 11th or 12th, it removes that line. It should count the days between the dates specified.
How it currently shows (for employee 9999 and dates 2011-08-11 to 2011-09-01):
employee_id | Start | start_am_pm | End | end_am_pm | Duration
9999 | 2011-09-01 | | 2011-09-01 | | 1
Similar was happening with the To date but I got that fixed. A similar approach didn't work for the From date. Below is my stored procedure.
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `GetLeaveDates`$$
CREATE DEFINER=`root`#`%` PROCEDURE `GetLeaveDates`(pEmpID INT, pDateFrom DATETIME, pDateTo DATETIME)
BEGIN
SELECT
a.start_date,
CASE WHEN a.am_pm = 1 THEN "AM"
WHEN a.am_pm = 2 THEN "PM"
ELSE "" END AS start_am_pm,
CASE WHEN pDateTo > MIN(c.start_date) THEN
MIN(c.start_date)
ELSE
pDateTo
END AS End,
CASE WHEN c.am_pm = 1 THEN "AM"
WHEN c.am_pm = 2 THEN "PM"
ELSE "" END AS start_am_pm,
CASE WHEN a.am_pm = 0 AND c.am_pm = 0 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+1
WHEN (a.am_pm = 0 AND c.am_pm <> 0) OR (c.am_pm = 0 AND a.am_pm <> 0) THEN
DATEDIFF(MIN(c.start_date),a.start_date)+0.5
WHEN a.am_pm <> 0 AND c.am_pm <> 0 THEN
DATEDIFF(MIN(c.start_date),a.start_date)
END
AS Duration
FROM t AS a
LEFT JOIN t AS b ON a.employee_id=b.employee_id AND a.start_date = ADDDATE(b.start_date,1)
LEFT JOIN t AS c ON a.employee_id=c.employee_id AND a.start_date <= c.start_date
LEFT JOIN t AS d ON c.employee_id=d.employee_id AND c.start_date = ADDDATE(d.start_date,-1)
WHERE b.start_date IS NULL AND c.start_date IS NOT NULL AND d.start_date IS NULL
AND a.EMPLOYEE_ID = pEmpID
AND a.START_DATE BETWEEN pDateFrom AND pDateTo
GROUP BY a.employee_id, a.start_date
; END$$
DELIMITER ;
Ok I figured it out and it was quite simple, basically on each of the LEFT JOIN I had to filter by out the start date by the parameters passed in.
I also had to filter by whether the leave was approved, an Approved_DateTime and Approved_By field where filled in if it was approved. Also the calculation of the duration was a bit off in some circumstances. So my stored procedure now looks like:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `GetLeaveDates`$$
CREATE DEFINER=`root`#`%` PROCEDURE `GetLeaveDates`(pEmpID INT, pDateFrom DATETIME, pDateTo DATETIME, pApproved BOOLEAN)
BEGIN
SELECT
DATE_FORMAT(a.start_date,'%d/%m/%y') AS start,
CASE WHEN a.am_pm = 1 THEN "AM"
WHEN a.am_pm = 2 THEN "PM"
ELSE "" END AS start_am_pm,
DATE_FORMAT(CASE WHEN pDateTo > MIN(c.start_date) THEN
MIN(c.start_date)
ELSE
pDateTo
END, '%d/%m/%y') AS end,
CASE WHEN c.am_pm = 1 THEN "AM"
WHEN c.am_pm = 2 THEN "PM"
ELSE "" END AS end_am_pm,
CASE WHEN a.am_pm = 0 THEN
CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+1
WHEN c.am_pm = 1 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+0.5
END
WHEN a.am_pm = 1 THEN
CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+1
WHEN c.am_pm = 1 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+0.5
END
WHEN a.am_pm = 2 THEN
CASE WHEN c.am_pm = 0 OR c.am_pm = 2 THEN
DATEDIFF(MIN(c.start_date),a.start_date)+0.5
WHEN c.am_pm = 1 THEN
DATEDIFF(MIN(c.start_date),a.start_date)
END
END AS Duration
FROM t AS a
LEFT JOIN t AS b ON a.employee_id=b.employee_id AND a.start_date = ADDDATE(b.start_date,1) AND ISNULL(b.approved_datetime) <> pApproved AND b.start_date BETWEEN pDateFrom AND pDateTo
LEFT JOIN t AS c ON a.employee_id=c.employee_id AND a.start_date <= c.start_date AND ISNULL(c.approved_datetime) <> pApproved AND c.start_date BETWEEN pDateFrom AND pDateTo
LEFT JOIN t AS d ON c.employee_id=d.employee_id AND c.start_date = ADDDATE(d.start_date,-1) AND ISNULL(d.approved_datetime) <> pApproved AND d.start_date BETWEEN pDateFrom AND pDateTo
WHERE b.start_date IS NULL AND c.start_date IS NOT NULL AND d.start_date IS NULL
AND a.EMPLOYEE_ID = pEmpID
AND a.START_DATE BETWEEN pDateFrom AND pDateTo
AND ISNULL(a.approved_datetime) <> pApproved
AND a.start_date BETWEEN pDateFrom AND pDateTo
GROUP BY a.employee_id, a.start_date
; END$$
DELIMITER ;