使用相同列为同一查询中的不同结果组合返回数据

时间:2017-10-11 22:38:01

标签: sql-server

我是SQL的新手,试图自我教学,并且已经在我看来应该是一个非常简单的查询,或者至少是一个非常常见的查询。我甚至不确定我是否知道要用来描述我想要实现的内容的词汇,所以如果这个帖子的标题被误导了,那么道歉! :)

我有一个大数据库。我想识别一行中出现2个结果的行,或者在设定的时间段内同一列中出现另外2个结果。

我开始使用AND,但结果为零。我花了最后2-3个小时在网上搜索,但收效甚微。似乎有许多类似的主题提示UNIONSELECT DISTINCTJOIN或其他解决方案,但我完全无法根据自己的需要调整它们。

任何指针都非常赞赏。

因此,我的表名为VNH.dbo.ClinicalCaseItem

数据表如下所示:

ActualDateTime | CaseId | ProcedureTestId | Description | FeeAmount

我需要将ActualDateTime限制为大于2017-04-03,否则条目数将达到数千。 ProcedureTestId是关键列。我需要专门识别与CaseId合并的ProcedureTestId 309 ProcedureTestId 245以及与CaseId合并的ProcedureTestId 309 ProcedureTestId 326 ActualDateTime | CaseId | ProcedureTestId | Description | FeeAmount 2017-04-25 | 123456 | 309 | Place iv catheter | 30.00 2017-04-25 | 123456 | 329 | GA Patient | 100.00 2017-04-27 | 134523 | 309 | Place iv catheter | 30.00 2017-04-27 | 234567 | 245 | Sedate Patient | 45.00 2017-04-28 | 234567 | 309 | Place iv catheter | 30.00 2017-04-28 | 345321 | 245 | Sedate Patient | 45.00 2017-04-29 | 451324 | 309 | Place iv catheter | 30.00 2017-04-30 | 451324 | 309 | Place iv catheter | 30.00 2017-04-30 | 451324 | 375 | Surgical Pack | 28.00 2017-04-25 | 989898 | 309 | Place iv catheter | 30.00 2017-04-27 | 989898 | 245 | Sedate Patient | 45.00 2017-04-25 | 999999 | 309 | Place iv catheter | 30.00 2017-04-27 | 999999 | 245 | Sedate Patient | 45.00 2017-04-27 | 999999 | 326 | Bathe Patient | 45.00 2017-04-27 | 987654 | 375 | Surgical Pack | 28.00 2017-04-25 | 987654 | 309 | Place iv catheter | 30.00 2017-04-27 | 987654 | 245 | Sedate Patient | 45.00 2017-04-27 | 987654 | 329 | GA Patient | 100.00 2017-04-27 | 987654 | 326 | Bathe Patient | 45.00 }。

我不会详细说明我尝试过的所有不同的查询,因为已经有大约六个左右,而且没有一个能够达到我需要的结果,而且大多数都完全失败了。

非常感谢提前

示例数据:

ActualDateTime | CaseId | ProcedureTestId | Description       | FeeAmount | count_of  
    2017-04-25 | 123456 | 309             | Place iv catheter | 30.00     | 2
    2017-04-25 | 123456 | 329             | GA Patient        | 100.00    | 2   
    2017-04-27 | 234567 | 245             | Sedate Patient    | 45.00     | 2  
    2017-04-28 | 234567 | 309             | Place iv catheter | 30.00     | 2  
    2017-04-25 | 989898 | 309             | Place iv catheter | 30.00     | 2
    2017-04-27 | 989898 | 245             | Sedate Patient    | 45.00     | 2
    2017-04-25 | 999999 | 309             | Place iv catheter | 30.00     | 2
    2017-04-27 | 999999 | 245             | Sedate Patient    | 45.00     | 2
    2017-04-25 | 987654 | 309             | Place iv catheter | 30.00     | 3
    2017-04-27 | 987654 | 245             | Sedate Patient    | 45.00     | 3
    2017-04-27 | 987654 | 329             | GA Patient        | 100.00    | 3

查询的预期结果是:

static void printText(string textToPrint)
    {
        ConsoleKeyInfo cki;
        do
        {
            int x = 0;
            while (Console.KeyAvailable == false)
            {
                Console.Write(textToPrint[x]);
                System.Threading.Thread.Sleep(150);
                x++;
                if (x >= textToPrint.Length)
                    return;
            }
            cki = Console.ReadKey(true);
            Console.WriteLine("\r" + textToPrint);
        } while (cki.Key != ConsoleKey.X);
        Console.ReadLine();
    }

1 个答案:

