我有一个查询针对单个服务返回多行,因为一个人可能具有多个凭据。在医学领域,您保留几个证书,但为简单起见,我将仅使用标准证书Phd,MA,MS,BA,BS,AS
我需要知道最简单的方法来忽略Z_ServiceLedger.clientvisit_id在层次结构中较低的Credentials.credentials的行。因此,如果某位员工提供服务,并且拥有博士学位和MA,则只返回该行,如果拥有博士学位,则拥有Ma和BA,仅返回该行。我们大约有50个凭证,因此,如果我对每个凭证使用CASE,则可以看到会有多大的混乱,我希望有一种更好的方法来避免这种情况。
这是我当前的查询:
SELECT DISTINCT
SUM(CASE WHEN v.non_billable = 0 THEN v.duration ELSE 0 END) / 60 AS billable_hours,
SUM(CASE WHEN (v.non_billable = 0 AND Z_ServiceLedger.payer_id = 63) THEN v.duration ELSE 0 END) / 60 AS billable_mro_hours,
Credentials.credentials
FROM
Z_ServiceLedger
INNER JOIN
ClientVisit v ON Z_ServiceLedger.clientvisit_id = v.clientvisit_id
LEFT JOIN
Employees ON v.emp_id = Employees.emp_id
LEFT JOIN
EmployeeCredential ON Employees.emp_id = EmployeeCredential.emp_id
LEFT JOIN
Credentials ON Credentials.credential_id = EmployeeCredential.credential_id
WHERE
v.rev_timein <= CASE
WHEN EmployeeCredential.end_date IS NOT NULL
THEN EmployeeCredential.end_date
ELSE GETDATE()
END
AND v.rev_timein >= @param1
AND v.rev_timein < DateAdd(d, 1, @param2)
AND Z_ServiceLedger.amount > 0
AND v.splitprimary_clientvisit_id IS NULL
AND v.gcode_primary_clientvisit_id IS NULL
AND v.non_billable = 0
AND v.non_billable = 'FALSE'
AND v.duration / 60 > 0
AND Z_ServiceLedger.action_type NOT IN ('SERVICE RATE CHANGE', 'CLIENT STATEMENT')
AND (EmployeeCredential.is_primary IS NULL OR EmployeeCredential.is_primary != 'False')
AND v.client_id != '331771 '
GROUP BY
Credentials.credentials,
v.non_billable
ORDER BY
Credentials.credentials
答案 0 :(得分:1)
一些别名和格式确实为这里的一些主要逻辑缺陷提供了一些启示。在where子句中至少有两个谓词,这些谓词将左联接逻辑上转换为内部联接。这是黑暗中的总镜头,因为从您今天的两个问题来看,我们对于表或样本数据都没有任何实际用途。
尽管,最大的担忧是您的where子句试图获取v.non_billable = 0的行以及它等于'FALSE'的位置。不能两者都是。
Select sum(Case When v.non_billable = 0 Then v.duration Else 0 End) / 60 As billable_hours
, sum(Case When (v.non_billable = 0 And sl.payer_id = 63) Then v.duration Else 0 End) / 60 As billable_mro_hours
, c.credentials
From Z_ServiceLedger sl
Inner Join ClientVisit v On sl.clientvisit_id = v.clientvisit_id
Left Join Employees e On v.emp_id = e.emp_id
Left Join EmployeeCredential ec On e.emp_id = ec.emp_id
--if you leave these predicates in the where clause you have turned your left join into an inner join.
AND v.rev_timein <= isnull(ec.end_date, GetDate())
and (ec.is_primary Is Null Or ec.is_primary != 'False')
Left Join Credentials c On c.credential_id = ec.credential_id
Where v.rev_timein >= @param1
And v.rev_timein < DateAdd(day, 1, @param2)
And v.splitprimary_clientvisit_id Is Null
And v.gcode_primary_clientvisit_id Is Null
--you need to pick one value for v.non_billable. It can't be both 0 and 'FALSE' at the same time.
And v.non_billable = 0
And v.non_billable = 'FALSE'
--And v.duration / 60 > 0
and v.duration > 60 --this is the same thing and is SARGable
And sl.amount > 0
And sl.action_type NOT IN ('SERVICE RATE CHANGE', 'CLIENT STATEMENT')
And v.client_id != '331771 '
Group By c.credentials
, v.non_billable
Order By c.credentials
答案 1 :(得分:0)
编辑:修改后的查询,添加了CTE以使用credential_rank
表值构造函数语法来计算FROM (VALUES (...))
。这适用于SQL 2008+。 (https://docs.microsoft.com/en-us/sql/t-sql/queries/table-value-constructor-transact-sql?view=sql-server-2017)
首先,我将构建一个非常简单的数据。
设置:
CREATE TABLE Employees ( emp_id int, emp_name varchar(20) ) ;
INSERT INTO Employees (emp_id, emp_name)
VALUES (1,'Jay'),(2,'Bob')
;
CREATE TABLE Credentials ( credential_id int, credentials varchar(20), credential_rank int ) ;
INSERT INTO Credentials (credential_id, credentials, credential_rank)
VALUES (1,'BA',3),(2,'MA',2),(3,'PhD',1)
;
CREATE TABLE EmployeeCredential (emp_id int, credential_id int, is_primary bit, end_date date )
INSERT INTO EmployeeCredential (emp_id, credential_id, is_primary, end_date)
VALUES
( 1,2,null,'20200101' )
, ( 1,3,0,'20200101' ) /* NON-PRIMARY */
, ( 1,1,1,'20100101' ) /* EXPIRED CRED */
, ( 2,3,null,'20200101' )
, ( 2,3,1,'20200101' )
;
CREATE TABLE z_ServiceLedger ( payer_id int, clientvisit_id int, amount int, action_type varchar(50) ) ;
INSERT INTO z_ServiceLedger ( payer_id, clientvisit_id, amount, action_type )
VALUES (63,1,10,'XXXXX'),(63,2,20,'XXXXX'),(63,3,10,'XXXXX'),(63,4,30,'XXXXX')
;
CREATE TABLE ClientVisit ( clientvisit_id int, client_id int, non_billable bit, duration int, emp_id int , rev_timein date, splitprimary_clientvisit_id int, gcode_primary_clientvisit_id int ) ;
INSERT INTO ClientVisit ( clientvisit_id, client_id, non_billable, duration, emp_id, rev_timein, splitprimary_clientvisit_id, gcode_primary_clientvisit_id )
VALUES
(1, 1234, 0, 110, 1, getDate(), null, null )
, (2, 1234, null, 120, 1, getDate(), null, null )
, (3, 1234, 1, 110, 2, getDate(), null, null )
, (4, 1234, 0, 130, 2, getDate(), null, null )
;
主要查询:
; WITH creds AS (
SELECT c.credential_id, c.credentials, r.credential_rank
FROM Credentials c
LEFT OUTER JOIN (VALUES (1,3),(2,2),(3,1) ) r(credential_id, credential_rank)
ON c.credential_id = r.credential_id
)
SELECT DISTINCT
SUM(CASE WHEN ISNULL(v.non_billable,1) = 0 THEN v.duration ELSE 0 END)*1.0 / 60 AS billable_hours,
SUM(CASE WHEN (ISNULL(v.non_billable,1) = 0 AND zsl.payer_id = 63) THEN v.duration ELSE 0 END)*1.0 / 60 AS billable_mro_hours,
s2.credentials
FROM Z_ServiceLedger zsl
INNER JOIN ClientVisit v ON zsl.clientvisit_id = v.clientvisit_id
AND v.rev_timein >= @param1
AND v.rev_timein < DateAdd(d, 1, @param2)
AND v.splitprimary_clientvisit_id IS NULL
AND v.gcode_primary_clientvisit_id IS NULL
AND ISNULL(v.non_billable,1) = 0
AND v.duration*1.0 / 60 > 0
AND v.client_id <> 331771
INNER JOIN (
SELECT s1.emp_id, s1.emp_name, s1.credential_id, s1.credentials, s1.endDate
FROM (
SELECT e.emp_id, e.emp_name, c.credential_id, c.credentials, ISNULL(ec.end_date,GETDATE()) AS endDate
, ROW_NUMBER() OVER (PARTITION BY e.emp_id ORDER BY c.credential_rank) AS rn
FROM Employees e
LEFT OUTER JOIN EmployeeCredential ec ON e.emp_id = ec.emp_id
AND ISNULL(ec.is_primary,1) <> 0 /* I don't think a NULL is_primary should be TRUE */
LEFT OUTER JOIN creds c ON ec.credential_id = c.credential_id
) s1
WHERE s1.rn = 1
) s2 ON v.emp_id = s2.emp_id
AND v.rev_timein <= s2.endDate /* Credential not expired at rev_timein */
WHERE zsl.amount > 0
AND zsl.action_type NOT IN ('SERVICE RATE CHANGE', 'CLIENT STATEMENT')
GROUP BY s2.credentials
ORDER BY s2.credentials
Results :
| billable_hours | billable_mro_hours | credentials |
|----------------|--------------------|-------------|
| 1.833333 | 1.833333 | MA |
| 2.166666 | 2.166666 | PhD |
需要注意的几件事:
1)整数除法:duration/60
将返回一个整数。因此,如果您拥有duration=70
,那么您将拥有70/60 =1。您会错过10分钟,因为结果将被转换回整数。您损失了那10分钟。可能不是您的意思。最简单的解决方案是将duration
乘以1.0
,以便将其强制为十进制数据类型,并且不会导致将运算视为整数。
2)EmployeeCredential.is_primary != 'False'
:您应该使用实际的布尔值(1/0),而不是考虑字符串“ True” /“ False”。 NULL
值应指示该值为NOT TRUE
或FALSE
,而不是暗示TRUE
。另外,在SQL中,!=
可以指示NOT EQUAL TO
,但是您应该改用<>
。意思是相同的,但是在语法上对SQL更正确。
3)v.non_billable = 0 AND v.non_billable = 'FALSE'
:可以将其缩短为ISNULL(v.non_billable,1)=0
,以使两个检查都短路,特别是因为non_billable
可以是NULL
。比较数字0和字符串'False'时,还可以避免隐式类型转换。
4)v.client_id != '331771 '
:更改为v.client_id<>33171
。首先,我之前提到的!=
至<>
。然后'331771'
被隐式转换为数字。您应该避免隐式转换。
5)您最初在v.non_billable
中有GROUP BY
。由于您没有将其包含在SELECT
中,因此无法将其用于GROUP BY
。另外,您已经过滤掉了non_billable=0
以外的所有内容,因此无论如何GROUP BY
永远不会有多个值。只需排除它即可。
6)CASE WHEN EmployeeCredential.end_date IS NOT NULL THEN EmployeeCredential.end_date ELSE GETDATE() END
:这与说ISNULL(EmployeeCredential.end_date,GETDATE())
相同。
7)除非您出于特定原因实际上需要过滤掉特定记录,否则将更多JOIN
条件放入JOIN
而不是在WHERE
子句中使用它们。这将帮助您更有效地使用初始查询在过滤或减少之前返回的数据。另外,将WHERE
过滤器与LEFT JOIN
一起使用时,可能会导致意外结果。