这是数据集的表格视图。在计数列中,如果该放置位置同时存在于两个数据源(即“ DBM”和“ DCM”)中,我想返回输出。
我处理了选择查询,通过该查询按“数据源和放置位置”列对数据进行分组,然后计算了放置位置,如果为1,则仅在一个数据源中,如果为2,则在两个数据源中,但无法将其转换为更新查询。
选择查询-
Select placement, COUNT(placement)
from (select placement from [dataset1]
group by placement, data_source
)
group by placement
答案 0 :(得分:1)
使用这两个表:
create table dataset1(
Date date,
Data_source varchar(3),
Placement varchar(15),
count int
)
create table dataset2(
id int identity(1,1),
Placement varchar(15)
)
create table dataset3(
id int identity(1,1),
Placement varchar(15)
)
和这些数据:
dataset1
Date Data_source Placement count
1900-01-01 DBM Placement1 (NULL)
1900-01-02 DCM Placement1 (NULL)
1900-01-03 DCM Placement1 (NULL)
1900-01-04 DCM Placement2 (NULL)
1900-01-05 DCM Placement2 (NULL)
dataset2
id Placement
1 Placement1
dataset3
id Placement
1 Placement1
使用此查询:
update dataset1 set count=
(case when exists(select Placement from dataset2 b where b.placement=dataset1.placement) then 1 else 0 end )+
(case when exists(select Placement from dataset3 b where b.placement=dataset1.placement) then 2 else 0 end )
您获得此结果
Date Data_source Placement count
1900-01-01 DBM Placement1 3
1900-01-02 DCM Placement1 3
1900-01-03 DCM Placement1 3
1900-01-04 DCM Placement2 0
1900-01-05 DCM Placement2 0
只是为了澄清
答案 1 :(得分:0)
您可以按以下方式简化查询:
select placement, count(distinct datasource)
from [dataset1]
group by placement
答案 2 :(得分:0)
您可以将public Bitmap ConvertByteArrayToBitmap(byte[] Array)
{
if (Array == null) return null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(Array, 0, Array.Length);
ms.Position = 0L;
return new Bitmap(ms);
}
}
与COUNT DISTINCT
结合使用,以按放置位置计数不同数据源的数量。然后,您也可以通过放置将原始数据集与这些结果结合起来。
GROUP BY
如果您不想只计算UPDATE T SET
Count = DistinctDataSources
FROM
dataset1 AS T
INNER JOIN (
SELECT
D.Placement,
COUNT(DISTINCT(D.DataSource)) AS DistinctDataSources
FROM
dataset1 AS D
WHERE
D.DataSource IN ('DBM', 'DCM')
GROUP BY
D.Placement
) AS P
ON T.Placement = P.Placement
和WHERE
,则可以删除DBM
。
答案 3 :(得分:0)
如果两个数据源(即'DBM'和'DCM')中都存在该展示位置,我想返回输出。
我想你只是想要
select placement
from dataset1
where data_source in ('DBM', 'DCM')
group by placement
having count(distinct data_source) = 2;
如果您想要原始数据,那么我建议exists
:
select d.*
from dataset1 d
where exists (select 1
from dataset1 d2
where d2.placement = d.placement and
d2.data_source = 'DBM'
) and
exists (select 1
from dataset1 d2
where d2.placement = d.placement and
d2.data_source = 'DCM'
);