简体   繁体   English

SQL Server-替代包含聚合或子查询的表达式的函数的替代方法

[英]SQL Server - Alternatives to aggregate functions for an expression containing an aggregate or subquery

First of all sorry for the confusing code sample, simplifying my problem is proving tricky. 首先,对于令人困惑的代码示例感到抱歉,简化我的问题非常棘手。

select [...]
    SUM(case when 
        (select [CustID] from [Source3] S3
            where (S3.[CustID] = S1.[StaffID1] or S3.[CustID] = S1.[StaffID2])
            and substring(datename(dw,S1.[AppDateTime]),1,3)=substring(S3.[DoW],1,3)) is not null
        then 1 else 0 end), [...]
from [Source 1] S1
left join [Source 2] S2
    on S2.[SbSpcID]=S1.[SbSpcID]
group by S1.[Condition1], S1.[Condition2]

Basically I need to grab (as just 1 field in this query) a number indicating how many times my Source 3 table contains lines that match the line of my joined table (Source 1 and 2 joined together in this example). 基本上,我需要获取一个数字(作为此查询中的1个字段),该数字指示源3表中包含与联接表的行匹配的行的次数(在此示例中,源1和2联接在一起)。

This join needs to be whenever the [CustID] field of Source 3 equals either the StaffID1 or StaffID2 fields of Source 1 and the day of the week of Source 3's [DoW] field matches the day of the week of the AppDateTime field of Source 1. 每当源3的[CustID]字段等于源1的StaffID1或StaffID2字段且源3的[DoW]字段的星期几与源1的AppDateTime字段的星期几匹配时,此联接就必须为。

substring(datename(dw,S1.[AppDateTime]),1,3) returns things like 'Mon', 'Tue', etc.. which I use because the dates are stored as names ('Thurs', 'Wed') in Source 3. substring(datename(dw,S1。[AppDateTime]),1,3)返回诸如“ Mon”,“ Tue”等之类的名称。由于日期存储为名称(“ Thurs”,“ Wed”),因此使用之在来源3中。

I am getting the 'Cannot perform an aggregate function on an expression containing an aggregate or subquery' error. 我收到“无法对包含聚合或子查询的表达式执行聚合功能”错误。 Normally to solve this I would join Source 3 to the table containing Source 2 and 1, however because I am expecting multiple matches that means that the line count would increase which would ruin the results of other (currently working fine) results of this query. 通常,为了解决这个问题,我会将Source 3加入到包含Source 2和1的表中,但是因为我期望有多个匹配项,所以这意味着行数会增加,这会破坏该查询的其他结果(当前工作正常)。

... ...

.. ..

.

Help! 救命! My brain hurts. 我的脑袋疼。

---------------------EDIT:--------------------- - - - - - - - - - - -编辑: - - - - - - - - - - -

Sample Data: 样本数据:

Source 1&2: 来源1&2:

[Condition1]  [Condition2]  [SbSpcID]  [StaffID1]  [StaffID2]  [AppDateTime]
Hosp          Doc           xxx        Con123       NULL       2018-02-10 16:00
Hosp          Nur           xxx        NULL         Con123     2018-03-15 21:05
Clin          Doc           xxx        Con125       NULL       2018-04-12 18:30
Hosp          DIT           xxx        NULL         NULL       2018-02-25 16:01
Hosp          Reg           xxx        NULL         Con126     2018-06-30 09:45
Hosp          Doc           xxx        Con321       NULL       2018-03-11 11:55
Hosp          Nur           xxx        NULL         Con125     2018-01-01 06:29
Hosp          Doc           xxx        Con125       NULL       2018-02-01 17:00

Source 3: 资料来源3:

[CustID]  [Dow]
Con123    Wed
Con123    Thurs
Con123    Fri
Con123    Sunday
Con125    Mon
Con125    Tues
Con126    Sat
Con321    Mon

Results: 结果:

[Condition1]  [Condition2]  [Query]
Hosp          Doc           0
Hosp          Nur           2
Clin          Doc           0
Hosp          DIT           0
Hosp          Reg           1

Though there are a lot of [Hosp] [Doc] entries they don't match the day of the week of those in Source 3. Whereas both [Hosp][Nur] entries match the day of the week so they both count. 尽管[Hosp] [Doc]条目很多,但它们与Source 3中的星期几不匹配。而两个[Hosp] [Nur]条目都与星期几匹配,因此它们都算在内。

