展望为查询返回所需的最终结果,我会使用这样的事情:
现在到最后我需要得到以下da TA:DataDate,DiskType,AggregateSizes(其他,使用,总计),聚合卷大小(其他,总和中,在总体积的合计)
select AggregateUuid = a.uuid , DiskType = dt.TypeName , DataDate = ad.DataDate , AggregateSizeAvailable = ad.SizeAvailable , AggregateSizeUsed = ad.SizeUsed , AggregateSizeTotal = ad.SizeTotal , VolumeSizeAvailable = sum(vd.SizeAvailable) , VolumeSizeUsed = sum(vd.SizeUsed) , VolumeSizeTotal = sum(vd.SizeTotal) from [Aggregate] a inner join DiskType dt on dt.Id = a.DiskTypeId inner join AggregateData ad on ad.Aggregate_uuid = a.uuid left join Volume v on v.Aggregate_uuid = a.uuid left join VolumeData vd on vd.Volume_uuid = v.uuid and vd.DataDate = ad.DataDate where a.uuid = 'C58D0098-D1A4-4ee9-A0E9-7de3eeb6275C' group by a.uuid , dt.TypeName , ad.DataDate , ad.SizeAvailable , ad.SizeUsed , ad.SizeTotal order by a.uuid, ad.DataDate;
测试设置
:
http://rextester.com/HZZHLI45077
create table DiskType(
Id int identity(1,1) not null
, TypeName nvarchar(255) null
);
set identity_insert DiskType on;
insert into DiskType (Id, TypeName) values
(1,'Type1'), (2,'Type2');
set identity_insert DiskType off;
create table [Aggregate](
Id bigint identity(1,1) not null
, uuid uniqueidentifier not null
, Name nvarchar(255) not null
, Cluster_uuid uniqueidentifier not null
, DiskTypeid int not null
);
insert into [Aggregate] (uuid, name, cluster_uuid, disktypeid)
select 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', 'ex', newid(), 1;
create table AggregateData(
Id bigint identity(1,1) not null
, Aggregate_uuid uniqueidentifier not null
, DataDate date not null
, SizeAvailable bigint not null
, SizeTotal bigint not null
, SizeUsed bigint not null
, PercentageUsed int not null
);
insert into AggregateData
select 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', '20170101', 12,100,87,87
union all select 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', '20170102', 9,100,90,90
union all select 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', '20170103', 6,100,93,93;
create table Volume(
Id bigint identity(1,1) not null
, uuid uniqueidentifier not null
, Name nvarchar(255) not null
, Aggregate_uuid uniqueidentifier not null
, ServiceClassid int null
, Protocolid int not null
, Environmentid int not null
);
insert into Volume
select '00000000-0000-0000-0000-000000000001', 'v1'
, 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', null, 1, 1
union all select '00000000-0000-0000-0000-000000000002', 'v2'
, 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', null, 1, 1
union all select '00000000-0000-0000-0000-000000000003', 'v3'
, 'C58D0098-D1A4-4EE9-A0E9-7DE3EEB6275C', null, 1, 1;
create table VolumeData(
Id bigint identity(1,1) not null
, Volume_uuid uniqueidentifier not null
, DataDate date not null
, SizeAvailable bigint not null
, SizeTotal bigint not null
, SizeUsed bigint not null
, PercentageUsed int not null
);
insert into VolumeData
select '00000000-0000-0000-0000-000000000001', '20170101', 4,33,29,88
union all select '00000000-0000-0000-0000-000000000002', '20170101', 4,33,29,88
union all select '00000000-0000-0000-0000-000000000003', '20170101', 4,34,29,87
union all select '00000000-0000-0000-0000-000000000001', '20170102', 3,33,30,91
union all select '00000000-0000-0000-0000-000000000002', '20170102', 3,33,30,91
union all select '00000000-0000-0000-0000-000000000003', '20170102', 3,34,30,90
union all select '00000000-0000-0000-0000-000000000001', '20170103', 2,33,31,94
union all select '00000000-0000-0000-0000-000000000002', '20170103', 2,33,31,94
union all select '00000000-0000-0000-0000-000000000003', '20170103', 2,34,31,93
go
/* -------------------------------------------------------- */
select
AggregateUuid = a.uuid
, DiskType = dt.TypeName
, DataDate = convert(varchar(10),ad.DataDate,121)
, AggregateSizeAvailable = ad.SizeAvailable
, AggregateSizeUsed = ad.SizeUsed
, AggregateSizeTotal = ad.SizeTotal
, VolumeSizeAvailable = sum(vd.SizeAvailable)
, VolumeSizeUsed = sum(vd.SizeUsed)
, VolumeSizeTotal = sum(vd.SizeTotal)
from [Aggregate] a
inner join DiskType dt on dt.Id = a.DiskTypeId
inner join AggregateData ad on ad.Aggregate_uuid = a.uuid
left join Volume v on v.Aggregate_uuid = a.uuid
left join VolumeData vd on vd.Volume_uuid = v.uuid
and vd.DataDate = ad.DataDate
where a.uuid = 'C58D0098-D1A4-4ee9-A0E9-7de3eeb6275C'
group by
a.uuid
, dt.TypeName
, ad.DataDate
, ad.SizeAvailable
, ad.SizeUsed
, ad.SizeTotal
order by a.uuid, ad.DataDate;
结果:
+--------------------------------------+----------+------------+------------------------+-------------------+--------------------+---------------------+----------------+-----------------+
| AggregateUuid | DiskType | DataDate | AggregateSizeAvailable | AggregateSizeUsed | AggregateSizeTotal | VolumeSizeAvailable | VolumeSizeUsed | VolumeSizeTotal |
+--------------------------------------+----------+------------+------------------------+-------------------+--------------------+---------------------+----------------+-----------------+
| c58d0098-d1a4-4ee9-a0e9-7de3eeb6275c | Type1 | 2017-01-01 | 12 | 87 | 100 | 12 | 87 | 100 |
| c58d0098-d1a4-4ee9-a0e9-7de3eeb6275c | Type1 | 2017-01-02 | 9 | 90 | 100 | 9 | 90 | 100 |
| c58d0098-d1a4-4ee9-a0e9-7de3eeb6275c | Type1 | 2017-01-03 | 6 | 93 | 100 | 6 | 93 | 100 |
+--------------------------------------+----------+------------+------------------------+-------------------+--------------------+---------------------+----------------+-----------------+
是的,你是对的。这对select中的子查询看起来很不错。现在对于我需要的所有其他列,我需要在select中添加另一个子查询,因为据我所知在select中使用时只能返回1个值。这会使它使用起来有点麻烦。所以,虽然有效的我的事情,我选择使用子查询的初始方法并不理想。我将在连接中使用子查询来尝试它。似乎我需要更好的方法。 – Marc
@Marc。 。 。如果您有*另一个问题涉及多个栏目,那么您应该将其作为问题而不是评论。 –
你说得对。所以我会在第二部分提出另一个问题。非常感谢。 – Marc