Google Spreadsheet When converting the GoogleFinance currency returned value only from the first line - spreadsheet

In Google Spreadsheet the column "B" three rows with prices in $, but when converting the currency returned value only from the first line, please tell me where the error?
=QUERY(importXML("https://www.globalpetrolprices.com/Azerbaijan/", "//*[#id='graphPageLeft']/table[1]//tr"),
"Select Col4
label Col4 ''
format Col4 '0.00'")
* GOOGLEFINANCE("currency:USDEUR")

I was advised this solution:
=ArrayFormula(TEXT(QUERY(ARRAY_CONSTRAIN(importXML("https://www.globalpetrolprices.com/Azerbaijan/", "//*[#id='graphPageLeft']/table[1]//tr"), 4, 4), "Select Col4 label Col4 ''") * GOOGLEFINANCE("currency:USDEUR"),"€ 0.00"))

Related

Splitting the string into columns to extract values using BigQuery

How can i split the string by a specific character and extract the value of each. The idea is that i need to extract each word between the line including the start/end of the string as this information represents something. Is there a regex pattern ? or a way to split the info into columns ?
Name
A|B|C|D|E|F|G
Name col1 col2 col3 col4 col5 col6 col7
A|B|C|D|E|F|G A B C D E F G
I am using BigQuery for this and couldn't find a way to get the info of all of those. I tried the regex code which only works for the case where we have A|B|C.
I have to compare each column value and then create conditions using case when
CODE:
select
regexp_extract(name, "\\w+\\S(x|y)") as c2, -- gives either x or y
left(regexp_substr(name, "\\w+\\S\\w+\\S\\w+"),1) as c1,
right(regexp_extract(name, "\\w+\\S\\w+\\S\\w+"),1) as c3
from Table
Consider below approach
select * from (
select *
from your_table, unnest(split(name, '|')) value with offset
)
pivot(any_value(value) as col for offset in (0,1,2,3,4,5,6))
if applied to dummy data as in your question - output is
This seems like a use case for SPLIT().
select split(name,"|")[safe_offset(0)] as c1, split(name,"|")[safe_offset(1)] as c2, ..
from table
see https://cloud.google.com/bigquery/docs/reference/standard-sql/string_functions#split
Added use of safe_offset instead of offset per Array index 74 is out of bounds (overflow) google big query

How to show values from a generated column (containing a complex formula) in PostgreSQL with date_trunc?

New to DB. Here's the scenario
Table x
id(serial) Col1(date/ts) Col2(numeric) Col3(numeric) Col4-Generated(numeric)
auto Date of record val1 val2 Preset formula using val1 & val2
how do I do
select date_trunc('month',sum(Col1)), sum(Col2), sum(Col3), Col4 Preset formula ....
without using any aggregate function for Col4 or writing the actual formula?

SQL Server: How to display a specific character based on position in a column

So I'm attempting to display a single character based on its position in a string from one column. Since this is grid data, there is a simple math to it. The grid has 24 rows 'A-X', and 44 columns.
So lets say I want to see the value in D9. I already know the expected value should be a 'A1', so that means the character length is '2'. If I do the math: (A + B + C = 3 x 44, + 9). That two-character value for D9 starts at the 141st position of that string in Col2. I attempted to use SUBSTRING with no success
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Query result displays data in Col1, but for Col2 its just blank. What am I missing?
Asked too soon. Figured out I had to remove the ' from the column name
SELECT
Col1 , SUBSTRING('Col2',141,2)
FROM Table1
Didn't work
SELECT
Col1 , SUBSTRING(Col2,141,2)
FROM Table1
Works

SQL - Combine two strings having the second string always right aligned

I'm currently trying to attain alignment via SQL queries when combining two columns.
my current data set looks something like:
Col1 Col2
usd US Dollar
cad Canadian Dollar
mxn Mexican Peso
And I want to combine col1 + col2, but no matter how many characters are in col2, the data that comes out of col1 needs to always be aligned to the right in the display.
The display is limited at 49 characters. Col2 has no specific limit as it's a description column, while col1 is a percentage column so it will have a maximum of 7 characters: 100.00%
Any help will be appreciated.
Thanks
If I understood your question well and assuming that col1 has maximal length of 7 characters while col2 length is undetermined, the following query should give you the results needed:
SELECT
ISNULL(myTable.col1, '')
+ (CASE WHEN LEN(ISNULL(myTable.col2, '')) < 49 - LEN(ISNULL(myTable.col1, ''))
THEN SPACE(49 - LEN(ISNULL(myTable.col1, '')) - LEN(ISNULL(myTable.col2, '')))
+ ISNULL(myTable.col2, '')
ELSE ' ' + LEFT(ISNULL(myTable.col2, ''),
49 - LEN(ISNULL(myTable.col1, '')) - 1)
END) AS cols_for_49chars_display
FROM
myTable
Since col1 is assumed to have max length of 7 characters, the CASE statement verifies the length of col2 for specific row.
If it's lower than 49 - LEN(col1), prepend col2 with 49 - LEN(col1) - LEN(col2) spaces using TSQL SPACE function (docs here) to right-align col2 and then add col2 itself.
In opposite case add one space after col1 and left-cut col2 to the length of 49 - LEN(ISNULL(myTable.col1, '')) - 1 when 1 being the length of the added single space character.
Example input data and results of the query:
Lets take as an example the data provided in your answer adding an extra row to show what will happen when col2 value is too long:
myTable contents:
col1 col2
------------------------------------------------------------------------------------------------
usd US Dollar
cad PCanadian Dollar
mxn Mexican Peso
dummy Very long description of a "dummy" currency that in fact doesn't exist in the real world
The result of the query on above rows would be:
cols_for_49chars_display
-------------------------------------------------
usd US Dollar
cad PCanadian Dollar
mxn Mexican Peso
dummy Very long description of a "dummy" currency
I hope it helps at least slightly.

SQL - Select only one value from multiple fields

Assume the following
col1 col2 col3 col4
------+----------+---------+---------
abc | | |
Yes, col1-4 have a space in them!
I want to select the column that is not a space . On row 1 it's col1, but on row 20 it may be col3, row 55 it may be col2, and so on. I need to return just that column.
There will always be only one column with a stored value within this range of four columns, I just need the one that actually has information in it.
This will be part of a greater query for a report, so regardless of what column abc is in I need that to look the same in every result case. Meaning I can't have the results be col1 for one case and col2 for the other because the report won't recognize. The column needs to always be called the same.
Yes, I know it's better to store NULLS versus spaces and why use four columns when only one can have data, why not use one. I've complained enougth about that so don't rip me a new one about bad db design because I AGREE.
SELECT LTRIM(RTRIM(col1 + col2 + col3 + col4))
Here we go... Why not add bad code to bad design. You could technically add all of the columns together and then trim them for leading/trailing spaces. I don't recommend it for performance on large scale deployments. Heck, I don't recommend this for any production script but I've been here before... Gotta do what you can to get it done.
Why not use a CASE statement, like
CASE WHEN col1 <> ' ' THEN col1
WHEN col2 <> ' ' THEN col2
WHEN col3 <> ' ' THEN col3
WHEN col4 <> ' ' THEN col4
END
You can do this:
case
when col1 != '' then col1
when col2 != '' then col2
...
end