答案 0 :(得分:1)

拥有样本数据非常有用,但除非它与“预期结果”相关联,否则我们无法将任何查询输出与之比较;因此,我们只能发明一些可行的东西 - 这就是我在下面提出的内容。

我对你的话的解释是,如果案件至少已完成程序(309和245)或(309和326),则需要caseid。现在,当我将该需求与样本数据进行比较时,我找不到满足该条件的任何行,所以我添加了一些。但是请注意,在我采用的逻辑中,任何具有程序(245和326但不是309)的案例也将被退回。

这里的基本“技巧”(又名“方法”)是在SUM()函数中使用case表达式,并使用having子句(允许根据聚合值过滤结果)对其进行评估。

这个SQL Fiddle让您执行自己的试用查询。

MS SQL Server 2014架构设置

CREATE TABLE Table1
    ([ActualDateTime] datetime, [CaseId] int, [ProcedureTestId] int, [Description] varchar(17), [FeeAmount] int)
;

INSERT INTO Table1
    ([ActualDateTime], [CaseId], [ProcedureTestId], [Description], [FeeAmount])
VALUES
    ('2017-04-25 00:00:00', 989898, 309, 'Place iv catheter', 30.00),
    ('2017-04-27 00:00:00', 989898, 245, 'Sedate Patient', 45.00),

    ('2017-04-25 00:00:00', 999999, 309, 'Place iv catheter', 30.00),
    ('2017-04-27 00:00:00', 999999, 245, 'Sedate Patient', 45.00),
    ('2017-04-27 00:00:00', 999999, 326, 'whatever 326 is', 45.00),

    ('2017-04-25 00:00:00', 123456, 309, 'Place iv catheter', 30.00),
    ('2017-04-25 00:00:00', 123456, 329, 'GA Patient', 100.00),
    ('2017-04-27 00:00:00', 134523, 309, 'Place iv catheter', 30.00),
    ('2017-04-27 00:00:00', 234567, 245, 'Sedate Patient', 45.00),
    ('2017-04-28 00:00:00', 234567, 309, 'Place iv catheter', 30.00),
    ('2017-04-28 00:00:00', 345321, 245, 'Sedate Patient', 45.00),
    ('2017-04-29 00:00:00', 451324, 309, 'Place iv catheter', 30.00),
    ('2017-04-30 00:00:00', 451324, 309, 'Place iv catheter', 30.00),
    ('2017-04-30 00:00:00', 451324, 375, 'Surgical Pack', 28.00)
;

查询1

      select CaseId
         , sum(case when ProcedureTestId in (309,245,326) then 1 else 0 end) count_of
      from Table1
      group by CaseId
      having sum(case when ProcedureTestId in (309,245,326) then 1 else 0 end) > 1

<强> Results

| CaseId | count_of |
|--------|----------|
| 234567 |        2 |
| 451324 |        2 |
| 989898 |        2 |
| 999999 |        3 |

查询2

select
      t.*, d.count_of
from Table1 t
inner join (
      select CaseId
         , sum(case when ProcedureTestId in (309,245,326) then 1 else 0 end) count_of
      from Table1
      group by CaseId
      having sum(case when ProcedureTestId in (309,245,326) then 1 else 0 end) > 1
      ) d on t.CaseId = d.CaseId
order by caseid, ProcedureTestId

<强> Results

|       ActualDateTime | CaseId | ProcedureTestId |       Description | FeeAmount | count_of |
|----------------------|--------|-----------------|-------------------|-----------|----------|
| 2017-04-27T00:00:00Z | 234567 |             245 |    Sedate Patient |        45 |        2 |
| 2017-04-28T00:00:00Z | 234567 |             309 | Place iv catheter |        30 |        2 |
| 2017-04-29T00:00:00Z | 451324 |             309 | Place iv catheter |        30 |        2 |
| 2017-04-30T00:00:00Z | 451324 |             309 | Place iv catheter |        30 |        2 |
| 2017-04-30T00:00:00Z | 451324 |             375 |     Surgical Pack |        28 |        2 |
| 2017-04-27T00:00:00Z | 989898 |             245 |    Sedate Patient |        45 |        2 |
| 2017-04-25T00:00:00Z | 989898 |             309 | Place iv catheter |        30 |        2 |
| 2017-04-27T00:00:00Z | 999999 |             245 |    Sedate Patient |        45 |        3 |
| 2017-04-25T00:00:00Z | 999999 |             309 | Place iv catheter |        30 |        3 |
| 2017-04-27T00:00:00Z | 999999 |             326 |   whatever 326 is |        45 |        3 |
相关问题