Incorrect extract epoch and current time - postgresql-9.5

Here table
column states is jsonb.
Example of states (json)
[
{
"dt": "2020-12-23T16:15:18.405+00:00",
"id": "order.new",
"data": {}
}
]
Current date is 23 Dec 2020 and current time is 20:26.
Now I want to show records with modified_at > 900 sec (15 min) and states->0->'id' = 'order.new'
I try this:
SELECT id, modified_at, states->0 as state FROM shop_order
WHERE (states ->0 #> '{"id":"order.new"}')
AND (extract(epoch from CURRENT_TIMESTAMP - modified_at)::integer > 900)
But result is empty

Related

Collapse elements of array of structs in BigQuery

I have an array of structs in BigQuery that looks like:
"categories": [
{
"value": "A",
"question": "Q1",
},
{
"value": "B",
"question": "Q2",
},
{
"value": "C",
"question": "Q3",
}
]
I'd like to collapse the values "A", "B" and "C" into a separate column, and the value for this particular row should be something like "A - B - C".
How can I do this with a query in BigQuery?
Consider below
select id,
( select string_agg(value, ' - ')
from t.questions_struct) values
from questions t
if applied to sample data in your question/answer -
with questions as (
SELECT 1 AS id,
[
STRUCT("A" as value, "Q1" as question),
STRUCT("B" as value, "Q2" as question),
STRUCT("C" as value, "Q3" as question)
] AS questions_struct
)
output is
Assuming this is an array of structs, you can use:
select (select q.value from unnest(ar) q where q.question = 'q1') as q1,
(select q.value from unnest(ar) q where q.question = 'q2') as q2,
(select q.value from unnest(ar) q where q.question = 'q3') as q3
from t;
I think it can be done with the following code:
with questions as (
SELECT 1 AS id,
[
STRUCT("A" as value, "Q1" as question),
STRUCT("B" as value, "Q2" as question),
STRUCT("C" as value, "Q3" as question)
] AS questions_struct
), unnested as (
select * from questions, unnest(questions_struct) as questions_struct
) select id, string_agg(value, ' - ') from unnested group by 1

datatables order by number doc and year of doc

I don't know how to implement a simple way to sort a column that show me number of doc with trailing year like:
COL 1 COL 2 COL 3 DOC. NR
------------------------------------
x x x 2/2020
------------------------------------
x x x 3/2020
------------------------------------
x x x 4/2021
------------------------------------
x x x 1/2022
------------------------------------
In my example i'd like to sort doc. nr col by asc or desc, based on number of doc (grouped by year).
I tried to set data-order with formula: numberdoc + year but it doesn't work cause, for example, 3 + 2020 is equal to 1 + 2022.... so this way is not correct. Any idea?
<td data-order="2020">1/2019</td>
Script:
$(document).ready( function () {
var table = $('#example').DataTable({
"aaSorting": [[ 1, "desc" ]],
"columnDefs": [
{
targets: [1],
data: {
_: "1.display",
sort: "1.#data-order",
type: "1.#data-order"
}
}
]
});
} );
My fiddle: http://live.datatables.net/jivekefo/1/edit
Expected (ASC) result:
1/2018
2/2018
3/2018
1/2019
2/2019
3/2019
4/2019
1/2020
...
Ok solved with this rule:
<td data-order="<?php echo (new Datetime($dateDoc))->format("Y") . str_pad(ltrim($numberDoc, "0"), 5, "0", STR_PAD_LEFT); /* example. 201900001 */ ?>">
http://live.datatables.net/jivekefo/2/edit

Nested dictionary to pandas df

My first question in stackoverflow!
I have a triple nested dictionary and I want to convert it to pandas df.
The dictionary has the following structure:
dictionary = {'CompanyA': {'Revenue': {date1 : $1}, {date2: $2}},...
{'ProfitLoss': {date1 : $0}, {date2: $1}}},
'CompanyB': {'Revenue': {date1 : $1}, {date2: $2}},...
{'ProfitLoss': {date1 : $0}, {date2: $1}}},
'CompanyC': {'Revenue': {date1 : $1}, {date2: $2}},...
{'ProfitLoss': {date1 : $0}, {date2: $1}}}}
So far I been able to construct a df using:
df = pd.DataFrame.from_dict(dictionary)
But the results its a df with values as dictionaries like this:
CompanyA CompanyB CompanyC
Revenue {date1:$0,..} {date1:$1,..} {date1:$0,..}
ProfitLoss{date1:$0,..} {date1:$0,..} {date1:$0,..}
I want the table to look like this:
CompanyA CompanyB CompanyC
Revenue Date1 $1 $1 $1
Date2 $2 $2 $2
ProfitLoss Date1 $0 $0 $0
Date2 $1 $1 $1
I had tried using pd.MultiIndex.from_dict (.from_product) and change the index, with no result. Any idea what to do next? Any hint will be appreciated!
I see you're new, but there may be an answer to a similar question, see this. Next time try looking for a similar question using keywords. For example, I found the one I linked by searching "pandas nested dict", and that's it, the first link was the SO post!
Anyway, you need to reshape your input dict. You want a dict structured like this:
{
'CompanyA': {
('Revenue', 'date1'): 1,
('ProfitLoss', 'date1'): 0,
}
...
}
I would do something like this:
import pandas as pd
data = {
'CompanyA': {
'Revenue': {
"date1": 1,
"date2": 2
},
'ProfitLoss': {
"date1": 0,
"date2": 1
}
},
'CompanyB': {
'Revenue': {
"date1": 4,
"date2": 5
},
'ProfitLoss': {
"date1": 2,
"date2": 3
}
}
}
# Reshape your data and pass it to `DataFrame.from_dict`
df = pd.DataFrame.from_dict({i: {(j, k): data[i][j][k]
for j in data[i] for k in data[i][j]}
for i in data}, orient="columns")
print(df)
Output:
CompanyA CompanyB
ProfitLoss date1 0 2
date2 1 3
Revenue date1 1 4
date2 2 5
EDIT
Using actual datetimes to respond to your comment:
import pandas as pd
import datetime as dt
date1 = dt.datetime.now()
date2 = date1 + dt.timedelta(days=365)
data = {
'CompanyA': {
'Revenue': {
date1: 1,
date2: 2
},
'ProfitLoss': {
date1: 0,
date2: 1
}
},
'CompanyB': {
'Revenue': {
date1: 4,
date2: 5
},
'ProfitLoss': {
date1: 2,
date2: 3
}
}
}
# Reshape your data and pass it to `DataFrame.from_dict`
df = pd.DataFrame.from_dict({i: {(j, k): data[i][j][k]
for j in data[i] for k in data[i][j]}
for i in data}, orient="columns")
print(df)
Output:
CompanyA CompanyB
ProfitLoss 2018-10-08 11:19:09.006375 0 2
2019-10-08 11:19:09.006375 1 3
Revenue 2018-10-08 11:19:09.006375 1 4
2019-10-08 11:19:09.006375 2 5

