If you only want 4 rows, because they are only 4 dates, then you should start select by dates, not by tagindex
. And then you will want to show each value of tagname
in columns, correct?
Considering the data:
create table tagtablepocos
(
TagName varchar(20),
tagindex int
);
create table floattablepocos
(
dateandtime datetime,
val float,
tagindex int
);
insert into tagtablepocos values ('[AGUA]FT[1].Out',1);
insert into tagtablepocos values ('[AGUA]LT[1].Out',5);
insert into floattablepocos values ('2018-03-12 10:52:03', 66,1);
insert into floattablepocos values ('2018-03-12 10:52:03', 8,5);
insert into floattablepocos values ('2018-03-12 10:52:08', 23,1);
insert into floattablepocos values ('2018-03-12 10:52:08', 47.99,5);
insert into floattablepocos values ('2018-03-12 10:52:32', 5,1);
insert into floattablepocos values ('2018-03-12 10:52:32', 47.99,5);
insert into floattablepocos values ('2018-03-12 10:52:45', 0,1);
insert into floattablepocos values ('2018-03-12 10:52:45', 47.99,5);
You can work with sub-select for each column you want:
Select distinct
FTags.DateAndTime,
(select x.val from FloatTablePocos x where x.dateandtime = FTags.DateAndTime and x.TagIndex = 1) as FT,
(select x.val from FloatTablePocos x where x.dateandtime = FTags.DateAndTime and x.TagIndex = 5) as LT
from FloatTablePocos FTags;
Result:
Iputitin SQLFiddle