I wanna subtract column i+1 from column i for i in all columns name except the first column. I did the following but I get error. could you plz see where is the problem?
average_monthly_trend.withColumn('diff'+str(i), (col(average_monthly_trend.columns[i+1])-col(average_monthly_trend.columns[i]) for i in range(1,len(average_monthly_trend.columns)-1)))
I get the error:
AssertionError: col should be Column
but I am using columns' name
Related
I am trying to find the difference between the Unix seconds and adding into the existing null column but the results are not added into the column. As I am new I can't figure it out.
INSERT INTO "Operation"(minutes)
select (departure_unix_seconds - arrival_unix_seconds)/60 As Difference
from public."Operation";
assuming you have a column in your table called "minutes" , and you want to update that column , here is the syntax:
update public."Operation"
set minutes = (departure_unix_seconds - arrival_unix_seconds)/60
however usually when a column value depends on other column(s) ,It's better to be implemented as "generated column":
alter table Operation
add column minutes generated always as (departure_unix_seconds - arrival_unix_seconds)/60 stored;
How to convert JSONB column into multiple column when I dont know the fixed keys inside JSONb column in postgreSQL.
For example, Imagine I have three rows with different set of keys inside columnJson column as below
rowID columnjson
row 1 {"a":21,"b":90}
row 2 {"a":46,"b":12, "c": 754}
row 3 {"a":19}
I want to fetch the columnjson as 3 columns like below
Can anyone help on this to achive it.
All columns of a query must be known before the query is started. So you can't have a query that returns a different number of columns each time you run it.
But given your sample data, the following returns your expected result:
select row_id,
column_json ->> 'a' as a,
column_json ->> 'b' as b,
column_json ->> 'c' as c
from the_table
I am trying to subtract 2 columns in a database table and call the column calc, and its giving me an error:
Invalid operator for data type. Operator equals subtract, type equals nvarchar.
Anyone shed some light on this? Thanks.
SELECT
,[old]
,[new]
, (new - old) as calc
FROM database
database:
what i want to show with calc column:
Try:
SELECT
,[old]
,[new]
, (numeric(new) - numeric(old)) as calc
FROM database
I need to allow my user to select a column name and its value from a parameter (or two params) to filter the result.
I have a text parameter with a few column names that are listed in my dataset
Column1, Column2, Column3. Each of those columns has only two values 1 and 0.
I would love some help in getting an idea how to filter my dataset based on the column name listed in the parameter and a selected value (1 or 0)
I assume it has to be related to a dynamic sql but, not sure how to incorporate that in either the WHERE clause or the actual dataset filter.
Thanks for any points guys!! :)
In the tablix you can use the filter section to set the criteria
col1 = param1.This will only select the rows that match the value of the parameter.
https://www.mssqltips.com/sqlservertip/2597/dataset-and-tablix-filtering-in-sql-server-reporting-services/
I want to sum and subtract two or more timestamp columns.
I'm using PostgreSQL and I have a structure as you can see:
I can't round the minutes or seconds, so I'm trying to extract the EPOCH and doing the operation after, but I always get an error because the first EXTRACT recognizes the column, but when I put the second EXTRACT in the same SQL command I get an error message saying that the second column does not exist.
I'll give you an example:
SELECT
EXAMPLE.PERSON_ID,
COALESCE(EXTRACT(EPOCH from EXAMPLE.LEFT_AT),0) +
COALESCE(EXTRACT(EPOCH from EXAMPLE.ARRIVED_AT),0) AS CREDIT
FROM
EXAMPLE
WHERE
EXAMPLE.PERSON_ID = 1;
In this example I would get an error like:
Column ARRIVED_AT does not exist
Why is this happening?
Could I sum/subtract time values from same row?
Is ARRIVED_AT a calculated value instead of a column? What did you run to get the query results image you posted showing those columns?
The following script does what you expect, so there's something about the structure of the table you're querying that isn't what you expect.
CREATE SCHEMA so46801016;
SET search_path=so46801016;
CREATE TABLE trips (
person_id serial primary key,
arrived_at time,
left_at time
);
INSERT INTO trips (arrived_at, left_at) VALUES
('14:30'::time, '19:30'::time)
, ('11:27'::time, '20:00'::time)
;
SELECT
t.person_id,
COALESCE(EXTRACT(EPOCH from t.left_at),0) +
COALESCE(EXTRACT(EPOCH from t.arrived_at),0) AS credit
FROM
trips t;
DROP SCHEMA so46801016 CASCADE;