我正试图编写一个与教育系统有关的查询,该查询系统根据11月份在特定学校进行的以下检查类型返回数字值,只有我不太明白该怎么做。< / p>
我需要返回11月份因违规而被引用的组织列表,然后返回导致被引用的当月内的访问次数类型。我需要基于insp.Type_of_Visit__c条件的每种类型的计数,该条件基于每个提供程序,但如果有多个不合规情况,则不需要基于不同的提供程序。
我需要每种访问类型都必须包含一个单独的列。
到目前为止,我的嵌入式子查询返回未过滤的结果:
SELECT ...
, (SELECT COUNT(*) FROM Inspection__c WHERE Type_of_Visit__c = 'Pre-Licensing') AS [Number of PreLicensing Statement of Non-Compliance issue date]
那不能过滤我已有的WHERE子句。
此外,除非有更好的方法,否则我需要针对每种访问类型重复该行。
老实说,我只是想知道如何正确地做到这一点。
SELECT a.Provider_Number__c AS [Provider ID]
, a.Provider_No__c AS [Legacy System Number]
, a.License_Status__c AS [License Status]
, a.Record_Type_Name__c AS [Record Type Name]
, a.Provider_Status__c AS [Provider Status]
, a.License_Type__c AS [License Type]
, a.Name AS [Name]
, a.Provider_Address_1__c AS [Street Address 1]
, a.Provider_Address_2__c AS [Street Address 2]
, a.Provider_city_text__c AS City
, a.Provider_Zip_Code__c AS [Zip Code]
, a.Phone
, a.Provider_Email__c AS Email
, a.Capacity__c AS [Licensed Capacity]
, insp.Date_of_Visit__c AS [Visit Date]
, inv.Statement_of_non_compliance_Issue_Date__c AS [Statement of Non-Compliance Issued Date]
, (SELECT COUNT(*) FROM Inspection__c WHERE Type_of_Visit__c = 'Pre-Licensing') AS [Number of PreLicensing Statement of Non-Compliance issue date]
FROM Inspection__c insp
JOIN Account a ON a.Id = insp.Provider_No__c
JOIN Investigation__c inv ON inv.Provider_Id__c = a.Id
WHERE (insp.Date_of_Visit__c BETWEEN '2018-11-01' AND '2018-11-30')
AND (a.Record_Type_Name__c = 'Large Group' OR a.Record_Type_Name__c = 'Small Group')
AND insp.Type_of_Visit__c IN ('Pre-Licensing',
'Provisional License 90-day Monitoring',
'Provisional License Renewal',
'Renewal-Monitoring',
'Renewal-Full Review',
'Monitoring',
'Enhanced Monitoring',
'Capacity Increase',
'Change of Location',
'Change of Space',
'Follow-up',
'Monitoring outside differential Licensing Cycle',
'License Upgrade',
'Re-Activate Provider',
'Technical Assistance',
'Progress',
'Renewal-Enhanced Monitoring')
我需要计算每个提供者的每种访问类型,并将值返回到表中。如果有更好的方法,则不必是COUNT函数。
非常感谢您的帮助。谢谢。
答案 0 :(得分:0)
如果您正在使用MySql 8,则可以使用
COUNT(*) OVER (PARTITION BY Type_of_Visit)
可能会解决您的问题。
答案 1 :(得分:0)
我不会输入所有字符串,但我将向您展示如何进行输入。
可以说您的查询如下:
SELECT A,B,C
FROM T
WHERE Z in ('x','y','z')
其中T的索引为index1(在您的情况下,index1可能是Provider_ID)
那你就可以做到
SELECT A,B,C, G1.x_count, G1.y_count, G1.z_count
FROM T
JOIN (
SELECT index1,
SUM(CASE WHEN Z = 'x' THEN 1 ELSE 0 END) AS x_COUNT,
SUM(CASE WHEN Z = 'y' THEN 1 ELSE 0 END) AS y_COUNT,
SUM(CASE WHEN Z = 'z' THEN 1 ELSE 0 END) AS z_COUNT
FROM T
WHERE Z in ('x','y','z')
GROUP BY index1
) G1 on T.index1 = G1.index1
所以现在您有了每个索引1的x,y,z计数