Having trouble finding the error in my code - mysql

I am not sure what is wrong with my sql code here. it tells me there is an error at the end but say that its at ''. Thanks for the help!
SELECT PatientID, HCPCCode, HCPCCodeCount, DischargeStatus, State, Gender, Race, Age, County, ProcedureDate, ClaimThroughDate, ICDAccessCode, ClaimID,
CASE WHEN DiagnosisCodeType = 4 THEN 4 END code4,
CASE WHEN DiagnosisCodeType = 4 THEN `Procedures` END procedures4,
CASE WHEN DiagnosisCodeType = 4 THEN `ICD9Codes` END ICD94,
CASE WHEN DiagnosisCodeType = 6 THEN 6 END code6,
CASE WHEN DiagnosisCodeType = 6 THEN `Procedures` END procedures6,
CASE WHEN DiagnosisCodeType = 7 THEN 7 END code7,
CASE WHEN DiagnosisCodeType = 7 THEN `Procedures` END procedures7,
CASE WHEN DiagnosisCodeType = 8 THEN 8 END code8,
CASE WHEN DiagnosisCodeType = 8 THEN `Procedures` END procedures8,
CASE WHEN DiagnosisCodeType = 9 THEN 9 END code9,
CASE WHEN DiagnosisCodeType = 9 THEN `Procedures` END procedures9
FROM ((SELECT subquery2.*, GROUP_CONCAT(CONCAT (ICDAccessCode) SEPARATOR ', ') AS ICD9Codes
FROM (SELECT subquery.*, GROUP_CONCAT(CONCAT (ProcedureID) SEPARATOR ', ') AS Procedures
FROM(SELECT revenue.HCPCCode, revenue.HCPCCodeCount, patient.*, icd9.DiagnosisCodeType, procedures.ProcedureID, procedures.ProcedureDate, claims.ClaimThroughDate, icd9.ICDAccessCode
FROM `cmsdata-outpatient-2012`.revenue revenue
RIGHT OUTER JOIN `cmsdata-outpatient-2012`.patient patient
ON revenue.ClaimID = patient.ClaimID
JOIN `cmsdata-outpatient-2012`.diagnosisicd9 icd9
ON revenue.ClaimID = icd9.ClaimID
JOIN `cmsdata-outpatient-2012`.claims claims
ON revenue.ClaimID = claims.ClaimID
JOIN `cmsdata-outpatient-2012`.procedures procedures
ON revenue.ClaimID = procedures.ClaimID
AND revenue.HCPCCode IN (659, 698, 5116, 5118, 5122, 5123, 5127, 12164, 12171, 16976, 17283)
GROUP BY ClaimID, DiagnosisCodeType) AS subquery
) subquery2)) GROUP BY ClaimID AS subquery3;

Related

Converting MySQL Queries to SQL Server

