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

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?

Related

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

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"))

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

Select from column where value starts with 1

i have a table T
id val1
a 199.87
b 166.56
c 100.67
d 233.45
e 177.23
I want to select those rows where val1 starts with 1
rows where val1 starts with 19.
Is there any way of doing this in SQL Server.
The data type of val1 is float.
1) I want to select those rows where val1 starts with 1
SELECT
*
FROM Table_1
WHERE val1 LIKE '1%'
2) rows where val1 starts with 19.
SELECT
*
FROM Table_1
WHERE val1 LIKE '19%'
You can use CONVERT or cast function in where clause to cast float to character data type . Below queries will generate your desired result.
select * from table1
where convert(varchar,val) like '1%';
select * from table1
where cast(val as char(10)) like '1%';
You can use anyone of the above.
Result
id val
-----------
a 199,87
b 166,56
c 100,67
e 177,23
To return val like 19, just replace 1 with 19.
You can check the demo here
If your data values will never exceed 200 it would be preferable not to convert them to another data type just to answer this query. Instead leave them as floats and assess them using a range comparison
SELECT * FROM t WHERE val1 >= 100 AND val1 < 200
Similarly for 19x.xx values, make it >= 190
Avoid converting data wherever possible; it adds overhead which affects query performance, and also means that indexes cannot be used - this can massively impact the performance of a query
You can simply use LIKE % conditioning when filtering results you selct from the table and so it goes like this.
SELECT * FROM TABLE_NAME WHERE STR(VAL1, 10, 5) LIKE '19%'
This works for me whenever I select a row containing a specific char or number of a column containing value.
EDIT: Used STR() function to convert numeric data into character data.
Use like operator to get your results. No need to convert datatype of Val1.
rows where val1 starts with 1:
SELECT * FROM T WHERE val1 LIKE '1%';
rows where val1 starts with 19:
SELECT * FROM T WHERE val1 LIKE '19%';
This question already has many good answers, below answer is a different approach.
create table table1(
id char(10),
val float
);
insert into table1 values('a',199.87);
insert into table1 values('b',166.56);
insert into table1 values('c',100.67);
insert into table1 values('d',233.45);
insert into table1 values('e',177.23);
Query
select * from table1 where (CHARINDEX( '1',val, 1))=1;
select * from table1 where (CHARINDEX( '19',val, 1))=1;
output
id val
a 199,87
b 166,56
c 100,67
e 177,23
id val
a 199,87

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

how to create a very long INSERT sql INTO EXCEL

oracle : how to create a very long INSERT sql INTO EXCEL
prompt excel gives : your sql is very large , use concatenate function
plz guide me on how to do this
example
="insert into customers(20 columns one after another) values('" &B3 &"','" & C3 & "','"&D3&"'.........20th value);
With these values in your Excel table:
A1 = INSERT INTO mytable(id, col2, col3, col4, col5) VALUES ('
A2 = '','
And data in A4:E4, like this:
A4 = testID
B4 = value
C4 = value2
D4 = value3
E4 = value4
You can build another column like this:
G4 = =CONCATENATE($A$1,A4,$A$2,B4,$A$2,C4,$A$2,D4,$A$2,E4,"');")
Which evaluates to this:
INSERT INTO mytable(id, col2, col3, col4, col5) VALUES ('testID','value','value2','value3','value4');
Using the references makes adding additional columns easier in the future since you won't have to remember to "Fill Down" the full column of formula text after you make changes to it.
Use the Excel CONCATENATE function instead of the concatenation symbol &.
=CONCATENATE("insert into customers(20 columns one after another) values('", B3, "','", C3, "','", D3, "'.........20th value")