I have a table in postgres with 2 fields: they are columns of ids of users who have looked at some data, under two conditions:
viewee viewer
------ ------
93024 66994
93156 93151
93163 113671
137340 93161
92992 93161
93161 93135
93156 93024
And I want to group them by both viewee and viewer field, and count the number of occurrences, and return that count
from high to low:
id count
------ -----
93161 3
93156 2
93024 2
137340 1
66994 1
92992 1
93135 1
93151 1
93163 1
I have been running two queries, one for each column, and then combining the results in my JavaScript application code. My query for one field is...
SELECT "viewer",
COUNT("viewer")
FROM "public"."friend_currentfriend"
GROUP BY "viewer"
ORDER BY count DESC;
How would I rewrite this query to handle both fields at once?
You can combine to columns from the table into a single one by using union all then use group by as below:
select id ,count(*) Count from (
select viewee id from vv
union all
select viewer id from vv) t
group by id
order by count(*) desc
Results:
This is a good place to use a lateral join:
select v.viewx, count(*)
from t cross join lateral
(values (t.viewee), (t.viewer)) v(viewx)
group by v.viewx
order by count(*) desc;
You can try this :
SELECT a.ID,
SUM(a.Total) as Total
FROM (SELECT t.Viewee AS ID,
COUNT(t.Viewee) AS Total
FROM #Temp t
GROUP BY t.Viewee
UNION
SELECT t.Viewer AS ID,
COUNT(t.Viewer) AS Total
FROM #Temp t
GROUP BY t.Viewer
) a
GROUP BY a.ID
ORDER BY SUM(a.Total) DESC
Related
When using Array_agg, it returns the same values in different orders. I tried using distinct in a few places and it didn't work. I tried using an order before and after the array and it would fail or not properly exclude results.
I am trying to find all fields in the field column that share the same time and same ID and put them into an array.
Columns are Fieldname, ID, Time
select b.Field, count(*)
from (select Time, ID, array_agg(fieldname) as Field
from a
group by 1,2
order by 3) b
group by b.field
order by 1 desc
This produces duplicate results
For example I will have:
Field Name Count
Ghost,Mark 1234
Mark,Ghost 1234
I also tried this below where I add a subquery where I first order the fields alphabetically when grouping time and ID but it failed to execute. I think due to array_agg not being the root query?
select a.Field, count(*)
from
(select Time, ID, array_agg(fieldname) as field
from
(select Time, ID, fieldname
from a
group by 1,2
order by 3 desc) a
group by 1,2 ) b
group by 1
order by 2 desc
Create a query of unique values, but some of them have same ids but different dates. Just want the newest date. I am joining several tables, but do not know how to handle this
SELECT DISTINCT ap.id, MAX(ap.date)
FROM sometable;
I tried this code but no result.
I get these resulst:
id date
------------
1 10/31/18
1 10/15/18
2 11/05/17
2 11/04/17
But I want these results:
1 10/31/18
2 11/05/17
In case your query has other columns too that you want to show in result, you will have to resort to analytical function, in such case your query will look like following
select id, the_date /* ,other columns */ from (
select row_number() over (partition by id
order by some_date /* your date column */ desc ) ord,
id,
some_date the_date
/* ,other columns */
from <your_table>
) where ord = 1
;
You need group by
SELECT ap.id, MAX(ap.date)
from sometable ap
group by ap.id;
the aggregation function as min(), max() ,, count() need group by for return the related agreagated result
and for the query in you comment you should use
SELECT ap.id, MAX(ap.date)
from sometable ap
where ap.id in ( id1, id2, id3.... idns)
group by ap.id
I have this table called item:
| PERSON_id | ITEM_id |
|------------------|----------------|
|------CP2---------|-----A03--------|
|------CP2---------|-----A02--------|
|------HB3---------|-----A02--------|
|------BW4---------|-----A01--------|
I need an SQL statement that would output the person with the most Items. Not really sure where to start either.
I advice you to use inner query for this purpose. the inner query is going to include group by and order by statement. and outer query will select the first statement which has the most items.
SELECT * FROM
(
SELECT PERSON_ID, COUNT(*) FROM TABLE1
GROUP BY PERSON_ID
ORDER BY 2 DESC
)
WHERE ROWNUM = 1
here is the fiddler link : http://sqlfiddle.com/#!4/4c4228/5
Locating the maximum of an aggregated column requires more than a single calculation, so here you can use a "common table expression" (cte) to hold the result and then re-use that result in a where clause:
with cte as (
select
person_id
, count(item_id) count_items
from mytable
group by
person_id
)
select
*
from cte
where count_items = (select max(count_items) from cte)
Note, if more than one person shares the same maximum count; more than one row will be returned bu this query.
I have an sql table with the below columns
OrderNo, GroupNum, ShipMethod, TrackingNo
I want to find number of orders that have multiple 'ShipMethod' for same groupnum?
Sample records wourld be:
Order123 1 DHL
Order123 2 DHL1
Order123 2 Fedex
Then i need to get result stating 2 or if possible output as below:
OrderNumer GroupNum Count
---------- ------- -----
Order123 2 2 (Because 2 shipmethods)
Group by the columns you want to be unique, use count() to get each groups count and use having to limit the output to only the relevant groups
select ordernum, groupnum, count(*) as cnt
from your_table
group by ordernum, groupnum
having count(*) > 1
If I understand correctly:
select OrderNumer, groupnum, count(*)
from t
group by OrderNumer, groupnum
having count(*) > 1;
You may also want count(distinct shipmethod), if you want to count the distinct values rather than the number of rows.
I want to make a select query which groups rows based on a given column and then sorts by size of such groups.
Let's say we have this sample data:
id type
1 c
2 b
3 b
4 a
5 c
6 b
I want to obtain the following by grouping and sorting the column 'type' in a descending way:
id type
2 b
3 b
6 b
1 c
5 c
4 a
As of now I am only able to get the count of each group but that is not exactly what I need:
SELECT *, COUNT(type) AS typecount
FROM sampletable
GROUP BY type
ORDER BY typecount DESC, type ASC
id type count
2 b 3
1 c 2
4 a 1
Can anybody please give me a hand with this query?
Edit:
Made 'b' the biggest group to avoid coming to the same solution by using only SORT BY
You can't use a column alias in your GROUP BY; just repeat the expression:
SELECT type, COUNT(type) AS count
FROM sampletable
GROUP BY type
ORDER BY COUNT(*) DESC, type ASC
Note that I changed the SELECT clause - you can't use * in your SELECT either since expressions in the SELECT need to either be in the GROUP BY clause or an aggregation.
It may not be the best way, but it will give you what you want.
You work out the totals for each group and then join that "virtual" table to your original table by the determined counts.
SELECT *
FROM sampletable s1
INNER JOIN (SELECT count(type) AS iCount,type
FROM sampletable
GROUP BY type) s2 ON s2.type = s1.type
ORDER BY s2.iCount DESC, s1.type ASC
http://sqlfiddle.com/#!9/f6b0c4/6/0
You can't perform GROUP BY operation on COLUMN ALIAS.
The reason why you can't use ALIAS on the GROUP BY clause that is created on the same level of the SELECT statement is because the GROUP BY is executed before the SELECT clause in which the ALIAS is created.
This is the SQL Order of Operation:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
Try following query:
SELECT type, COUNT(type) AS count
FROM sampletable
GROUP BY type
ORDER BY COUNT(*) DESC, type ASC;
EDIT:-
SELECT id, type
FROM sampletable
ORDER BY type DESC, id ASC;