Dynamically unpivot each column in list, powerquery - powerquery

I'm trying to dynamically unpivot each column provided in a list.
(Table as table, ColumnNameList as list) as table =>
List.Accumulate(
List.Zip(ColumnNameList, {1..List.Count(ColumnNameList)}),
Table,
(state, column) => Table.Unpivot(state, column{0}, "Option" & column{1} & "Name", "Option" & column{1} & "Value")
)
This code doesn't work though, how can I do what I'm trying to do?

I ended up creating a function to provide a unique column name, somewhat like how the power query visual editor will automatically create names if an existing column already has one.
Haven't been able to test how it works once you get past more than one with the same name though.
(Table as table, ColumnName as text) =>
if Table.HasColumns(Table, ColumnName) then
let
NewName =
if Text.At(ColumnName, 3) <> "." then
ColumnName & ".01"
else
Text.Start(
ColumnName,
Text.Length(ColumnName) - 2
)
& Text.PadStart(
Text.From(
Number.FromText(Text.End(ColumnName, 2))
+ 1
),
2,
"0"
)
in
#FnTableNewColumnName(Table, NewName)
else
ColumnName
Then I changed the fuction I was working on, to call the naming function for both of the two extra parameters.
(Table as table, ColumnNameList as list) as table =>
List.Accumulate(
ColumnNameList,
Table,
(state, column) =>
Table.Unpivot(
state,
{ column },
FnTableNewColumnName(state, "attribute"),
FnTableNewColumnName(state, "value")
)
)

Related

Transform Sql to EF Core Linq query

I am trying to translate the following query from SQL to EF Core. I can easily just use a stored procedure (I already have the SQL), but am trying to learn how some of the linq queries work. Unfortunately this is not by any means an ideal database schema that I inherited and I don't have the time to convert it to something better.
DECLARE #userId INT = 3
SELECT *
FROM dbo.CardGamePairs
WHERE EXISTS (SELECT 1
FROM dbo.Users
WHERE Users.Id = CardGamePairs.player1Id
AND Users.userId = #userId)
UNION
SELECT *
FROM dbo.CardGamePairs
WHERE EXISTS (SELECT 1
FROM dbo.Users
WHERE Users.Id = TableB.player2Id
AND Users.userId = #userId)
So basically I have an id that can exist in one of two separate columns in table b and I don't know in which column it may be in, but I need all rows that have that ID in either column. The following is what I tried to make this work:
//Find data from table A where id matches (part of the subquery from above)
var userResults = _userRepository.GetAllAsQueryable(x => x.userId == userId).ToList();
//Get data from table b
var cardGamePairsResults = _cardGamePairsRepository.GetAllAsQueryable(x => userResults .Any(y => y.userId == x.player1Id || y.userId == x.player2Id));
When I run the code above I get this error message:
predicate: (y) => y.userId == x.player1Id || y.userId == x.player2Id))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
Any ideas on how I can make this work? (I tried changing the column and table names to something that would actually make sense, hopefully I didn't miss any spots and make it more confusing.)
Because you are already have user id use it to query both columns.
var userResults = _userRepository
.GetAllAsQueryable(x => x.userId == userId)
.ToList();
var cardGamePairsResults = _cardGamePairsRepository
.GetAllAsQueryable(x => x.player1Id == userId || x.player2Id == userId));

Compare two records and highlight the differences

I have a oracle apex report which fetches two rows every time.
I want to highlight all the columns where data is different in two rows.
So when user looks at the comparison report he don't have to go through all the columns to identify where data has been changed.
I have tried to look at the apex features and some javascript code but was unable to do so reliably.
You can have a look at sample report here:
https://apex.oracle.com/pls/apex/f?p=128616:8:109311280077805:::::
go to page "help me with comparison"
I want to highlight the benefit name column as data is different in benefit name column.
if your table has id "test" you can try call this function on page load compareRows(document.getElementById('test'));
the function body:
function compareRows(table) {
var row1,row2, rows = table.rows;
var cell1,cell2;
var rowText;
row1=rows[1];
row2=rows[2];
cell1=row1.cells;
cell2=row2.cells;
for (var i=0; i<cell1.length; i++) {
if(cell1[i].textContent != cell2[i].textContent){
cell1[i].style.backgroundColor = "red";
cell2[i].style.backgroundColor = "red";
}
}
}
You can use the analytic function "LAG" to reference the previous row in your resultset.
So one possible solution is to (1) select the value of the current row and the value of the previous row, (2) compare the 2 columns and set a flag, only in row 2 because that is where you want to highlight, (3) use highlighting in apex to indicate which columns have different values. See example sql below for an example.
-- create tables
create table so_dummy_data (
id number generated by default on null as identity
constraint so_dummy_data_id_pk primary key,
name varchar2(100) not null,
email varchar2(100) not null
)
;
-- load data
insert into so_dummy_data (
id,
name,
email
) values (
1,
'John Doe',
'john.doe#mail.com'
);
insert into so_dummy_data (
id,
name,
email
) values (
2,
'John Doe',
'john.x.doe#mail.com'
);
commit;
WITH old_and_new AS
(SELECT
id,
name,
LAG(name,1)OVER(
ORDER BY
name
)AS new_name,
email,
LAG(email,1)OVER(
ORDER BY
1
)AS new_email,
row_number() over (order by 1) rn
FROM
so_dummy_data
)
SELECT
name,
CASE
WHEN rn = 1 THEN 'N'
WHEN name = new_name THEN
'N'
ELSE
'Y'
END AS name_changed,
email,
CASE
WHEN rn = 1 THEN 'N'
WHEN email = new_email THEN
'N'
ELSE
'Y'
END AS email_changed
FROM
old_and_new;

