Teradata update target table with source table with multiple ID instances with different dates - sql-update

I have two tables A and B.
Table A has has fields such as ID,DATE,AMOUNT etc. ID field has id numbers spanning from 10-15 digit numbers. Some of the numbers are 1 to 4 digit in length and we need to get full digit for these sets. There is another table B that has a ID number details and matches with partial Ids in table A. I need to update table A partial ID with full ID from table B where only few numbers match.
I attempted the following and keep getting an error, please help.
Update A
FROM tablenamexyz a,
(select distinct a.ID1,b.ID,date
FROM TABLE tablenamexyz a,
tablenamebty b
where a.ID1=RIGHT(b.ID,4)
QUALIFY ROW_NUMBER(OVER(PARTITION BY b.ID ORDER BY date DESC)=1)z
SET a.ID=b.ID
where a.ID1=b.ID1
--I keep getting update failed error-7547, Target row update by multiple source rows. You can see in table B, we can just use one source with the latest date. Please help. Thanks in advance.

No need to include table A in your derived table Z, but you do need to partition by the value you are going to use in the JOIN (ID1 not ID) for uniqueness:
Update A
FROM tablenamexyz a,
(select RIGHT(b.ID,4) as ID_4,b.ID,"date"
FROM tablenamebty b
QUALIFY ROW_NUMBER()OVER(PARTITION BY ID_4 ORDER BY "date" DESC)=1)z
SET a.ID=z.ID
where a.ID1=z.ID_4
Note that date is a reserved word so if that's really your column name you should quote it.

Related

Categorised data in PowerBi

I am looking for a suggestion on how to categorised/group data in PowerBi.
For example,
I have set up a conditional column in Power Query to achieve the results seeing in “Group” column by saying if ID is 8304 then Group B, if ID is 8660 then Group F -- but the database is large and I am already facing a performance issue when trying to set up a report based on individual Groups, it takes long to load the data.
Is there any alternative or better approach to group data?
ID
Group
8015
A
8020
A
8229
A
8304
B
8389
B
8391
C
8414
D
8421
A
8469
A
8572
A
8619
F
8660
F
8663
J
9102
A
9104
K
9120
A
I have set up a conditional column in Power Query to achieve the results seeing in “Group” column by saying if ID is 8304 then Group B, if ID is 8660 then Group F
Instead of a conditional column, use a helper table to store these links.
You can add the information to your main table by joining the two tables.

How to make a existing column "unique" in PowerBI, so I can form a "one-to-many" relationsship?

I have 40 tables. One table has 20 rows, and one of the columns have 1385 distinct values.
I would like to use this in a relationship with another table.
TableName(1385 rows) Column:Name:(1385 distinct values)
But when I try to do this in Powerbi/Manage-Relations, it will only accept the option "Many-to-Many" relationship. It reports that none of the column are "Unique".
Well, the data in the column is unique. So how can I configure this column to be unique so I can use it in a "One-to-Many" relationship"?
Do I have to edit the DAX expression and put the "DISTINCT" keyword in the expression for that column? And How?
Now I have:
}, {"Columnname", Int64.Type}, {
what you can try is to perform remove duplicates in that table(i know its already contains distinct values but you can give it a try)... and/or just load the data again.
Best way would be when you group your data in the query editor. This way your table has only distinct values and you can create your relationship.
In the query designer under Home > Group By you can group after your column.
Example
Table:
Table (2):
Relationship (One to Many):
Result:
I hope this helps.

Work with matrix (I can't edit visualisation)

I have this table in Power BI, But I can't do another table.
How I can do this?
Now the values are grouped by date (different fields have information under one date, next the same fields are grouped by another date)
I want the values in the columns to be grouped by field (one field has date information next to it).
Edit1:
I can't set Date on the 2nd place in the grouping
Because date is column, traffic,orders,rev,costs- are values
You need to set Date on the 2nd place in the grouping, after a field containing traffic, orders, etc.
EDIT:
You need to unpivot these columns first, for example, in PowerQuery. Use Edit Query. This results in transforming your 4 columns to 2: Attribute and Value. Attribute will be your first grouping parameter. 2nd will be Date. Value column goes to values.
If you need your source query somewhere else, you may create new query for this very report only. It is done by first right-clicking original one and selecting Reference Query, and the doing any edits. This will keep original query intact.

Informatica : something like CDC without adding any column in target table

I have a source table named A in oracle.
Initially Table A is loaded(copied) into table B
next I operate DML on Table A like Insert , Delete , Update .
How do we reflect it in table B ?
without creating any extra column in target table.
Time stamp for the row is not available.
I have to compare the rows in source and target
eg : if a row is deleted in source then it should be deleted in target.
if a row is updated then update in target and if not available in source then insert it in the target .
Please help !!
Take A and B as source.
Do a full outer join using a joiner (or if both tables are in the same databse, you can join in Source Qualifier)
In a expression create a flag based on the following scenarios.
A key fields are null => flag='Delete',
B key fields are null => flag='Insert',
Both A and B key fields are present - Compare non-key fields of A and B, if any of the fields are not equal set flag to 'Update' else 'No Change'
Now you can send the records to target(B) after applying the appropriate function using Update Strategy
If you do not want to retain the operations done in target table (as no extra column is allowed), the fastest way would simply be -
1) Truncate B
2) Insert A into B

Auto incrementing a virtual column after a GROUP BY, ORDER BY query

I've made extensive research about auto-increment before posting this but couldn't find similar case:
I have a query pulling data from a main table, grouping by player_id and ordering by points desc, therefore creating a ranking output. My aim is to make the same query, once it finishes aggregating and sorting data, create a new column 'Rank' and auto increment it so it shows 1,2,3 etc since everything is already grouped by player and ordered by points DESC.
Thanks guys.
Example of the source table :
player_id-----------points-----
---1-------------------5----------
---1-------------------10---------
---1-------------------5---------
---2-------------------20---------
---2-------------------5---------
Desired output according to this example:
Rank------player_id-------score-----
----1----------2-----------25 POINTS ---------
----2----------1-----------20 POINTS ---------
EDIT
Rownum does the job well, no need for an auto increment virtual column! see Mutnowski's accepted answer below please.
Try this
SELECT #rownum:=#rownum+1 AS ‘rank’, Player_ID, Points FROM (SELECT Player_ID, SUM(Points) AS 'Points' FROM tblScores GROUP BY Player_ID ORDER BY Points DESC) AS foo, (SELECT #rownum:=0) AS foo2
I think you need to run a query to get your results without the rank, then run another query on first that selects all and adds the rank
Applying SUM to the Points column should produce the results you want.
SELECT #rownum:=#rownum+1 AS ‘rank‘,player_id, SUM(points)
FROM scores
GROUP BY player_id
ORDER BY SUM(points) DESC;