Run SUM(*) on Supabase - supabase-database

I'm starting with Supabase and would like to understand how I can pass the SUM() aggregator in a SELECT.
I noticed that for COUNT we use:
const { data, error, range, count } = supabase
.from('table')
.select('*', { count: 'exact' })
Is there anything similar to SUM that I haven't noticed?
My query is this:
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;

Your best bet is to put this into a PostgreSQL function and call it from Supabase using .rpc():

CREATE OR REPLACE FUNCTION get_my_sums()
RETURNS TABLE (
"GE_PRODUCTS" TEXT,
Quantity NUMBER,
Revenue NUMBER
) AS $$
DECLARE
var_r record;
BEGIN
RETURN QUERY
select
"GE_PRODUCTS",
sum("GE_QUANTITY") as Quantity,
sum("GE_SALEVALUE") as Revenue
from "GE_SELLS"
where "GE_ENTERPRISE" = 'G.E.'
and "DELETED" <> '*'
group by "GE_PRODUCTS"
order by Revenue desc
limit 3;
END; $$
LANGUAGE 'plpgsql';

Related

How to use string as column name in Bigquery

There is a scenario where I receive a string to the bigquery function and need to use it as a column name.
here is the function
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT column from WORK.temp WHERE rownumber = row_number)
);
When I call this function as select METADATA.GET_VALUE("TXCAMP10",149); I get the value as TXCAMP10 so we can say that it is processed as SELECT "TXCAMP10" from WORK.temp WHERE rownumber = 149 but I need it as SELECT TXCAMP10 from WORK.temp WHERE rownumber = 149 which will return some value from temp table lets suppose the value as A
so ultimately I need value A instead of column name i.e. TXCAMP10.
I tried using execute immediate like execute immediate("SELECT" || column || "from WORK.temp WHERE rownumber =" ||row_number) from this stack overflow post to resolve this issue but turns out I can't use it in a function.
How do I achieve required result?
I don't think you can achieve this result with the help of UDF in standard SQL in BigQuery.
But it is possible to do this with stored procedures in BigQuery and EXECUTE IMMEDIATE statement. Consider this code, which simulates the situation you have:
create or replace table d1.temp(
c1 int64,
c2 int64
);
insert into d1.temp values (1, 1), (2, 2);
create or replace procedure d1.GET_VALUE(column STRING, row_number int64, out result int64)
BEGIN
EXECUTE IMMEDIATE 'SELECT ' || column || ' from d1.temp where c2 = ?' into result using row_number;
END;
BEGIN
DECLARE result_c1 INT64;
call d1.GET_VALUE("c1", 1, result_c1);
select result_c1;
END;
After some research and trial-error methods, I used this workaround to solve this issue. It may not be the best solution when you have too many columns but it surely works.
CREATE OR REPLACE FUNCTION METADATA.GET_VALUE(column STRING, row_number int64) AS (
(SELECT case
when column_name = 'a' then a
when column_name = 'b' then b
when column_name = 'c' then c
when column_name = 'd' then d
when column_name = 'e' then e
end from WORK.temp WHERE rownumber = row_number)
);
And this gives the required results.
Point to note: the number of columns you use in the case statement should be of the same datatype else it won't work

How to remove null rows from MDX query results

How can I remove the null row from my MDX query results?
Here is the query I'm currently working with
select
non empty
{
[Measures].[Average Trips Per Day]
,[Measures].[Calories Burned]
,[Measures].[Carbon Offset]
,[Measures].[Median Distance]
,[Measures].[Median Duration]
,[Measures].[Rider Trips]
,[Measures].[Rides Per Bike Per Day]
,[Measures].[Total Distance]
,[Measures].[Total Riders]
,[Measures].[Total Trip Duration in Minutes]
,[Measures].[Total Members]
} on columns
,
non empty
{
(
[Promotion].[Promotion Code Name].children
)
} on rows
from [BCycle]
where ([Program].[Program Name].&[Madison B-cycle])
;results
This is not a null value however it is one of the children of [Promotion].[Promotion Code Name].Children.
You can exclude that particular value from children using the EXCEPT keyword of MDx.
Example query:
//This query shows the number of orders for all products,
//with the exception of Components, which are not
//sold.
SELECT
[Date].[Month of Year].Children ON COLUMNS,
Except
([Product].[Product Categories].[All].Children ,
{[Product].[Product Categories].[Components]}
) ON ROWS
FROM
[Adventure Works]
WHERE
([Measures].[Order Quantity])
Reference -> https://learn.microsoft.com/en-us/sql/mdx/except-mdx-function?view=sql-server-2017

How to validate dates in Hash format in MySQL or C++

