March 21, 2018 at 9:35 pm
I have the below query which merges 9 tables into a single table NBCVadodra but doesn't get updated everytime if there are changes in the source table. How do I fix this ?
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till INTO NBCVadodra
FROM
(SELECT Tstamp, Inverter_id, AC_Power, total_energy_generated_till FROM [dbo].[IN-023C-I1]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I2]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I3]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I4]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I5]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I6]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I7]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I8]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I9]
)t
March 21, 2018 at 9:48 pm
rva.raghav - Wednesday, March 21, 2018 9:35 PMI have the below query which merges 9 tables into a single table NBCVadodra but doesn't get updated everytime if there are changes in the source table. How do I fix this ?
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till INTO NBCVadodra
FROM
(SELECT Tstamp, Inverter_id, AC_Power, total_energy_generated_till FROM [dbo].[IN-023C-I1]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I2]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I3]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I4]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I5]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I6]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I7]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I8]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I9]
)t
First of all, you do use SELECT INTO unless you are creating the table from scratch each time.
f you are inserting new data and updating existing data you should be using the MERGE statement.
I
March 21, 2018 at 10:10 pm
Merge statement doesn't combine tables. Stack them in rows one by one. Can you suggest some other way ?
March 21, 2018 at 10:58 pm
rva.raghav - Wednesday, March 21, 2018 10:10 PMMerge statement doesn't combine tables. Stack them in rows one by one. Can you suggest some other way ?
Haven't used MERGE a lot but I would beg to differ with you. In your original code you combined all the source tables into a erived table. You can do the same thing with MERGE. You could also change the derived table into a CTE and use that as your source table.
March 21, 2018 at 11:28 pm
rva.raghav - Wednesday, March 21, 2018 10:10 PMMerge statement doesn't combine tables. Stack them in rows one by one. Can you suggest some other way ?
Yes, You could try use CTE instead...
March 22, 2018 at 9:38 am
I'm pretty sure what you want is a view:CREATE VIEW NBCVadodra
AS
SELECT Tstamp, Inverter_id, AC_Power, total_energy_generated_till
FROM [dbo].[IN-023C-I1]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I2]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I3]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I4]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I5]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I6]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I7]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I8]
UNION ALL
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till
FROM [dbo].[IN-023C-I9]
GO
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
March 22, 2018 at 3:38 pm
rva.raghav - Wednesday, March 21, 2018 9:35 PMI have the below query which merges 9 tables into a single table NBCVadodra but doesn't get updated everytime if there are changes in the source table. How do I fix this ?
SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till INTO NBCVadodra
FROM
(SELECT Tstamp, Inverter_id, AC_Power, total_energy_generated_till FROM [dbo].[IN-023C-I1]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I2]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I3]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I4]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo]. [IN-023C-I5]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I6]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I7]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I8]UNION ALL SELECT Tstamp, Inverter_id, AC_Power,total_energy_generated_till FROM [dbo].[IN-023C-I9]
)t
Convert your query to a partitioned view and everything will update magically. If you do it right, it'll be an updateable view, to boot.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 22, 2018 at 8:11 pm
Hi sgmunson,
Thanks for the answer. Appreciate it. I would like to know how to write a sub view inside this view.
For example a calculation like this.
CREATE VIEW MeterReadings
AS
SELECT Tstamp,Inverter_ID
,MAX(total_energy_generated_till)-MIN(total_energy_generated_till) [total_energy_generated_till] FROM NBCVadodra
GROUP BY Tstamp, Inverter_ID
March 22, 2018 at 9:45 pm
rva.raghav - Thursday, March 22, 2018 8:11 PMHi sgmunson,Thanks for the answer. Appreciate it. I would like to know how to write a sub view inside this view.
For example a calculation like this.
CREATE VIEW MeterReadings
AS
SELECT Tstamp,Inverter_ID
,MAX(total_energy_generated_till)-MIN(total_energy_generated_till) [total_energy_generated_till] FROM NBCVadodra
GROUP BY Tstamp, Inverter_ID
That would likely be a mistake. Treat the view that SGMunson wrote as if it were a table. Better yet, look up "Partitioned Views" and learn how to create them so that the underlying tables aren't only readable through the view but they'll be updateable through the view, as well.
The reason why you don't want a GROUP BY/Aggregates in the view is because someone will certainly try to apply criteria to the aggregates in the view and the view will have to materialize all the tables to come up with the answer instead of just (possibly) reading from one table.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply