Skip to content
Advertisement

Regarding SQL Inner Join [closed]

The question is as follows :

My question is regarding SQL code for the following question.

Final query should return the vendor information along with the values from the table cb_vendorinformation.

You should combine the values of the two tables based on the GroupID column.

The final query should only print out the GroupID, CompanyName, and final count of all rows that are grouped into each company name under a column titled Count.

The output table should be then sorted by the Count column and then sorted by GroupID so that a higher number appears first.

This is the main table :

maintable

The output should look like this :

finaloutput

I could develop a code as follows but I am not getting the correct output :

SELECT m.GroupID, c.CompanyName, Count(*) as count
FROM maintable_KN01N m
  INNER JOIN cb_vendorinformation c  ON m.GroupID = c.GroupID

My output :

my output

Expected output :

expected output

Could someone please help me out here ?

I tried this code :

SELECT m.GroupID
     , c.CompanyName
     , Count(*) as count 
  FROM maintable_KN01N m 
  JOIN cb_vendorinformation c  
    ON m.GroupID = c.GroupID 
 GROUP 
    BY m.GroupID
     , c.CompanyName;

And was getting the output as follows :

Output2

Advertisement

Answer

Why don’t you just use ORDER BY as follows:

SELECT m.GroupID,c.CompanyName,Count(*) as count_ 
  FROM maintable_KN01N m 
 INNER JOIN cb_vendorinformation c ON m.GroupID=c.GroupID 
 GROUP BY m.GroupID,c.CompanyName 
 ORDER BY count_, m.GroupID desc;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement