Selecting data from a datatable in PowerShell - powershell

I'm trying to figure out how to get the data i need from a Datatable.
I'm new to datatables and cannot figure out by reading the documentation how to properly build the needed query.
I made a test script in PowerShell that i posted below:
# clear the screen:
CLS
# Create the Datatable:
$dt1 = New-Object System.Data.Datatable
# Name the datatable:
$dt1.TableName = "MyTable"
# Add collumns to the table:
[void]$dt1.Columns.Add("datacolumnone")
[void]$dt1.Columns.Add("datacolumntwo")
[void]$dt1.Columns.Add("datacolumnthree")
# Add data:
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 4", [string]"green")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 1", [string]"blue")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 4", [string]"yellow")
# Write the data to a xml and data file:
$dt1.WriteXmlSchema('.\CacheSchema.xsd')
$dt1.WriteXml(".\CacheData.xml")
##############################
# End of this script #
# Going on with the next one #
##############################
# Set the search criteria:
$column1 = ""
$column2 = "fact 1"
$column3 = "purple"
# build the query:
$qquery1 = "
datacolumnone like '%"+$column1+"%'
AND datacolumntwo like '%"+$column2+"%'
AND datacolumnthree like '%"+$column3+"%'
";
# Create the Datatable:
$dt2 = New-Object System.Data.Datatable
# Load the data:
[void]$dt2.ReadXmlSchema(".\CacheSchema.xsd")
[void]$dt2.ReadXml(".\CacheData.xml")
# Run the query:
$dt2.Select($qquery1)
Now what i try to achieve is:
Find all id's (datacolumnone) from the rows that have the value: "fact 1" in "datacolumntwo" and "purple" in "datacolumnthree"
Then further filter the id's where "datacolumnone" is in the results from the query above, "datacolumntwo" is "fact 4" and
"datacolumnthree" is "yellow"
Then when we have the filtered id's, show the rows that have in "datacolumnone" an id from the filtered query above and in
"datacolumntwo" the value "fact 2"
https://msdn.microsoft.com/en-us/library/system.data.datatable.select%28v=vs.110%29.aspx

If I am understanding the question correctly, querying a DataTable is the same as selecting rows that meet certain criteria using the Pipeline:
So, query one would be:
$results1 = $dt | Where-Object {$.datacolumntwo -eq "fact 1" -and $.datacolumnthree -eq "purple"} | Select Id
And query2 would be:
$results2 = $dt | Where-Object {$.datacolumnone -in $result1 -and $.datacolumntwo -eq "fact 4" -and $_.datacolumnthree -eq "yellow"} | Select Id
or you can combine the two queries if the intermediate results are not needed.

Related

Proper JSON construction to add different languages

I need to change this Query to be able to handle the different languages.
I'm already on my way but I want to be able to write out languages also.
with data (code, bez) as (
values
('01', 'Test 1'),
('02', 'Test 2'),
('03', 'Test 3'),
('04', 'Test 4')
), languages (lang) as (
values ('de'), ('fr'), ('it'), ('en')
)
select jsonb_build_object('cus_test', jsonb_object_agg('1'||code, l))
from (
select code,
jsonb_object_agg('bezeichnung_'||lang, bez) ||
jsonb_build_object('code', '1'||code) ||
jsonb_build_object('cus_immopaccode', code) ||
jsonb_build_object('cus_immopac_mandant_cd', 1) as l
from data
cross join languages
group by data.code
) x
;
At the moment the output is like this:
{
"cus_test": {
"101": {
"code": "101",
"bezeichnung_de": "Test 1",
"bezeichnung_en": "Test 1",
"bezeichnung_fr": "Test 1",
"bezeichnung_it": "Test 1",
"cus_immopaccode": "01",
"cus_immopac_mandant_cd": 1
},
"102": {
"code": "102",
"bezeichnung_de": "Test 2",
"bezeichnung_en": "Test 2",
"bezeichnung_fr": "Test 2",
"bezeichnung_it": "Test 2",
"cus_immopaccode": "02",
"cus_immopac_mandant_cd": 1
},
"103": {
"code": "103",
"bezeichnung_de": "Test 3",
"bezeichnung_en": "Test 3",
"bezeichnung_fr": "Test 3",
"bezeichnung_it": "Test 3",
"cus_immopaccode": "03",
"cus_immopac_mandant_cd": 1
},
"104": {
"code": "104",
"bezeichnung_de": "Test 4",
"bezeichnung_en": "Test 4",
"bezeichnung_fr": "Test 4",
"bezeichnung_it": "Test 4",
"cus_immopaccode": "04",
"cus_immopac_mandant_cd": 1
}
}
}
Now I want to add the language translations to it like this:
with data (code, bez_de, bez_fr, bez_it, bez_en) as (
values
('01','Pruefung 1', 'Test 1','la proova 1','Test 1'),
('02', 'Pruefung 1', 'Test 1','la proova 1','Test 1'),
('03', 'Pruefung 1', 'Test 1','la proova 1','Test 1'),
('04', 'Pruefung 1', 'Test 1','la proova 1','Test 1'))
But I don't how to implement it correctly.

It is not possible to make nested migrations to Prisma

I'm trying to create my first API on nets.js/prisma/postgresql
But I had difficulties describing the models in the prisma
I want that on request https://my-api/seasons the server gave a response of the form:
[
{
"Season1": {
"name": "Season 1",
"series": {
"seria1": {
"title": "text 1 1",
"url": "url 1 1",
"date": "date 1 1"
},
"seria2": {
"title": "text 1 2",
"url": "url 1 2",
"date": "date 1 2"
}
}
}
},
{
"Season2": {
"name": "Season 2",
"series": {
"seria1": {
"title": "text 2 1",
"url": "url 2 1",
"date": "date 2 1"
},
"seria2": {
"title": "text 2 2",
"url": "url 2 2",
"date": "date 2 2"
}
}
}
}
]
To make it clearer and clearer:
json data example
json data example
This is a simplified kind of migration that doesn't work for me
How can I make the same level of nesting using Prisma migration as I described above?
model Seasons {
id Int #id #default(autoincrement())
name Season #relation(fields: [seasonId], references: [id])
seasonId Int
}
model Season {
id Int #id #default(autoincrement())
name String #db.VarChar(40)
Seasons Seasons[]
}

How to inspect the state object stored via window.history.pushState() in Chrome devtools?

The state object is a JavaScript object which is associated with the
new history entry created by pushState().
https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method
Is it possible to inspect the content of this state object with Chrome devtools?
The state object is a property of the history object. You can access it in the Console with:
window.history.state
Example
Log each time you pop off the history stack
Method 1:
var back = window.history.back;
window.history.back = function() {
console.log("location: " + document.location + ", state: " +
JSON.stringify(window.history.state));
return back.apply(this, arguments);
}
history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.pushState({page: 3}, "title 3", "?page=3");
history.pushState({page: 4}, "title 4", "?page=4");
Method 2:
window.onpopstate = function(event) {
console.log("location: " + document.location + ", state: " +
JSON.stringify(event.state));
};
history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.pushState({page: 3}, "title 3", "?page=3");
history.pushState({page: 4}, "title 4", "?page=4");
This second one doesn't log the current state so you have to do that first.
It's not possible to peek through the history stack for security reasons.

MongoDB - SyntaxError: Unexpected token ILLEGAL

Am trying to run this simple query and am getting
"SyntaxError: Unexpected token ILLEGAL".
db.Bookdetail.insert({
"Subject":"Book Details",
"Details": [
{
"Book Id":"001",
"Book Name":"C# step by step",
"Book Description":"Programming book on C#"
},
{
"Book Id":"002",
"Book Name":"Head First Java",
"Book Description":"Programming Book on Java"
},
{
"Book Id":"003",
"Book Name":"Steve Jobs",
"Book Description":"biography of Jobs"
}
]
});
It seems that the : in "Book Id":"001" is invalid. Per your code, the :in "Book Id":"001" is full width letter, which is invalid character for JS
please use this code.
db.Bookdetail.insert({
"Subject":"Book Details",
"Details": [
{
"Book Id":"001",
"Book Name":"C# step by step",
"Book Description":"Programming book on C#"
},
{
"Book Id":"002",
"Book Name":"Head First Java",
"Book Description":"Programming Book on Java"
},
{
"Book Id":"003",
"Book Name":"Steve Jobs",
"Book Description":"biography of Jobs"
}
]
});
It seems you copied query form the source, it has invalid character.
Thanks

Concatenate list from JSON array

Let's say I have json in the database of items:
Row1:
{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}
Row2:
{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}
How do I get rows formatted like this (| is column seperator):
1 | Test Item, Test Item #2
2 | Test Item #3, Test Item #1
SELECT ID || '|' || ARRAY_TO_STRING( ARRAY_AGG( ITEMS ), ', ')
FROM
(
SELECT T.J->>'Id' AS ID, json_array_elements((T.J->'Items')::json)->>'Item' AS ITEMS
FROM
(
SELECT ('{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}')::json AS J
UNION all
SELECT ('{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}')::json AS J
) T
) T
GROUP BY ID