Can anyone please help converting the MySQL query below to SQL Server.
The 'DATE_FORMAT' and the LIMIT:limit are the problem.
SELECT
DATE_FORMAT(e.eventTime, '%m/%d/%Y %h:%i:%s %p') eventTime,
e.displayPath,
SUBSTRING_INDEX(source, '/alm:', -1) name,
e.eventType,
CASE WHEN e.priority = 0 THEN 'Diagnostic' WHEN e.priority = 1 THEN 'Low' WHEN e.priority = 2 THEN 'Medium' WHEN e.priority = 3 THEN 'High' WHEN e.priority = 4 THEN 'Critical' ELSE '' END priority,
COALESCE(COALESCE(COALESCE(d.intvalue, d.floatvalue), d.strvalue), '') eventValue,
COALESCE(ack.strvalue, '') ackUser
FROM
alarm_events e
LEFT JOIN alarm_event_data d ON d.id = e.id AND d.propname = 'eventValue'
LEFT JOIN alarm_event_data ack ON ack.id = e.id AND ack.propname = 'ackUser'
WHERE
eventtime BETWEEN :startDate AND :endDate AND priority BETWEEN :minPriority AND :maxPriority AND
((:active AND e.eventtype = 0) OR (:clear AND e.eventtype = 1) OR (:ack AND e.eventtype = 2))
ORDER BY
e.eventTime DESC
LIMIT :limit
Variables in SQL use an # instead of the : so you would need to adjust for that and populate your variables from the application.
Instead of the LIMIT parameter, you'd begin the query with a SELECT TOP #limit
The SUBSTRING_INDEX function needs to be adjusted to a standard SUBSTRING and PATINDEX function.
You would need to use the FORMAT() instead of DATE_FORMAT() function ().
As mentioned in the comments, using additional COALESCE() functions in SQL Server is unnecessary.
So the final query would look something like this (with variables to be set by your calling program):
SELECT TOP (#limit)
FORMAT(e.eventTime, 'M/d/yy hh:mm:ss tt') eventTime,
e.displayPath,
SUBSTRING(source, patindex(source,'/alm:'),len(source)) name,
e.eventType,
CASE WHEN e.priority = 0 THEN 'Diagnostic' WHEN e.priority = 1 THEN 'Low' WHEN e.priority = 2 THEN 'Medium' WHEN e.priority = 3 THEN 'High' WHEN e.priority = 4 THEN 'Critical' ELSE '' END priority,
COALESCE(d.intvalue, d.floatvalue, d.strvalue, '') eventValue,
COALESCE(ack.strvalue, '') ackUser
FROM
alarm_events e
LEFT JOIN alarm_event_data d ON d.id = e.id AND d.propname = 'eventValue'
LEFT JOIN alarm_event_data ack ON ack.id = e.id AND ack.propname = 'ackUser'
WHERE
eventtime BETWEEN #startDate AND #endDate AND priority BETWEEN #minPriority AND #maxPriority AND
((#active AND e.eventtype = 0) OR (#clear AND e.eventtype = 1) OR (#ack AND e.eventtype = 2))
ORDER BY
e.eventTime DESC

How to group row values based on same column name

I have created the schema and query on SQL Fiddle:
SQL Fiddle(http://sqlfiddle.com/#!9/e43a13/1)
I am trying to get prices of work type according to the urgency and academic level, I am successful in getting that but I can't figure out how to format it in this way:
| Urgency | Academic Level Name 1 | Academic Level 2 | Academic Level 3 |
-------------------------------------------------------------------------------------------
| 1 hour |Price Of Academic Level 1|Price Of Academic Level 2 | Price Of Academic Level 3|
-------------------------------------------------------------------------------------------
I have as the output:
If you are trying to group by urgency, you may consider below query.
SELECT
utt.UrgencyTime as `Urgency`
, sum(case when al.AcademicID = 1 then wtd.Price else 0 end) as `Academic Level Name 1`
, sum(case when al.AcademicID = 2 then wtd.Price else 0 end) as `Academic Level Name 2`
, sum(case when al.AcademicID >= 3 then wtd.Price else 0 end) as `Academic Level Name 3`
FROM `WorkTypeData` wtd
JOIN UrgencyTimeTable utt ON utt.UrgencyID = wtd.UrgencyID
JOIN WorkType wt ON wt.WorkTypeID = wtd.WorkTypeID
JOIN AcademicLevel al ON al.AcademicID = wtd.AcademicLevelID
WHERE wt.WorkTypeID = '1'
GROUP BY utt.UrgencyTime
to get prices of work type according to the urgency and academic level, use the following query,
SELECT
utt.UrgencyTime as Urgency,
al.LevelName as "Academic Level Name",
wtd.Price
FROM `WorkTypeData` wtd
JOIN
UrgencyTimeTable utt
ON
utt.UrgencyID = wtd.UrgencyID
JOIN
WorkType wt
ON
wt.WorkTypeID = wtd.WorkTypeID
JOIN
AcademicLevel al
ON
al.AcademicID = wtd.AcademicLevelID
WHERE wt.WorkTypeID = '1'
group by utt.UrgencyTime, al.LevelName
I Think this might be helpful
SELECT
utt.UrgencyTime AS `Urgency` ,
CASE
WHEN al.AcademicID = 1 THEN wtd.Price
ELSE 0
END AS `Academic LEVEL Name 1` ,
CASE
WHEN al.AcademicID = 2 THEN wtd.Price
ELSE 0
END AS `Academic LEVEL Name 2` ,
CASE
WHEN al.AcademicID = 7 THEN wtd.Price
ELSE 0
END AS `Academic LEVEL Name 7` ,
CASE
WHEN al.AcademicID = 8 THEN wtd.Price
ELSE 0
END AS `Academic LEVEL Name 8` ,
CASE
WHEN al.AcademicID = 9 THEN wtd.Price
ELSE 0
END AS `Academic LEVEL Name 9`
FROM
`WorkTypeData` wtd
JOIN UrgencyTimeTable utt ON utt.UrgencyID = wtd.UrgencyID
JOIN WorkType wt ON wt.WorkTypeID = wtd.WorkTypeID
JOIN AcademicLevel al ON al.AcademicID = wtd.AcademicLevelID
group BY utt.UrgencyTime, al.AcademicID

How to multiple rows to column & show one row

I am facing a problem with the MySQL query result.
I want result rows to a column & get only one row
please suggest & help to solve my problem
I Done that in numeric values using sum() but in this case, data is string not numeric
SELECT
CASE WHEN IB.`IndicatorId` = 459 THEN AA.`Answer` ELSE '' END AS 'FO',
CASE WHEN IB.`IndicatorId` = 462 THEN AA.`Answer` ELSE '' END AS 'Main_Canal',
CASE WHEN IB.`IndicatorId` = 461 THEN AA.`Answer` ELSE '' END AS 'Parent_Canal',
CASE WHEN IB.`IndicatorId` = 473 THEN AA.`Answer` ELSE '' END AS 'Channel_length',
CASE WHEN IB.`IndicatorId` = 472 THEN AA.`Answer` ELSE '' END AS 'Design_Discharge_Cusecs',
CASE WHEN IB.`IndicatorId` = 474 THEN AA.`Answer` ELSE '' END AS 'GCA',
CASE WHEN IB.`IndicatorId` = 475 THEN AA.`Answer` ELSE '' END AS 'CCA',
CASE WHEN IB.`IndicatorId` = 471 THEN AA.`Answer` ELSE '' END AS 'Water_Courses',
A.`DataCollectorId`
FROM `answer_water_discharge` AS A
INNER JOIN `answers_answer_water_discharge` AS AA ON AA.`AnswerId` = A.`AnswerId`
INNER JOIN `activity_sections_indicators` AS ASI ON ASI.`SectionIndicatorId` = AA.`SectionIndicatorId`
INNER JOIN `indicators_bank` AS IB ON IB.`IndicatorId` = ASI.`IndicatorId`
WHERE 1=1 AND A.AnswerId = 57
I want one row with rows to column

How to Group Counted Data

I have 3 categories (Below SLA, Near SLA, Over SLA) that has different conditions, I try to count the data but the result is not summarized by their category
This is my query:
SELECT
B.province AS 'PROVINCE',
CASE
WHEN TIMEDIFF(A.deli_time, A.create_time) < '20:00:00' THEN COUNT(TIMEDIFF(A.deli_time, A.create_time))
END AS 'Below SLA',
CASE
WHEN (TIMEDIFF(A.deli_time, A.create_time) > '20:00:00') AND (TIMEDIFF(A.deli_time, A.create_time) < '24:00:00') THEN COUNT(TIMEDIFF(A.deli_time, A.create_time))
END AS 'NEAR SLA',
CASE
WHEN TIMEDIFF(A.deli_time, A.create_time) > '24:00:00' THEN COUNT(TIMEDIFF(A.deli_time, A.create_time))
END AS 'OVER SLA'
FROM
deli_order A
INNER JOIN
deli_order_delivery B on A.id = B.order_id
WHERE
(DATE(A.plat_create_time) BETWEEN '2019-03-30' AND'2019-04-07') AND (TIMEDIFF(A.deli_time, A.create_time) IS NOT NULL)
GROUP BY B.province;
and this is the result that i got:
Province | Below SLA | Near SLA | Over SLA
------------------------------------------------
Bali 30 Null Null
30 is the total of all the records of 'Bali', but its actually divided into 19 Below SLAs, 5 Near SLAs, and 6 Over SLAs.
What should i change in my query?
SELECT
B.province AS 'PROVINCE',
SUM(CASE
WHEN TIMEDIFF(A.deli_time, A.create_time) < '20:00:00' THEN 1
END) AS 'Below SLA',
Put an aggregate function for each case,OUTSIDE of it.I did it for just one case,it`s all the same.

Return 1 or 0 in SQL depending on the multiple statements

If I find that some of the user exists with such a parameters, I want to get 1 otherwise 0. In the future I'll have to add more blocks. But it doesn't seem to work now. What am I doing wrong?
SELECT CAST(CASE WHEN EXISTS(SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0)
OR (WHERE Country = 'Germany' AND PostalCode%2 = 0))
)
THEN 1
ELSE 0
END AS BIT)
You need two separate exists:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0
)
THEN 1
WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'Germany' AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
Actually, I broke this into two separate THEN clauses. This is almost equivalent to using OR, but because the logic is inside a CASE, THEN seems more natural. (The difference is that the optimizer could choose to re-arrange the OR conditions, but the THEN conditions are executed in lexical order.)
If your statements are actually this simple, you can combine them as:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country IN ('France', 'Germany') AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
It looks to me like you're just having issues with your bracketing:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE (Country = 'France' AND PostalCode%2 = 0)
OR (Country = 'Germany' AND PostalCode%2 = 0)
) THEN 1 ELSE 0 END AS BIT)
Building on Gordon's assumption that PostalCode%2 = 0 for all tested 'sets' of conditionals (you haven't said as much yet), you could likewise shorten this to:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE PostalCode%2 = 0
AND Country IN ('France', 'Germany')
) THEN 1 ELSE 0 END AS BIT)