select data from multiple tales using linq query

I have 2 tables I want to select all records from table one but in second table i want select all records that's on the id base that id is not related to table one .i want to do this in linq query .
any help
For what I could understand, you want to do a join between the two tables:
var query = database.FirstTable // starting point - the first table from your question
.Join(database.SecondTable, // the second table
first => first.ID, // Select the primary key (the first part of the "on" clause in an sql "join" statement)
second => second.first_ID, // Select the foreign key (the second part of the "on" clause)
(first, second) => new { First = first, Second = second }) // selection
.Where(result => result.first.ID == id); // where statement

Sorting issue with LINQ query and join using tables from different databases

I'm having trouble writing my LINQ query.
Here's the scenario:
I have 2 databases: A and B
In database A: I have a tableX which has the following fields: Employee ID, Name, Address, Phone, ..., Active
In database B: I have a tableY which has the following fields: Employee ID, Visible, Order
the number of records in table Y is less than or equal to the number of records in table X.
Basically, I need to extract the employee records from table X who have the attribute 'Visible' (in table Y) set to True and would like to sort them using the 'Order' attribute.
This is what I have so far:
ADataContext dbA = new ADataContext();
BDataContext dbB = new BDataContext();
//Get the list of records from tableY where 'Visbile' is set to True
var List = dbB.tableY
.Where(x => x.Visible == true).OrderBy(x => x.Order)
.ToList();
//Extract the list of employee IDs
IEnumerable<int> ids = List.Select(x => x.EmployeeID).Distinct();
var employees = dbA.tableX
.Where(x => ids.Contains(x.EmployeeID) && x.Active == true)
.ToList();
I'm able to get the correct list of employees, but cannot figure out how to apply the sorting order (present in tableY) on tableX
Currently, regardless of the order specified in tableY, the records returned from tableX are sorted as they were entered in the table (oldest to most recent).
Any ideas how I can fix my query.
Thanks,
I've re-written it all as a single query:
var employees =
from x in dbA.tableX
where x.Active
from y in dbB.tableY
where x.EmployeeID == y.EmployeeID
orderby y.Order
select x;

jdbc getMetaData for a specific query

In clojure/java jdbc, i have learned that using getMetaData i can return massive amounts of interesting information about a database connected to through jdbc. This can be filtered based on the catalog, schema, and table name.
(defn get-db-metadata
[db-spec ]
(with-connection (get-db-connection-map db-spec)
; get columns returs the following:
; TABLE_CAT String => table catalog (may be null)
; TABLE_SCHEM String => table schema (may be null)
; TABLE_NAME String => table name
; COLUMN_NAME String => column name
; DATA_TYPE int => SQL type from java.sql.Types
; TYPE_NAME String => Data source dependent type name, for a UDT the type name is fully qualified
; COLUMN_SIZE int => column size. For char or date types this is the maximum number of characters, for numeric or decimal types this is precision.
; BUFFER_LENGTH => not used.
; DECIMAL_DIGITS int => the number of fractional digits
; NUM_PREC_RADIX int => Radix (typically either 10 or 2)
; NULLABLE int => is NULL allowed.
; columnNoNulls => might not allow NULL values
; columnNullable => definitely allows NULL values
; columnNullableUnknown => nullability unknown
; REMARKS String => comment describing column (may be null)
; COLUMN_DEF String => default value (may be null)
; SQL_DATA_TYPE int => unused
; SQL_DATETIME_SUB int => unused
; CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column
; ORDINAL_POSITION int => index of column in table (starting at 1)
; IS_NULLABLE String => "NO" means column definitely does not allow NULL values; "YES" means the column might allow NULL values. An empty string means nobody knows.
; SCOPE_CATLOG String => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
; SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
; SCOPE_TABLE String => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)
; SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types (null if DATA_TYPE isn't DISTINCT or user-generated REF)
(into #{}
(map #(str (% :table_name) "." (% :column_name) "\n")
(resultset-seq (->
(connection)
(.getMetaData)
; Params in are catalog, schemapattern, tablenamepattern
;(.getColumns "stuff" "public" nil "%")
(.getColumns "db_catalog" "schema_name" "some_table" "%")
)
)
)
)
)
)
I'm interested in this information not for every table in the database, but rather for the result set returned by a specific query. My needs specifically at this point for doing this is to know the max length of a particular column that is retrieved, to print to the screen for instance in fixed-width format.
What i have considered so far (not ideal i'm sure):
Trying to parse the in-bound sql statement to figure out what tables
are being queried and then fetching the metadata for those tables
specifically. This would prove to be complicated with the results
of a select statement with functions, or common table expressions, etc.
I think it could get messy (and inaccurate) quickly.
What also may work would be to create a temporary view based on the
inbound query... then i could fetch the metadata on this view. This
however wouldn't work if i only had a read-only connection to the
database i was working with. often the case i believe for what i am trying to do.
Fetching the results, and then for each column returned finding the
max length of the values, and then creating my fixed-width grid
accordingly. This wouldn't be great if i was looking at large
result sets.....
Is there a better way to determine the types of everything i'm returning from my query? How do other programs do this? It seems like i should be able to get the metadata from the time i issue the query request somehow here:
(defn fetch-results
"Treat lazy result sets in whole for returning a database query"
[db-spec query]
(with-connection
(get-db-connection-map db-spec)
(with-query-results res query
; (get the medata here somehow for columns returned ????)
(doall res))
)
)
thanks in advance.
You can do like this in scala, may be this can help you out:
var stmt: PreparedStatement = null
var rs: ResultSetMetaData = null
try {
stmt = conn.prepareStatement(query)
rs = stmt.getMetaData()
} finally {
cleanup(stmt)
}
}

Resources