Update in SQL from a select statement to another table - sql-update

I have a select query and working fine. Now the output of this query I want to update in another table.
Question is - output by a select query, aggregate stuff, I want to keep in another table. How can I do that?
SELECT
CostID as CostID,
SUM(Budget) as Budget,
SUM(TD) as TD,
SUM(Forecast) as Forecast,
FROM
A
GROUP BY
TD,
Forecast,
CostID,
Cost,
)
I want to update CostID, Sum of Budget, Sum of TD, Forecast in another table.

Is there a key in the other table where you can join your select statement above?
If there is a key then you can just write an update statement:
UPDATE SOURCE_TABLE
SET SOURCE_TABLE.Cost_ID = NEW_QUERY.Cost_ID
SOURCE_TABLE.Budget = NEW_QUERY.Budget
SOURCE_TABLE.TD = NEW_QUERY.TD
SOURCE_TABLE.Forecast = NEW_QUERY.Forecast
FROM SOURCE_TABLE
INNER JOIN
(
SELECT CostID as CostID,
SUM(Budget) as Budget,
SUM(TD) as TD,
SUM(Forecast) as Forecast
FROM A
GROUP BY TD,
Forecast,
CostID,
Cost
) AS NEW_QUERY
ON SOURCE_TABLE.joining_key = NEW_QUERY.joining_key

Related

how do i join two tables using dax

i have two tables in power bi that i am trying to join or lookup.
the period table has the days, months, and month id.
the data table has the days and data.
i need to pull the month id from the period table into the data table so that i have a table like the final result picture where the month id is now included.
final result
i tried using lookup and crossjoin but they dont work.
Month = EVALUATE
ADDCOLUMNS (
Sales,
"month", RELATED ( 'period'[day] )
)

How is measure able to filter dimension via fact table?

I have 2 tables: dimProduct and factSales
Dim table has productid, name, category
Fact table has salesid, productid, status, amount, paiddate
I have a table visual that shows the status from the fact table. Against each status I want to show the count of products and the count of distinct category.
CountProducts=DISTINCTCOUNT(FACTSALES[PRODUCTID])
CountDistinctCategory=DISTINCTCOUNT(dimProduct[category])
How to correct this?
Set the cross-filter direction for your relationship to 'Both', so that filtering can also propagate from the fact table to the dimension table.
Alternatively, using DAX:
CountDistinctCategory =
CALCULATE(
DISTINCTCOUNT( dimProduct[category] ),
TREATAS(
VALUES( factSales[productid] ),
dimProduct[productid]
)
)

How to do an Update from a Select in Azure?

I need to update a second table with the results of this query:
SELECT Tag, battery, Wearlevel, SensorTime
FROM (
SELECT m.* , ROW_NUMBER() OVER (PARTITION BY TAG ORDER BY SensorTime DESC) AS rn
FROM [dbo].[TELE] m
) m2
where m2.rn = 1;
But. I had a hard time fixing the SET without messing it up. I want to have a table which has all data from last date of each TAG without duplicates.
Below code maybe you want.
UPDATE
Table_A
SET
Table_A.Primarykey = 'ss'+Table_B.Primarykey,
Table_A.AddTime = 'jason_'+Table_B.AddTime
FROM
Test AS Table_A
INNER JOIN UsersInfo AS Table_B
ON Table_A.id = Table_B.id
WHERE
Table_A.Primarykey = '559713e6-0d85-4fe7-87a4-e9ceb22abdcf'
For more details, you also can refer below posts and blogs.
1. How do I UPDATE from a SELECT in SQL Server?
2. How to UPDATE from SELECT in SQL Server

New column indicating if row is the first instance of the value for the Entity ID in PowerQuery Editor

I currently have a column that is created using the following DAX formula which indicates if the listed activity is the first one ever for that Entity ID:
First Time Activity =
if('Activity Table'[Timestamp]=
CALCULATE(min('Activity Table'[Timestamp]),
filter('Activity Table',
'Activity Table'[Entity ID] = earlier('Activity Table'[Entity ID]) &&
'Activity Table'[Activity Name] = earlier('Activity Table'[Activity Name])
)
)
,1,BLANK())
But I need this now to be a column made in PowerQuery instead of DAX. Any help on the PowerQuery formula would be much appreciated.
Thanks
In the query editor, you can do the following steps:
Group by ID and Activity taking the min over the Date column as its aggregation.
Merge that grouped table back onto the original table (matching on ID and Activity and using left outer join).
Expand the min date column.
On the merged and expanded table, replace non-nulls in the expanded column with 1.

Exasol Update Table using subselect

I got this statement, which works in Oracle:
update table a set
a.attribute =
(select
round(sum(r.attribute1),4)
from table2 p, table3 r
where 1 = 1
and some joins
)
where 1 = 1
and a.attribute3 > 10
;
Now I would like to do the same statement in Exasol DB. But I got error [Code: 0, SQL State: 0A000] Feature not supported: this kind of correlated subselect (Session: 1665921074538906818)
After some research, I found out you need to write the query in following syntax:
UPDATE table a
set a.attribute = r.attribute2
FROM table a, table2 p, table3 r
where 1 = 1
and some joins
and a.attribute3 > 10;
The problem is I can't take sum of r.attribute2. So I get unstable set of rows. Is there any way to do the first query in Exasol DB?
Thanks for help guys!
Following SQL UPDATE statement will work for cases if JOIN between table1 and table2 are 1-to-1 (or if there is a 1-to-1 relation between target table and resultset of JOINs)
In this case target table val column is updated otherwise an error is returned
UPDATE table1 AS a
SET a.val = table2.val
FROM table1, table2
WHERE table1.id = table2.id;
On the other hand, if the join is causing multiple returns for single table1 rows, then the unstable error raised.
If you want to sum the column values of the multiplying rows, maybe following approach can help
First sum all rows of table2 in bases of table1 and use this sub-select as a new temp table, then use this in UPDATE FROM statement
UPDATE table1 AS a
SET a.val = table2.val
FROM table1
INNER JOIN (
select id, sum(val) val from table2 group by id
) table2
ON table1.id = table2.id;
I tried to solve the issue using two tables
In your case probably you will use table2 and table3 in the subselect statement
I hope this is the answer you were looking for