Group data based on higest value of a partcular column - postgresql

I have two tables department and category.In my query result listing same department multiple times.I want to group departments based on category table priority column.
Eg Data:
id dept_name dept_category priority
1 Cardio category 2 2
2 Ortho category 3 3
3 Ortho category 1 1
4 ENT category 1 1
5 Ortho category 2 2
I wannt the reslt like:
id dept_name dept_category priority
1 Cardio category 2 2
3 Ortho category 1 1
4 ENT category 1 1
How can i fetch the resul like this.In my category table cantain priority 1,2,3 then 1 is highest priority.I need to group department based on highest priority.

We can use DISTINCT ON with Postgres:
SELECT DISTINCT ON (dept_name) *
FROM yourTable
ORDER BY dept_name, priority;

Related

Create Row Number based column and repeating for another

I want to create Row Number by Column based on each Product ID and back to 1 for another Product ID.
Pack ID
Product ID
Row Number
A001
P001
1
A002
P001
2
A003
P001
3
A004
P002
1
A005
P002
2
A006
P003
1
A007
P004
1
A008
P004
2
What query should I write ?
Use the ROW_NUMBER window function (see here and here).
select pack_id
, product_id
, row_number() over (partition by product_id
order by pack_id) "row number"
from test;
demo.

SELECT the most occuring value PostgreSQL

I have a table structure using postgres:
tran_id (int), cust_id(int), movie(varchar(255))
Approximately the data is like this:
tran_id cust_id movie
------- ------- --------
1 1 Zootopia
2 1 Zootopia
3 1 Saw
4 2 Fight Club
5 2 Fight Club
6 3 Avengers: End Game
7 3 Avengers: End Game
8 3 Avengers: End Game
9 3 Iron Man
10 4 Fight Club
11 4 Fight Club
12 4 Percy Jackson
13 4 Space Jam
14 5 Inception
15 5 Inception
I want to query so that it will show favorite movie for each customer. The expected output is below:
cust_id fav_movie
------- -------------------
1 Zootopia
2 Fight Club
3 Avengers: End Game
4 Fight Club
5 Inception
This is my current script:
SELECT cust_id, movie, MAX(fav_movie) FROM
(SELECT cust_id, movie, COUNT(movie) AS fav_movie FROM transaction
GROUP BY cust_id, movie
ORDER BY cust_id) A
GROUP BY cust_id, movie;
Using DISTINCT ON:
SELECT DISTINCT ON (cust_id) cust_id, movie AS fav_movie
FROM "transaction"
ORDER BY cust_id, COUNT(*) OVER (PARTITION BY cust_id, movie) DESC;
Demo
I would first get the counts for each movie and then output the one with the highest count per customer:
SELECT DISTINCT ON (cust_id)
cust_id, movie
FROM (SELECT cust_id, movie,
count(*) AS c
FROM transaction
GROUP BY cust_id, movie) AS grouped
ORDER BY cust_id, c DESC;

Replacing an array column of ids with an array of the records - SQL

Let's ignore the database design - https://stackoverflow.com/questions/41180638/sql-replacing-an-array-column-of-ids-with-an-array-of-the-records. I'm more just curious if this is possible. I have a table called orders with a column of products. In this column is an array with the ids of all the products that are in the order. Is it possible for me to query for the products and replace the products column with the result? This is what I have so far, but errors for having too many columns on the subquery.
SELECT o.*, ( SELECT p.* FROM products p WHERE p.id = ANY(o.products::int[])) products FROM orders o
Order's Table:
ID Products Status Total
1 [1,2,5] 1 4
2 [1,3,5] 1 5
3 [1,3,6] 1 5
Products Table:
ID Name Price
1 Blue shoe 1
2 Red Shoe 1
3 Green Shoes 2
4 Purple Shoe 1
5 Orange Shoes 2
6 Pink Shoes 2
Desired Output - selecting the first order.
{
ID:1,
Products:[ <Products Record-1>, <Products Record-2>, <Products Record-5>
],
Status: 1,
Total: 4
}

How to group by one column and summation of second?

tsql newbie here.
I have a table, similar to this one:
CarId CarName UserId RentedTimes CrashedTimes
`````````````````````````````````````````````````````````
1 Ferrari 1 2 0
2 DB9 1 5 0
3 Ferrari 2 4 0
4 Audi 3 1 0
5 Audi 1 1 0
Assuming the table is called 'Cars', I am trying to select total number of times each of cars were rented. According to the table above, Ferrari was rented total of 6 times, DB9 five times and Audi twice.
I tried doing this:
select CarName, SUM(RentedTimes)
from Cars
group by CarName, RentedTimes
order by RentedTimes desc
but, it is returning two rows of ferrari with 2,4 as rented times and so on..
How do I select all cars, and total times each were rented, please?
Thanks
Edited the query to include sort order, sorry.
select CarName, SUM(RentedTimes)
from Cars
group by CarName
ORDER BY SUM(RentedTimes) DESC
Try this way.. removed RentedTimes from group by

How to hide duplicates values in column

I have 2 tables in sql table_a and table_b this is the output: (1 to many relationship)
table_a table_b
id_no (pk) name id_no (fk) id_tabl(pk) order_code order_item
1 a 1 1 11 aple
1 a 1 2 12 orange
1 a 1 3 13 ice
2 b 2 4 12 orange
2 b 2 5 13 ice
3 c 3 6 13 ice
3 c 3 7 12 orange
3 c 3 8 11 aple
I want to display only 1 name with all his order_item.
How can I display it using iReport in the xml?
The output sample:
id_no name order_item
1 a aple
orange
ice
2 b orange
ice
3 c ice
orange
aple
Using only 2 (order_item) field pattern in every pages of my invoice
the other display will be displayed in invoice pages 2.
You should use Data Grouping.
You can read this article about data grouping.
Your can use the query like this:
SELECT table_a.id_no, table_a.name, table_b.order_item FROM table_a, table_b WHERE table_a.id_no=table_b.id_no ORDER BY table_a.name
Note: may be you need to add sort by table_a.id_no column.
You should create the iReport's group for the name field
Note: may be you need to create two groups - for id_no and name fields.
You can use the Group and Details bands for drawing the data row. Or you can put all textField elements to the Detail band. In this case you should set false value to the isPrintRepeatedValues textField's property (for id_no and name fields).