So I have a table where I want the count of rows where the customer is McDonalds's and Date > 2019-06-30
I am trying
select "Customer",
Count("Customer")
FROM
public.master_environmental_data
WHERE "Customer" = 'McDonald''s' AND "Date" > '2021-06-30';
However I am getting this error:
column "master_environmental_data.Customer" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select "Customer",
^
SQL state: 42803
Character: 8
What is the correct query?
Should add a GROUP BY at the end of the query:
select "Customer",
Count("Customer")
FROM
public.master_environmental_data
WHERE "Customer" = 'McDonald''s' AND "Date" > '2021-06-30'
GROUP BY "Customer";
Related
So I am trying to create a query where I group a subquery in an array.
I am not an expert in Postgres especially not arrays in queries so I am trying to learn the more advanced stuffs.
I want to reach the following output:
Date | Jobs
-----------------------------------------------------------------------------
21/11/2022 | {{TestJob1,1500},{TestJob2,1100},{TestJob3,500}}
20/11/2022 | {{TestJob1,1300},{TestJob2,100},{TestJob3,500}}
19/11/2022 | {{TestJob1,1400},{TestJob2,1900}}
18/11/2022 | {{TestJob1,1200},{TestJob2,1700},{TestJob3,800},{TestJob4,500}}
I alread started experimenting and this is how far I got:
SELECT j."Start time"::date AS "Date",
(select array["Job name", count(*)::varchar] from amdw."Job runs" where "Start time"::date = j."Start time"::date group by "Job name") as "Jobs"
FROM amdw."Job runs" j
GROUP BY "Date"
ORDER BY "Date" DESC;
But with this query I get the following error:
SQL Error [42803]: ERROR: subquery uses ungrouped column "j.Start time" from outer query
Position: 135
Anybody an idea how to solve this query issue, and get to the output I want to get?
After adapting the query as #a_horse_with_no_name suggested:
SELECT j."Start time"::date AS "Date",
["Job name", count(*)::varchar] as "Jobs"
FROM amdw."Job runs" j
GROUP BY "Date", "Job name"
ORDER BY "Date" DESC;
Date | Jobs
-----------------------------------------------------------------------------
21/11/2022 | {{TestJob1,1500}
21/11/2022 | {TestJob2,1100}
21/11/2022 | {TestJob3,500}}
20/11/2022 | {{TestJob1,1300}
20/11/2022 | {TestJob2,100}
20/11/2022 | {TestJob3,500}}
So I now need to find a way to only show the date once and create a second dimension in the array...
You can aggregate this in two steps, first you create a temporary table that count each Job name by Start time and then you aggregate your arrays with ARRAY_AGG()
-- Creation of a temporary table
WITH agg_by_date_and_job_name AS (
SELECT
"Start time" AS "Date",
ARRAY ["Job name", count(*)::text] AS "Job"
FROM
amdw
GROUP BY
"Date",
"Job name"
)
SELECT
"Date",
ARRAY_AGG("Job") AS "Jobs"
FROM
agg_by_date_and_job_name
GROUP BY
"Date"
ORDER BY
"Date" DESC;
I have a problem to make a query for view data in postgresql. I want to view data with 2 condition :
where employeeId
and between daterange
Heres my Query:
Select *
from employee
where employeeId = 3
and date(created_at) = between '2022-08-29' and '2022-08-31'
I have run that query but show error:
Reason:
SQL Error [42601]: ERROR: syntax error at or near "date" Position: 1`
The type data of column created_at is timestamp.
My questions is: What is correct query for view data from that conditions?
Remove the = operator from your query, the BETWEEN does not require the =
Select * from employee where employeeId = 3 and date(created_at) between '2022-08-29' and '2022-08-31'
You can use arithmetic operation
Select *
from employee
where employeeId = 3
and created_at>='2022-08-29'
and created_at< '2022-09-01'
I have HTTP access log data in a Druid data source, and I want to see access patterns based on certain identifiers in the URL path. I wrote this query, and it works fine:
select regexp_extract(path, '/id/+([0-9]+)', 1) as "id",
sum("count") as "request_count"
from "access-logs"
where __time >= timestamp '2022-01-01'
group by 1
The only problem is that not all requests match that pattern, so I get one row in the result with an empty "id". I tried adding an extra condition in the where clause:
select regexp_extract(path, '/id/+([0-9]+)', 1) as "id",
sum("count") as "request_count"
from "access-logs"
where __time >= timestamp '2022-01-01' and "id" != ''
group by 1
But when I do that, I get this error message:
Error: Plan validation failed: org.apache.calcite.runtime.CalciteContextException:
From line 4, column 46 to line 4, column 49: Column 'id' not found in any table
So it doesn't let me reference the result of the expression in the where clause. I could of course just copy the entire regexp_extract expression, but is there a cleaner way of doing this?
Since id is an aggregated column, you would need a HAVING clause to filter on it.
I'm trying to make a query in PostgreSQL for include results from 2 (or more) tables using left join lateral, and I need to have one record for each record for table entidad_a_ (main table) and all the records from table entidad_b_ must be included in one field generated by array_agg. And in this array, I have to delete duplicate elements and I have to preserve order array in main table.
I need to execute this SQL query:
SELECT entidad_a_._id_ AS "_id", CASE WHEN count(entidadB) > 0 THEN array_agg(DISTINCT entidadB._id,ordinality order by ordinality)
ELSE NULL END AS "entidadB"
FROM entidad_a_ as entidad_a_, unnest(entidad_a_.entidad_b_) WITH ORDINALITY AS u(entidadb_id, ordinality)
LEFT JOIN LATERAL (
SELECT entidad_b_3._id_ AS "_id", entidad_b_3.label_ AS "label"
FROM entidad_b_ as entidad_b_3
WHERE entidad_b_3._id_ = entidadb_id
GROUP BY entidad_b_3._id_
LIMIT 1000 OFFSET 0
) entidadB ON TRUE
GROUP BY entidad_a_._id_
LIMIT 1000 OFFSET 0
But I have errors....
How can I have these results?
Edited:
My error is:
ERROR: function array_agg (integer, bigint) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 69
If the query is:
......array_agg (DISTINCT entidadB._id order by ordinality).....
The eror is:
ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list
SQL state: 42P10
Character: 110
My problem is the combination of array_agg, DISTINCT, and ORDER by
Solved!! I've created a postgres extension with a custom aggregation.
CREATE AGGREGATE array_agg_dist (anyelement)
(
sfunc = array_agg_transfn_dist,
stype = internal,
finalfunc = array_agg_finalfn_dist,
finalfunc_extra
);
Creating functions and c code for this custom functions.
if I remove the second join query, its works but otherwise not works !
ERROR: column reference "category" is ambiguous LINE 1: ...tion",
"prefix_product"."full_desc" as "description", "category"... ^
SELECT "prefix_product"."id" as "product_id",
"prefix_product"."title" as "name",
"prefix_product"."short_desc" as "briefDescription",
"prefix_product"."full_desc" as "description",
"category" as "productCategory"
FROM "prefix_product"
JOIN "prefix_category"
ON "prefix_product"."category"="prefix_category"."id"
JOIN "prefix_category_attribs"
ON "prefix_product"."category"="prefix_category"."parent"
WHERE "vendor" = '8'
I am using codeigniter3 with postgresql and in codeigniter, I have :
$this->db->select(['prefix_product.id as product_id', 'prefix_product.title as name', 'prefix_product.short_desc as briefDescription', 'prefix_product.full_desc as description','category as productCategory']);
$this->db->where('vendor',$vendorId);
$this->db->from($this->tblName);
$this->db->join('prefix_category','prefix_product.category=prefix_category.id');
$this->db->join('prefix_category_attribs','prefix_product.category=prefix_category.parent');
$queryResult =$this->db->get()->result();
thanks
It seems there are more than one tables having the same column name, i.e. category. So you need to prefix column category in the SELECT clause in order to explicitly specify the table the column belongs to :
"prefix_product"."category" as "productCategory"