How can I achieve SQL Pivot statement from LINQ

I am looking to achieve below SQL statement from LINQ. I am not sure whether is it possible? Can someone advice me on this?
SELECT *
FROM (
SELECT CONVERT(VARCHAR, (DATEADD(WEEK, DATEDIFF(WEEK, 0, S.SampleDrawn), 0)), 101) [Date], [Range] =
CASE
WHEN ProbBacteremia >= 0 AND ProbBacteremia < 0.50 THEN 'Low'
WHEN ProbBacteremia >= 0.50 AND ProbBacteremia < 0.75 THEN 'Med'
ELSE 'High'
END
FROM Result.Calculation C INNER JOIN Data.SampleSet S ON C.SampleSetID = S.ID WHERE S.SampleDrawn >= DATEADD(WEEK,-1,GETDATE())) o
PIVOT
(
COUNT(o.[Range])
FOR [Range] IN (
[Low], [Med], [High])
) pt
ORDER BY [Date]
Result of the above query will be as below
Date Low Med High
09/04/2017 370 174 175
09/11/2017 764 352 389
09/18/2017 759 384 360
09/25/2017 765 385 404
10/02/2017 115 48 56
Note that, above date has grouped by week. Ie. 09/04 , 09/11, 09/18 etc. I did lot of research but i found only to group by Week Number.
This is as far as i could come up with LINQ which will return me the below result set.
data = (from a in context.Calculations
where a.SampleSet.SampleDrawn >= dtStart && (isDeptFilter || a.SampleSet.Department == location)
group a by new { Text = RangeProvider(a.ProbBacteremia * 100, riskCats), Date = a.SampleSet.SampleDrawn.Date } into groupedData
orderby groupedData.Key.Date ascending
select new { Value = groupedData.Count(), Text = groupedData.Key.Text, Date = groupedData.Key.Date.ToShortDateString() }).ToList();
public static string RangeProvider(int value)
{
if (value > 0 && value <= 25)
{ return "Low"; }
if (value > 25 && value <= 75)
{ return "Medium"; }
if (value > 75 && value <= 90)
{ return "High"; }
else
{ return "Very High"; }
}
Result dataset of the obver LINQ is
Date Text Value
09/04/2017 Low 65
09/04/2017 Med 80
09/04/2017 High 40
09/05/2017 Low 30
10/05/2017 Med 50
10/05/2017 High 44
Hope this explains what I'm trying to achieve. Please can someone help me with this?
Well as a work-around i have used the Entity Framework Core's "FromSQL" method to execute my stored procedure which take cares of all the GROUP BY's.
you can use this.
data = (from a in context.Calculations
where a.SampleSet.SampleDrawn >= dtStart && (isDeptFilter || a.SampleSet.Department == location)
group a by new { Text = RangeProvider(a.ProbBacteremia * 100, riskCats), Date = a.SampleSet.SampleDrawn.Date } into groupedData
orderby groupedData.Key.Date ascending
select new {
Date = groupedData.Key.Date.ToShortDateString() ,
Low = ( groupedData.Key.Text =="Low" )?groupedData.Count() : 0,
Medium = ( groupedData.Key.Text =="Medium" )?groupedData.Count() : 0,
High = ( groupedData.Key.Text =="High" )?groupedData.Count() : 0,
VeryHigh = ( groupedData.Key.Text =="Very High" )?groupedData.Count() : 0
}).ToList();

Rails group by time interval

How can I do this on sql or arel/activerecord, without "ruby"?
Event scheme:
integer "action" [0,1,2]
string "name"
float "price"
datetime "created_at"
datetime "updated_at"
Model method:
# interval maybe "2 hour", "1 hour", "15 minutes" etc.
def self.statistics(start_time, end_time, interval)
stats = Event.where(created_at: start_time..end_time).group_by do |e|
end_time - ((end_time - e.created_at).to_i / interval.to_f).floor * interval
end.map do |k, e|
gb = e.group_by {|e| e.action}
[k,
{
profit: e.reduce(0) {|m, e| m += e.price},
actions: (0..2).map {|v| gb.has_key?(v) ? gb[v].count : 0}
}
]
end.to_h
stats
end
Output:
{2015-10-12 11:00:00 +0300=>{:profit=>14685.0, :actions=>[86, 92, 105]},...}