选择对象具有某些属性的所有日期

时间:2019-05-16 13:02:30

标签: sql-server tsql

我使用动态属性来管理个人的特征。我有一个表IndividusDynPropValues,每个人和每个属性都有1行 例如:

ID  IndividualID    ValueString  ValueFloat      ValueDate StartDate   Prop
108 2               Outside      NULL            NULL      2018-06-17  Out Status
107 2               Male         NULL            NULL      2018-05-17  Sex
106 1               Dead         NULL            NULL      2018-08-17  Out Status
105 2               Unk          NULL            NULL      2018-04-03  Sex
104 2               Adult        NULL            NULL      2018-04-03  Status
103 1               Femal        NULL            NULL      2018-03-27  Sex
102 1               Adult        NULL            NULL      2018-03-27  Status
101 2               Egg          NULL            NULL      2018-03-25  Status
100 1               Egg          NULL            NULL      2018-03-17  Status

我想查看一个表格,其中所有活着的男性个体(即,性别=“男性”且“外出状态” =“成人”和“外出状态<>”“死亡”)仅使用一个SELECT即可查看。因此,在示例中,我想选择:

IndividualID    BeginDate          End Date      Group
2               2018-05-17         2019-05-16    Living male individuals

有可能吗?该怎么做?

我已经为“所有成人”群组尝试过:

SELECT IDPV1.[IndividualID]
      ,IDPV1.[StartDate] AS BeginDate
     ,GETDATE() AS EndDate
     ,IDPV1.[ValueString]
     ,'All Adult' AS [Group]
   FROM [dbo].[IndividusDynPropValues] IDPV1
   WHERE IDPV1.ValueString='Adulte'

但是它不能用于多个属性...

3 个答案:

答案 0 :(得分:2)

另一种可能的方法是计算匹配数并使用适当的HAVING子句:

表格:

CREATE TABLE #IDVP1 (
    ID int,
    IndividualID int,
    ValueString varchar(50),
    StartDate date,   
    Prop varchar(50)
)
INSERT INTO #IDVP1 
    (ID, IndividualID, ValueString, StartDate, Prop)
VALUES
    (108, 2, 'Outside', '20180617', 'Out Status'),
    (107, 2, 'Male',    '20180517', 'Sex'),
    (106, 1, 'Dead',    '20180817', 'Out Status'),
    (105, 2, 'Unk',     '20180403', 'Sex'),
    (104, 2, 'Adult',   '20180403', 'Status'),
    (103, 1, 'Femal',   '20180327', 'Sex'),
    (102, 1, 'Adult',   '20180327', 'Status'),
    (101, 2, 'Egg',     '20180325', 'Status'),
    (100, 1, 'Egg',     '20180317', 'Status')

声明:

SELECT 
    IndividualID, 
    MIN(CASE WHEN (Prop = 'Sex') AND (ValueString = 'Male') THEN StartDate END) AS BeginDate,
    MIN(CASE WHEN (Prop = 'Out Status') AND (ValueString = 'Dead') THEN StartDate ELSE GETDATE() END) AS EndDate,
    'Living male individuals' AS [Group]
FROM #IDVP1
GROUP BY IndividualID
HAVING 
    SUM(
        CASE WHEN (Prop = 'Sex') AND (ValueString = 'Male') THEN 1 ELSE 0 END +
        CASE WHEN (Prop = 'Status') AND (ValueString = 'Adult') THEN 1 ELSE 0 END +
        CASE WHEN (Prop = 'Out Status') AND (ValueString <> 'Dead') THEN 1 ELSE 0 END
    ) = 3

输出(根据您的示例数据,仅一行匹配):

IndividualID    BeginDate           EndDate             Group
2               17/05/2018 00:00:00 17/05/2019 11:39:42 Living male individuals

答案 1 :(得分:1)

使用CTE:

Create Table #tbl
(
ID Int,
IndividualID Int,
ValueString VarChar(10),
ValueFloat Float,
ValueDate Date,
StartDate Date,
Prop VarChar(15)
)
Insert Into #tbl Values
(108,2,'Outside',NULL,NULL,'2018-06-17','Out Status'),
(107,2,'Male',NULL,NULL,'2018-05-17','Sex'),
(106,1,'Dead',NULL,NULL,'2018-08-17','Out Status'),
(105,2,'Unk',NULL,NULL,'2018-04-03','Sex'),
(104,2,'Adult',NULL,NULL,'2018-04-03','Status'),
(103,1,'Femal',NULL,NULL,'2018-03-27','Sex'),
(102,1,'Adult',NULL,NULL,'2018-03-27','Status'),
(101,2,'Egg',NULL,NULL,'2018-03-25','Status'),
(100,1,'Egg',NULL,NULL,'2018-03-17','Status')

CTE:

;With cte As
(
Select IndividualID,
      Max(Case When Prop = 'Out Status' And ValueString = 'Dead' Then 'Y' Else 'N' End) As Dead,
      Max(Case When Prop = 'Status' And ValueString <> 'Adult' Then 'Y' Else 'N' End) As Adult,
      Max(Case When Prop = 'Sex' And ValueString = 'Male' Then 'Y' Else 'N' End) As Male,
      Min(StartDate) As StartDT,
      Max(StartDate) As EndDT,
      'Living male individuals' As [Group]
From #tbl
Group By IndividualID
)
Select * From cte Where Male = 'Y' And Adult = 'Y' And Dead = 'N'

答案 2 :(得分:-1)

尝试使用汇总来获取ID列表。

Create Table #tbl
(
ID Int,
IndividualID Int,
ValueString VarChar(10),
ValueFloat Float,
ValueDate Date,
StartDate Date,
Prop VarChar(15)
)
Insert Into #tbl Values
(108,2,'Outside',NULL,NULL,'2018-06-17','Out Status'),
(107,2,'Male',NULL,NULL,'2018-05-17','Sex'),
(106,1,'Dead',NULL,NULL,'2018-08-17','Out Status'),
(105,2,'Unk',NULL,NULL,'2018-04-03','Sex'),
(104,2,'Adult',NULL,NULL,'2018-04-03','Status'),
(103,1,'Femal',NULL,NULL,'2018-03-27','Sex'),
(102,1,'Adult',NULL,NULL,'2018-03-27','Status'),
(101,2,'Egg',NULL,NULL,'2018-03-25','Status'),
(100,1,'Egg',NULL,NULL,'2018-03-17','Status')


Select IDPV1.IndividualID
    , MIN(STartDate) as BeginDate
    , MAX(StartDate) as EndDate
    , 'Living male individuals' as [group]
from #tbl IDPV1
where (Prop='Sex' and ValueString='Male')
    or (Prop='Status' and valuestring='Adult')
    and not exists(select 1 
                   from #tbl sub
                   where sub.IndividualID=IDPV1.IndividualID
                      and sub.Prop = 'Out Status' and sub.Prop='Dead')

group by IndividualID
having count(*)=2

结果:

IndividualID    BeginDate   EndDate     group
2               2018-04-03  2018-05-17  Living male individuals