Postgres : get min and max rows count in many to many relation table

Darshan Patel

I have mapping table for RFQ(request for quotation) and Vendor's bid amount.

rfq_vendor_mapping :

id  rfq_id(FK)  vendor_id(FK)   amount
---------------------------------------

 1      1           1            100
 2      1           2            50
 3      2           1            200
 4      2           3            300
 5      2           2            40
 6      3           4            70
 7      3           1            90
 8      3           2            250
 9      4           3            30
 10     5           1            500

In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.

Expected Output :

vendor_id   min_bid_count   max_bid_count
-----------------------------------------
    1           1               2
    2           2               1
    3           1               2
    4           1               0

http://sqlfiddle.com/#!15/60198/1

Erwin Brandstetter

Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:

SELECT vendor_id
     , count(min_bid OR NULL) AS min_bid_count
     , count(max_bid OR NULL) AS max_bid_count
FROM  (
   SELECT vendor_id
        , amount = min(amount) OVER w AS min_bid
        , amount = max(amount) OVER w AS max_bid
   FROM   rfq_vendor_mapping
   WINDOW w AS (PARTITION BY rfq_id)
   ) sub
GROUP  BY 1
ORDER  BY 1;

SQL Fiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count(), max(),min() fuctions definition with many selects

From Dev

Find missing rows from table through a many to many relation

From Dev

Many to Many relation with join table

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

Get Max(id) from one to many table

From Dev

How to have in many-to-one relation dictionary table with distinct rows?

From Dev

Updating table from temp table with many rows in postgres

From Dev

Postgres select from one to many table to single table rows

From Dev

SQL join on junction table with many to many relation

From Dev

JPA many to many relation not inserting into generated table

From Dev

Is it okay to "hook" into a many-to-many relation table?

From Dev

FuelPHP - Many to Many and fields in relation table?

From Dev

How to join only one row from one-to-many relation where value is min or max?

From Dev

How to get the ids of many to many relation?

From Dev

Laravel get data from many to many relation

From Dev

Mysql Many to Many relation get data with 'and' and 'not' data

From Dev

How to get hive query for one to many relation with in a table

From Dev

Python sqlalchemy: How do I use the relation to get count data from many to many

From Dev

how can i get data from table with relation many to many in yii2?

From Dev

Rails counting rows with has_many relation

From Dev

Rails counting rows with has_many relation

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

Remove duplicate rows on many to many table (Mysql)

From Dev

Large db table with many rows or many columns

From Dev

Many to Many Eloquent Relation

From Dev

Laravel Many to Many relation

From Dev

Working with Many to Many relation

From Dev

Select for many to many relation

From Dev

Select on many to many relation

Related Related

  1. 1

    Count(), max(),min() fuctions definition with many selects

  2. 2

    Find missing rows from table through a many to many relation

  3. 3

    Many to Many relation with join table

  4. 4

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  5. 5

    Get Max(id) from one to many table

  6. 6

    How to have in many-to-one relation dictionary table with distinct rows?

  7. 7

    Updating table from temp table with many rows in postgres

  8. 8

    Postgres select from one to many table to single table rows

  9. 9

    SQL join on junction table with many to many relation

  10. 10

    JPA many to many relation not inserting into generated table

  11. 11

    Is it okay to "hook" into a many-to-many relation table?

  12. 12

    FuelPHP - Many to Many and fields in relation table?

  13. 13

    How to join only one row from one-to-many relation where value is min or max?

  14. 14

    How to get the ids of many to many relation?

  15. 15

    Laravel get data from many to many relation

  16. 16

    Mysql Many to Many relation get data with 'and' and 'not' data

  17. 17

    How to get hive query for one to many relation with in a table

  18. 18

    Python sqlalchemy: How do I use the relation to get count data from many to many

  19. 19

    how can i get data from table with relation many to many in yii2?

  20. 20

    Rails counting rows with has_many relation

  21. 21

    Rails counting rows with has_many relation

  22. 22

    Remove duplicate rows on many to many table (Mysql)

  23. 23

    Remove duplicate rows on many to many table (Mysql)

  24. 24

    Large db table with many rows or many columns

  25. 25

    Many to Many Eloquent Relation

  26. 26

    Laravel Many to Many relation

  27. 27

    Working with Many to Many relation

  28. 28

    Select for many to many relation

  29. 29

    Select on many to many relation

HotTag

Archive