select values from one table and avg of column from another?


I have Two Tables, from one table I want to select all column but from the second table I want to fetch only one column with average value using avg function with related id 

How can I achieve this using SQL query?


Asked by:- RameshwarLate
1
: 3085 At:- 9/5/2017 11:27:47 AM
sql sql-server avg-function







1 Answers
profileImage Answered by:- Vipin

You question doesn't have much description about the tables, so i can consider as there are two table Table1 and Table 2

For SQL Server Also I suggest you to use JOIN

SELECT Table1.Id, Name, AVG(Table2.Column) AS average
FROM Table1 t1 JOIN table2 t2 
      ON  t1.primarykey = t2.foriegnKey
GROUP By t1.ID, name
ORDER BY average DESC

In the above query we are joining two tables and matching values using Primary-Key/Foreign-Key, and selecting ID, name from Table1 while taking Average of SomeColumn of Table2 as average

Let me know if this helps you, also if you could explain your question in a better way there would be chances of getting better answer always(as other users will know your issue more properly), thanks :)

2
At:- 9/5/2017 11:51:37 AM
Thanks I need here load all data from first table like * in first table have all records related Hotels like ,id,name,etc, in second have the hotel id and rating column. i want show here all hotel details with average rating in control 0
By : RameshwarLate - at :- 9/5/2017 12:25:57 PM
According to your description and above answer by @vipin, it looks, he has provided the right answer, but you need to modify it according to your tables if you need exact code by answer, you may need to modify your questions and enter your table_name, table_columns, example data_image in the database, so other can understand your query better and provide your better you exact code, thanks 0
By : vikas_jk - at :- 9/5/2017 4:13:00 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use