My question is the following ;
After a first query, I have a table with a single column of bigints, for example :
id
----
1
2
3
4
I would like to convert this column into a postgresql array, which would give - according to the example - {1,2,3,4}.
Any ideas about how to do that ?
Thank you for all your answers and have a nice day,
best regards
Use aggregation:
select array_agg(id)
from the_table;
If you need a specific sort order:
select array_agg(id order by id)
from the_table;
Related
I am stuck with creating a column that would show aggregated numbers for number of created contacts.
I would like to achieve what it is visible in column C
SELECT x.date_period, count(x.vid) contacts FROM
(
SELECT c.firstname as owner, c.vid, to_char(c.properties__createdate__value::date, 'IYYYIW') as date_period
FROM "hmy"."contacts" as c
) x
group by x.date_period
Any ideas?
Thanks!
I believe you can just replace count(x.vid) with sum(x.vid) OVER(ORDER BY properties__createdate__value).
Here is a SQLFiddle exercising the idea. I built a table that replicates your example table. If you have the first two columns of the example built, you should be able to apply that line to it to create the running total.
I have a problem to formulate an sql question in postgresql, hoping to get some help here
I have a table called visitor that contains an column called fk_employee_id, fk_employee_id contains different number between 1-10, example:
1,3,4,6,4,6,7,3,2,1,6,7,6
Now I want to find out which value that is the most frequent in this column (in this case 6) I have made an question that seem to solve my question;
SELECT fk_employee_id
FROM visitor
GROUP BY fk_employee_id
ORDER BY COUNT(fk_employee_id) DESC
LIMIT 1
but this question, doesn't get right if it is two values that are the most frequent one. So instead I try to write a question which contains max function but cant figure out how, anyone now how to do this?
We can use RANK here to slightly modify your current query:
WITH cte AS (
SELECT
fk_employee_id,
RANK() OVER (ORDER BY COUNT(*) DESC) rank
FROM visitor
GROUP BY fk_employee_id
)
SELECT fk_employee_id
FROM cte
WHERE rank = 1;
Demo
For use in SSRS multi-select dropdowns, I need to generate multiple DISTINCT lists of values.
This is easily done. Select DISTINCT Department from DimEmployee
In order to set an SSRS default value, you must have an ID field also in your data set.. this is not achieved by the above query
I need to assign an arbitrary uniqueID to each of these records from my distinct list
Any advice appreciated.
Final output would be:
Select * From dsDeptList
Results...
ID | DeptName
1 | DeptName1
2 | DeptName2
etc.....
Just include the EmployeeID column twice (with column aliases). Use it as the ID and the value. The DISTINCT guarantees it's unique in the resultset.
Please try below code:
CREATE TABLE #Test(EmpName VARCHAR(10));
INSERT #Test VALUES('A'),('A'),('B'),('C'),('C');
SELECT DISTINCT DENSE_RANK() OVER (ORDER BY EmpName) ID,EmpName
FROM #Test;
DROP TABLE #Test;
I hope this will help.
I think GROUP BY should solve your problem
SELECT EmployeeID,EmpName FROM YourTable
GROUP BY EmployeeID,EmpName
You'll definitely avoid dublications
I can't do:
>>> session.query(
func.count(distinct(Hit.ip_address, Hit.user_agent)).first()
TypeError: distinct() takes exactly 1 argument (2 given)
I can do:
session.query(
func.count(distinct(func.concat(Hit.ip_address, Hit.user_agent))).first()
Which is fine (count of unique users in a 'pageload' db table).
This isn't correct in the general case, e.g. will give a count of 1 instead of 2 for the following table:
col_a | col_b
----------------
xx | yy
xxy | y
Is there any way to generate the following SQL (which is valid in postgresql at least)?
SELECT count(distinct (col_a, col_b)) FROM my_table;
distinct() accepts more than one argument when appended to the query object:
session.query(Hit).distinct(Hit.ip_address, Hit.user_agent).count()
It should generate something like:
SELECT count(*) AS count_1
FROM (SELECT DISTINCT ON (hit.ip_address, hit.user_agent)
hit.ip_address AS hit_ip_address, hit.user_agent AS hit_user_agent
FROM hit) AS anon_1
which is even a bit closer to what you wanted.
The exact query can be produced using the tuple_() construct:
session.query(
func.count(distinct(tuple_(Hit.ip_address, Hit.user_agent)))).scalar()
Looks like sqlalchemy distinct() accepts only one column or expression.
Another way around is to use group_by and count. This should be more efficient than using concat of two columns - with group by database would be able to use indexes if they do exist:
session.query(Hit.ip_address, Hit.user_agent).\
group_by(Hit.ip_address, Hit.user_agent).count()
Generated query would still look different from what you asked about:
SELECT count(*) AS count_1
FROM (SELECT hittable.user_agent AS hittableuser_agent, hittable.ip_address AS sometable_column2
FROM hittable GROUP BY hittable.user_agent, hittable.ip_address) AS anon_1
You can add some variables or characters in concat function in order to make it distinct. Taking your example as reference it should be:
session.query(
func.count(distinct(func.concat(Hit.ip_address, "-", Hit.user_agent))).first()
I'm using postgresql as my database, I'm stuck with getting desired results with a query,
what I have in my table is something like following,
nid date_start date_end
1 20 25
1 20 25
2 23 26
2 23 26
what I want is following
nid date_start date_end
1 20 25
2 23 26
for that I used SELECT DISTINCT nid,date_start,date_end from table_1 but this result duplicate entries, how can I get distinct nid s with corresponding date_start and date_end?
can anyone help me with this?
Thanks a lot!
Based on your sample data and sample output, your query should work fine. I'll assume your sample input/output is not accurate.
If you want to get distinct values of a certain column, along with values from other corresponding columns, then you need to determine WHICH value from the corresponding columns to display (your question and query would otherwise not make sense). For this you need to use aggregates and group by. For example:
SELECT
nid,
MAX(date_start),
MAX(date_end)
FROM
table_1
GROUP BY
nid
That query should work unless you are selecting more columns.
Or maybe you are getting the same nid with a different start and/or end date
Try distinct on:
select distinct on (col1) col1, col2 from table;
DISTINCT can't result in duplicate entries - that's what it does... removed duplicates.
Is your posted data is incorrect? Exactly what are your data and output?