Skip to content

Instantly share code, notes, and snippets.

View gharriso's full-sized avatar

Guy Harrison gharriso

View GitHub Profile
@gharriso
gharriso / chatgpt.js
Created March 16, 2023 01:25
Chat GPT aggregation framework
db.sales.aggregate([
{
$group: {
_id: {
month: { $month: "$date" },
department: "$department"
},
sales: { $sum: "$amount" }
}
},
@gharriso
gharriso / chatgpt.sql
Created March 16, 2023 01:20
chatgpt SQL
WITH monthly_sales AS (
SELECT date_trunc('month', sale_date) AS month, department, SUM(sale_amount) AS total_sales
FROM sales
GROUP BY date_trunc('month', sale_date), department
), ranked_sales AS (
SELECT month, department, total_sales,
RANK() OVER (PARTITION BY month ORDER BY total_sales DESC) AS rank
FROM monthly_sales
), previous_month_sales AS (
SELECT month, department, total_sales,
agg01.push({
$setWindowFields: {
partitionBy: '$deviceData',
sortBy: {
timeStamp: 1
},
output: {
movingAverage: {
$avg: '$measurement',
window: {
SQL> SELECT * FROM PROVENDB.PROVENDBREQUESTS WHERE ID=:request_id;
ID REQUESTTYPE
---------- ------------
REQUESTJSON
----------------------------------------------------------------------------------------------------
STATUS STATUSDAT
------------ ---------
MESSAGES
----------------------------------------------------------------------------------------------------
SQL> set lines 100
SQL>
SQL> BEGIN
2 :request_id := PROVENDBORACLE.FVALIDATEREQUEST(
3 proofid => :proof_id
4 );
5 END;
6 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM PROVENDB.PROVENDBREQUESTS WHERE ID=:request_id;
ID REQUESTTYPE
---------- ------------
REQUESTJSON
-------------------------------------------------------------------------------------------------------------------------------------------------
STATUS STATUSDAT
------------ ---------
MESSAGES
-------------------------------------------------------------------------------------------------------------------------------------------------
SQL> set long 30000
SQL> var request_id NUMBER
SQL> var proof_id char
SQL> var proof clob
SQL> set pages 10000
SQL>
SQL> BEGIN
2 :request_id := PROVENDBORACLE.FANCHORREQUEST(
3 TABLENAME => 'PROVENDBDEMO.CONTRACTSTABLE',
4 COLUMNLIST => 'CONTRACTDATA,METADATA',
mongo> mongoTuning.aggregationExecutionStats(explainOut);
1 $CURSOR ( ms 3505 returned:1183307)
2 IXSCAN ( iotts_ix ms:85 keys:1183307 nReturned:1183307)
3 PROJECTION_COVERED ( ms:115 nReturned:1183307)
4 $GROUP ( ms 1224 returned:5)
5 $SORT ( ms 0 returned:5)
Totals: ms: 4736 keys: 1183307 Docs: 0
@gharriso
gharriso / mongoOut.js
Last active September 18, 2021 01:01
mongo> function stageTimes(explan) {
var prevMS=0;
explan.stages.forEach(stg => {
print(Object.keys(stg)[0], stg.executionTimeMillisEstimate + 0-prevMS);
prevMS=stg.executionTimeMillisEstimate;
});
}
mongol> stageTimes(explainOut);
$cursor 3505
var explainOut=db.iotNoTimeSeries.explain('executionStats').aggregate([
{ $match:{ "deviceData":{ $gt:5 } } } ,
{ $group:{ _id:{ "deviceData":"$deviceData" },
"measureMent-avg":{$avg:"$measureMent"}
} },
{ $sort:{ "measureMent-avg":-1 }},
]);