I need to validate dates in Hash (SHA256) format in MySQL or C++.
For example: date1 < date2 or date1 > date2.
I have this query in MySQL:
SELECT SHA1(CURDATE()) -->'2017-09-06'
and:
SELECT SHA1('2017-09-06') --> '34152f3661d73490ac89b0fe15cb3170aac06bb8'
SELECT SHA1('2017-09-07') --> '0b10f03fb245a6486d6ab5b25a2f050bf87093a5'
But, if I use:
SELECT IF (SHA1('2017-09-06') <= SHA1('2017-09-07') ,'True','False') AS Test;
the result is False, therefore, incorrect!
Don't know why you'd do this, but here's a solution for you.
Here's some assumptions.
You were given two date hashes (two inputs)
You know exactly how the date hashes were created (EG: always in the format of YYYY-MM-DD)
You know the range of your target date (So you can reverse the hash back to the original date)
So in psudocode, you'd do something like this.
date getDateFromHash(string inputHash) {
date startDate = '1911-01-01';
date endDate = '2017-12-31';
for(date checkDate = startDate; checkDate < endDate; checkDate + 1 day) {
if (sha1(checkDate) = inputHash) { return checkDate }
}
return null;
}
date firstDate = getDateFromHash("0b10f03fb245a6486d6ab5b25a2f050bf87093a5"); // 2017-09-07
date secondDate = getDateFromHash("34152f3661d73490ac89b0fe15cb3170aac06bb8"); // 2017-09-06
if (firstDate > secondDate) {
return true;
} else {
return false;
} // compare using default date operators
But honestly speaking, like the comment says, there's no point of hashing date values. Hence why I'm only dropping a quich psudo code for you just to point you in the right direction, if you seriously want to tread down this path.

Doctrine2 Limit the rows to sum in a query

I have a query that return a sum, but I want to limit the number of rows to query, but is not working.
This is my query:
$qb = $this->createQueryBuilder('result')
->select('SUM(result.generalPoints) AS generalPoints, SUM(result.coefficient) AS coefficient')
->join('result.inscription', 'inscription', Join::WITH, 'inscription.user = :user')
->join('inscription.race', 'race')
->join('inscription.category', 'category', Join::WITH, 'category.generalRanking = true')
->join('race.event', 'event')
->join('event.competitionSeason', 'competitionSeason', Join::WITH, 'competitionSeason = :competitionSeason')
->orderBy('generalPoints', 'DESC')
->addOrderBy('coefficient', 'DESC')
->setParameter('competitionSeason', $competitionSeason)
->setParameter('user', $user);
if (isset($limit)) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getOneOrNullResult();
Some idea ?
Thanks
As sum is an aggregation function its result is one row, the limit statement is applied afterwards. You need create subquery producing multiple rows with generalPoints and coefficient, limit number of rows in this subquery and use the aggregation function in the wrapping query.

How to use date() function / return hydrated objects?

In my Entity Repository for Doctrine2, I have the following:
$date = new DateTime('NOW');
$date = $date->format('Y-m-d');
if ($region) {
$region_sql = " AND WHERE region LIKE ?3 ";
} else {
$region_sql = "";
}
$sql = "SELECT *, count(month) as count FROM (SELECT *, date(date_from, 'start of month', '+1 month', '-1 day') as month FROM manifestations WHERE date_to >= :date_to " . $region_sql . ") GROUP BY month";
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindValue(':date_to', $date);
if($region) {
$stmt->bindValue(3, sprintf('%%,%s,%%', $region));
}
$stmt->execute();
return $stmt->fetchAll();
But I need to change this so that it returns the objects hydrated instead of an array. I originally wanted to use DQL or queryBuilder but could not find a way to get the date() function to work.
With NativeQuery you can execute native SELECT SQL statements and map the results to Doctrine entities or any other result format supported by Doctrine.
What you want to do can be achieved using the ResultSetMappingBuilder.
ResultSetMappingBuilder is a convenience wrapper. It can generate the mappings for you based on Entities.
This is how I'd do it (I assume your query works, maybe you'll have to adjust it, as I will use a new alias):
Create the ResultSetMapping:
use Doctrine\ORM\Query\ResultSetMapping;// Don't forget this
$rsm = new ResultSetMappingBuilder($entityManager);// $entityManager points to your entity manager.
$rsm->addRootEntityFromClassMetadata('path/to/class/MyClass', 'a');// Notice the a, it's an alias that I'll later on use in the query.
$rsm->addScalarResult("count", "count");// column, alias
Prepare $region_sql part as you do in your code and add the a alias to whatever you want to map. a.* will be mapped to an object (notice the as a I use in the query):
$sql = "SELECT a.*, count(month) as count FROM (SELECT *, date(date_from, 'start of month', '+1 month', '-1 day') as month FROM manifestations WHERE date_to >= :date_to " . $region_sql . ") as a GROUP BY month";
Execute the query:
$query = $entityManager->createNativeQuery($sql, $rsm);
$query->setParameter('date_to', $date);
$result = $query->getResult();
This will give you an array of rows. Each of them will be a mixed array, $result[n][0] will contain the object and $result[n]["count"] the value of the count column of the query (name of the column is the same as the alias we set up in the $rsm) where n is the number of the row.