Sql server 为同一查询中的不同结果组合使用同一列返回数据
我是SQL新手,尝试自学,并且已经在努力解决一个在我看来应该是非常简单的查询,或者至少是一个非常常见的查询。我甚至不确定我是否知道描述我正在努力实现的目标的词汇,因此,如果这篇文章的标题被证明具有误导性,我深表歉意!:) 我有一个很大的数据库。我想标识一列中的两个结果同时出现的行,或者同一列中的另两个结果在设定的时间段内同时出现的行 我开始使用Sql server 为同一查询中的不同结果组合使用同一列返回数据,sql-server,Sql Server,我是SQL新手,尝试自学,并且已经在努力解决一个在我看来应该是非常简单的查询,或者至少是一个非常常见的查询。我甚至不确定我是否知道描述我正在努力实现的目标的词汇,因此,如果这篇文章的标题被证明具有误导性,我深表歉意!:) 我有一个很大的数据库。我想标识一列中的两个结果同时出现的行,或者同一列中的另两个结果在设定的时间段内同时出现的行 我开始使用和,但结果为零。在过去的2-3个小时里,我一直在网上搜索,但效果有限。似乎有很多类似的线程建议使用UNION、selectdistinct、JOINs或其
和
,但结果为零。在过去的2-3个小时里,我一直在网上搜索,但效果有限。似乎有很多类似的线程建议使用UNION
、selectdistinct
、JOIN
s或其他解决方案,但我完全无法根据自己的需要调整它们
非常感谢任何指点
因此,我的表名为VNH.dbo.ClinicalCaseItem
数据表如下所示:
ActualDateTime | CaseId | ProcedureTestId | Description | FeeAmount
我需要将ActualDateTime
限制在大于2017-04-03
的范围内,否则条目数将达到1000条。ProcedureTestId
是关键列。我需要专门识别与ProcedureTestId 245
一起出现的CaseId
,以及与ProcedureTestId 326
一起出现的CaseId
和ProcedureTestId 309
我不会详细说明我尝试过的所有不同的查询,因为已经有六个左右的查询,没有一个接近我需要的结果,而且大多数都完全失败了
非常感谢
示例数据:
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
拥有样本数据非常有用,但除非它与“预期结果”相耦合,否则我们无法将任何查询输出与之进行比较;因此,我们只能发明一些可能有效的东西——这就是我下面介绍的 我对你所说的话的理解是,如果案件至少通过了程序(309和245)或(309和326),你需要caseid。现在,当我将该要求与示例数据进行比较时,我找不到任何符合该标准的行,因此我添加了一些符合该标准的行。但是也要注意,在逻辑中,我采用的任何具有程序(245和326,但不是309)的案例也将被返回 这里最基本的“技巧”(也称为“方法”)是在SUM()函数中使用case表达式,并使用
having
子句(允许基于聚合值过滤结果)对此进行求值
这让您可以执行自己的试用查询
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)
;
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
| CaseId | count_of |
|--------|----------|
| 234567 | 2 |
| 451324 | 2 |
| 989898 | 2 |
| 999999 | 3 |
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
| 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 |
查询1:
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)
;
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
| CaseId | count_of |
|--------|----------|
| 234567 | 2 |
| 451324 | 2 |
| 989898 | 2 |
| 999999 | 3 |
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
| 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 |
:
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)
;
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
| CaseId | count_of |
|--------|----------|
| 234567 | 2 |
| 451324 | 2 |
| 989898 | 2 |
| 999999 | 3 |
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
| 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 |
查询2:
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)
;
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
| CaseId | count_of |
|--------|----------|
| 234567 | 2 |
| 451324 | 2 |
| 989898 | 2 |
| 999999 | 3 |
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
| 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 |
:
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)
;
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
| CaseId | count_of |
|--------|----------|
| 234567 | 2 |
| 451324 | 2 |
| 989898 | 2 |
| 999999 | 3 |
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
| 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 |
实际的数据样本会有所帮助。问:你所说的“关键栏目”是什么意思?它是主键还是外键?对不起,我的意思是——最重要的——就像我的问题所涉及的那个。示例数据:请提供示例数据和您想要获得的输出。对不起,我的意思是——最重要的——正如我的问题所涉及的那个。2017-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-04-4四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四四;45.00 2017-04-29 | 451324|309 |放置静脉导管| 30.00 2017-04-30 | 451324 | 309 |放置静脉导管| 30.00 2017-04-30 | 451324 | 375 |外科手术包| 28.00希望根本格式不好的帮助,是吗?希望你能理解。我想知道哪些行既有309和329,又有309和245,不包括所有其他结果。请注意“预期结果”的重要性(例如,您想要摘要还是详细布局?),而且示例数据也应提供“预期结果”的数据。谢谢。有趣的是,我提供的示例数据确实有匹配的行,但您的解释是正确的。查询2的结果几乎就是我想要的结果。我需要行CaseId而不包括ProcedureTestId 309 excluded,以及任何不包括309、326或245的ProcedureTestId,因此将排除所有CaseId 451324和底部行999999。我将编辑我的帖子以包含扩展数据表并添加结果表。