[英]How do I get the lates tm.detail_notes ( tm.detail_notes for max(tm.timeslip_date))?
I have a query that return some record, but I need to get whatever the latest tm.detail_notes
on max(tm.timeslip_date)
.我有一个返回一些记录的查询,但我需要在
max(tm.timeslip_date)
上获取最新的tm.detail_notes
。 Right now I am getting an error.现在我收到一个错误。 (I have multiple record dates on the table and each day have note)
(我在桌子上有多个记录日期,每天都有注释)
Query询问
SELECT
min(tm.create_date) 'Created Date'
, max(tm.timeslip_date) 'Last Bill Date'
, cases.case_sk
, cases.case_number
, cases.closed_ind
, cases.atty2_sk
, vc.atty2_name 'Business Leader'
, em.smtp_reply_to 'Business Leader Email'
, cases.atty1_sk
, vc.atty1_name 'Assign Attorney'
, tm.detail_notes
FROM dbo.cases
LEFT JOIN dbo.vcases vc ON cases.case_sk = vc.case_sk
LEFT JOIN dbo.employee em ON cases.atty2_sk = em.employee_sk
LEFT JOIN dbo.timeslips tm ON cases.case_sk = tm.case_sk
WHERE
cases.case_number = '0130751-KMG'
AND tm.timeslip_date <= DATEADD(day, -90, GETDATE())
AND cases.closed_ind = 'O'
GROUP BY
cases.case_sk
, cases.case_number
, cases.closed_ind
, cases.atty2_sk
, vc.atty2_name
, em.smtp_reply_to
, cases.atty1_sk
, vc.atty1_name
, tm.detail_notes
Error错误
The text, next, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. text、next 和 image 数据类型无法进行比较或排序,除非使用 IS NULL 或 LIKE 运算符。
I would really appreciate any help on this.我真的很感激这方面的任何帮助。 Lastly, can anyone confirmed my logic, I am trying to get cases with no
tm.timeslip_date
for the last 90 days.最后,任何人都可以证实我的逻辑,我正在尝试获取过去 90 天内没有
tm.timeslip_date
的案例。 I should get that with tm.timeslip_date <= DATEADD(day, -90, GETDATE())
this logic, right.我应该用
tm.timeslip_date <= DATEADD(day, -90, GETDATE())
这个逻辑来得到它,对吧。
Thank you so much太感谢了
Without knowing more tm.detail_notes
appears to be a text
column which is one of the special columns of large, open-ended data types.不知道更多
tm.detail_notes
似乎是一个text
列,它是大型开放式数据类型的特殊列之一。 See ntext, text, and image (Transact-SQL) .请参阅ntext、文本和图像 (Transact-SQL) 。 These fields cannot be used in columns expressions of an aggregate query with
GROUP BY
.这些字段不能用于带有
GROUP BY
的聚合查询的列表达式中。
To incorporate the text
field with your aggregation, consider using two related CTEs for a unit level and aggregate level join.要将
text
字段与聚合合并,请考虑使用两个相关的 CTE 进行单元级别和聚合级别的联接。 Final query will then bind the text column to aggregation joined at the Last Bill Date
.然后,最终查询会将文本列绑定到在
Last Bill Date
加入的聚合。
WITH unit AS (
-- NO AGGREGATIONS
SELECT
tm.create_date
, tm.timeslip_date
, cases.case_sk
, cases.case_number
, cases.closed_ind
, cases.atty2_sk
, vc.atty2_name AS [Business Leader]
, em.smtp_reply_to AS [Business Leader Email]
, cases.atty1_sk
, vc.atty1_name AS [Assign Attorney]
, tm.detail_notes
FROM dbo.cases
LEFT JOIN dbo.vcases vc ON cases.case_sk = vc.case_sk
LEFT JOIN dbo.employee em ON cases.atty2_sk = em.employee_sk
LEFT JOIN dbo.timeslips tm
ON cases.case_sk = tm.case_sk
AND tm.timeslip_date <= DATEADD(day, -90, GETDATE()) -- MOVED FROM WHERE TO ON CLAUSE
WHERE cases.case_number = '0130751-KMG'
AND cases.closed_ind = 'O'
), agg AS (
-- NO JOINS / WHERE
SELECT
MIN(u.create_date) AS [Created Date]
, MAX(u.timeslip_date) AS [Last Bill Date]
, u.case_sk
, u.case_number
, u.closed_ind
, u.atty2_sk
, u.[Business Leader]
, u.[Business Leader Email]
, u.atty1_sk
, u.[Assign Attorney]
FROM unit u
GROUP BY
u.case_sk
, u.case_number
, u.closed_ind
, u.atty2_sk
, u.[Business Leader]
, u.[Business Leader Email]
, u.atty1_sk
, u.[Assign Attorney]
)
-- FINAL QUERY JOINING ON GROUP COLUMNS AND AGG DATES
SELECT agg.*, unit.detail_notes
FROM agg
INNER JOIN unit
ON agg.case_sk = unit.case_sk
AND agg.case_number = unit.case_number
AND agg.closed_ind = unit.closed_ind
AND agg.atty2_sk = unit.atty2_sk
AND agg.[Business Leader] = unit.[Business Leader]
AND agg.[Business Leader Email] = unit.[Business Leader Email]
AND agg.atty1_sk = unit.atty1_sk
AND agg.[Assign Attorney] = unit.[Assign Attorney]
AND agg.[Created Date] = unit.[create_date]
AND agg.[Last Bill Date] = unit.[timeslip_date]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.