Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

Sam Keith

I've a customer table and purchases table, need to show cname, cid with max(customer_visits) from customer table and sum of total_purchases by customer in purchases table. I'm doing something like this

select p.cid, c.cname, sum(p.total_price)
from customers c where exists
(select max(visits_made) from customers having visits_made=max(visits_made)
and cid=p.cid)
inner join purchases p on p.cid=c.cid
group by p.cid,c.cname

and

select p.cid, c.cname, sum(p.total_price)
(select max(visits_made) from customers c where c.cid=p.cid) 
from purchases p
inner join customers c on c.cid=p.cid
group by p.cid,c.cname

What's going wrong with these queries?

Found the solution, had to include where clause after inner join :D

Gordon Linoff

I think this is just an aggregation query:

select p.cid, c.cname, sum(p.total_price) as total_price,
       max(visits_made) as visits_made
from purchases p inner join
     customers c
     on c.cid = p.cid
group by p.cid, c.cname;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Display the details from one table and count from another table

From Dev

Display records from one table which is not in another table by multiple columns

From Dev

How to display mysql count results from one table in different columns?

From Dev

insert statement one column from another table rest of the columns is values

From Dev

How to display all data in 3 columns from a table where the 4th column contains the same ID as another table?

From Dev

Mysql group by and having & sum of one column from table one

From Dev

MySQL join / sum value from one table colum with a count values from another

From Dev

Inputing column values in one table from another related table

From Dev

Inputing column values in one table from another related table

From Dev

Join column from a table with concat columns from another one

From Dev

Can I use a computed column in a table to fetch data from another table columns

From Dev

CakePHP 3: How to display sum of values in column from finder results

From Dev

Join multiple values from one column, selected from another table

From Dev

How to create a computed column that references another column from another table?

From Dev

How display 3 words from difference table columns to a single listview column using vb.net?

From Dev

mysql insert unique values from one column to a column of another table

From Dev

rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

From Dev

rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

From Dev

Match 2 columns from the same table to the same single column in another table (total of 3 joins)

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

Mysql from sum one table column subtract sum of another table column

From Dev

Getting table rows in order of sum of count of two another columns from another table

From Dev

Display only one time a value from column's table

From Dev

Display data from single table where column values match

From Dev

Display data from table with specific column values via php

From Dev

How to create table having one column as sum of other columns?

From Dev

not able to display Max salary from table (test)

From Dev

Procedure to insert data from one column into two columns in another table

From Dev

display result from 2 table

Related Related

  1. 1

    Display the details from one table and count from another table

  2. 2

    Display records from one table which is not in another table by multiple columns

  3. 3

    How to display mysql count results from one table in different columns?

  4. 4

    insert statement one column from another table rest of the columns is values

  5. 5

    How to display all data in 3 columns from a table where the 4th column contains the same ID as another table?

  6. 6

    Mysql group by and having & sum of one column from table one

  7. 7

    MySQL join / sum value from one table colum with a count values from another

  8. 8

    Inputing column values in one table from another related table

  9. 9

    Inputing column values in one table from another related table

  10. 10

    Join column from a table with concat columns from another one

  11. 11

    Can I use a computed column in a table to fetch data from another table columns

  12. 12

    CakePHP 3: How to display sum of values in column from finder results

  13. 13

    Join multiple values from one column, selected from another table

  14. 14

    How to create a computed column that references another column from another table?

  15. 15

    How display 3 words from difference table columns to a single listview column using vb.net?

  16. 16

    mysql insert unique values from one column to a column of another table

  17. 17

    rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

  18. 18

    rails 3.2 / activerecord - how to display 30 values from AR in a table with 3 columns?

  19. 19

    Match 2 columns from the same table to the same single column in another table (total of 3 joins)

  20. 20

    SQL Query to compare two columns with one column from another table (and get two values)

  21. 21

    Mysql from sum one table column subtract sum of another table column

  22. 22

    Getting table rows in order of sum of count of two another columns from another table

  23. 23

    Display only one time a value from column's table

  24. 24

    Display data from single table where column values match

  25. 25

    Display data from table with specific column values via php

  26. 26

    How to create table having one column as sum of other columns?

  27. 27

    not able to display Max salary from table (test)

  28. 28

    Procedure to insert data from one column into two columns in another table

  29. 29

    display result from 2 table

HotTag

Archive