You can still LEFT JOIN Source 3 to Sources 1 & 2. Your join will be weird and inefficient, since you're storing the weekdays as text, instead of their integer values. 您仍然可以将Source 3 LEFT JOIN移至Source 1和Source2。由于将工作日存储为文本(而不是整数值),因此LEFT JOIN将很奇怪且效率低下。

With your sample data (I combined Sources 1 & 2 into 1 CTE): 使用您的样本数据(我将来源1和来源2合并为1个CTE):

-- sample data
WITH 
    Source_1_2(Condition1, Condition2, StaffID1, StaffID2, AppDateTime)
    AS
    (
        SELECT * FROM
        (
            VALUES
            ('Hosp',          'Doc',    'Con123',   NULL,       '2018-02-10 16:00'),
            ('Hosp',          'Nur',    NULL,       'Con123',   '2018-03-15 21:05'),
            ('Clin',          'Doc',    'Con125',   NULL,       '2018-04-12 18:30'),
            ('Hosp',          'DIT',    NULL,       NULL,       '2018-02-25 16:01'),
            ('Hosp',          'Reg',    NULL,       'Con126',   '2018-06-30 09:45'),
            ('Hosp',          'Doc',    'Con321',   NULL,       '2018-03-11 11:55'),
            ('Hosp',          'Nur',    NULL,       'Con125',   '2018-01-01 06:29'),
            ('Hosp',          'Doc',    'Con125',   NULL,       '2018-02-01 17:00')
        ) v(c1, c2, c3, c4, c5)
    )
    ,Source_3(CustID, Dow) AS
    (
        SELECT * FROM
        (
            VALUES
            ('Con123',    'Wed'),
            ('Con123',    'Thurs'),
            ('Con123',    'Fri'),
            ('Con123',    'Sunday'),
            ('Con125',    'Mon'),
            ('Con125',    'Tues'),
            ('Con126',    'Sat'),
            ('Con321',    'Mon')
        ) v(c1, c2)
    )
-- actual query
SELECT
    Source_1_2.Condition1,
    Source_1_2.Condition2,
    COUNT(Source_3.CustID) AS [Count]
FROM Source_1_2
    LEFT JOIN Source_3 ON
        (Source_3.CustID = Source_1_2.StaffID1 OR Source_3.CustID = Source_1_2.StaffID2)
        AND LEFT(Source_3.Dow, 3) = LEFT(DATENAME(WEEKDAY, Source_1_2.AppDateTime), 3)
GROUP BY 
    Source_1_2.Condition1, 
    Source_1_2.Condition2

giving you this result: 给你这个结果:

+------------+------------+-------+
| Condition1 | Condition2 | Count |
+------------+------------+-------+
| Hosp       | DIT        |     0 |
| Clin       | Doc        |     0 |
| Hosp       | Doc        |     0 |
| Hosp       | Nur        |     2 |
| Hosp       | Reg        |     1 |
+------------+------------+-------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server:无法对包含聚合或子查询的表达式执行聚合功能 - SQL Server : cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL Server:无法对包含聚合或子查询的表达式执行聚合功能 - SQL Server: cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL Server无法对包含聚合或子查询的表达式执行聚合函数 - SQL Server Cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL中的减法-无法对包含聚合或子查询的表达式执行聚合功能 - Subtraction in SQL - Cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL 无法对包含聚合或子查询的表达式执行聚合函数 - SQL Cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL错误,无法对包含聚合或子查询的表达式执行聚合功能 - SQL Error of Cannot perform an aggregate function on an expression containing an aggregate or a subquery SQL Server“无法对包含聚合或子查询的表达式执行聚合函数”,但Sybase可以 - SQL Server “cannot perform an aggregate function on an expression containing an aggregate or a subquery”, but Sybase can 无法对包含聚合或子查询的表达式执行聚合功能。 SQL Server 2012 - Cannot perform an aggregate function on an expression containing an aggregate or a subquery. SQL Server 2012 SQL 服务器返回“无法对包含聚合或子查询的表达式执行聚合 function” - SQL Server returns “Cannot perform an aggregate function on an expression containing an aggregate or a subquery” 无法在包含聚合或子查询SQL Server 2012的表达式上执行聚合功能 - Cannot perform an aggregate function on an expression containing an aggregate or a subquery sql server 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM