Need Help on Inner Join two Query - sql

Hi Guys can you help me out to create this inner joint query.
the idea is I need to first get which is the top 3 highest keyword count then show that count of keyword per month (I need the Month Number only)
SELECT ReportRaw.Keyword, Format([DateApplying],'m') AS appdate, Count(ReportRaw.Keyword) AS CountOfKeyword1
FROM
(
SELECT TOP 3 Count(Keyword) AS CountOfKeyword,Keyword
FROM ReportRaw
GROUP BY Keyword
ORDER BY Count(Keyword) DESC;
) as T1
INNER JOIN ReportRaw
ON T1.Keyword = ReportRaw.Keyword
GROUP BY ReportRaw.Keyword, Format([DateApplying],'m') ;

It looks like that semicolon (;) after DESC. Assuming this is SQL Server
Made a tiny update to the query to get that month number:
SELECT #ReportRaw.Keyword, DATEPART(MONTH, [DateApplying]) AS appdate,
Count(#ReportRaw.Keyword) AS CountOfKeyword1
FROM
(
SELECT TOP 3 Count(Keyword) AS CountOfKeyword,Keyword
FROM #ReportRaw
GROUP BY Keyword
ORDER BY Count(Keyword) DESC
) as T1
INNER JOIN #ReportRaw
ON T1.Keyword = #ReportRaw.Keyword
GROUP BY #ReportRaw.Keyword, DATEPART(MONTH, [DateApplying]) ;

Related

SQL Server - Select multiple Columns Group By 1 Column

I have looked at many examples of selecting many columns but only grouping by 1 column and mine seem to give me duplicate results. See below I would like to select all the columns in my table but would like to only GROUP BY VehicleId. On the screenshot you'll see that the results are actually not grouped by VehicleId.
Any idea on what I am doing wrong?
Try 1:
SELECT
h.*,
TotalFines = 1,
TotalIncidents = 1,
TotalVehicleAllocations = 1,
TotalVehicleConditions = 1,
TotalMileageApplications = 1
FROM
(
SELECT h1.VehicleId FROM [dbo].[VehicleHistory] h1 GROUP BY h1.VehicleId
) GroupedList
INNER JOIN [dbo].[VehicleHistory] h ON GroupedList.VehicleId=h.VehicleId
ORDER BY
h.VehicleId;
Try 2:
SELECT t1.* FROM VehicleHistory t1
INNER JOIN
(
SELECT VehicleId FROM VehicleHistory GROUP BY VehicleId
) t2
ON t1.VehicleId=t2.VehicleId
Both queries produce the same results with duplicate rows for each VehicleId as per below:
Here's my expected results below. The results are a query produced by Entity Framework. But I would like to rewrite the linq query into T-SQL:
This is because you are gtouping in subquery (which would be same as SELECT DISTINCT h1.VehicleId FROM [dbo].[VehicleHistory] h1):
SELECT h1.VehicleId FROM [dbo].[VehicleHistory] h1 GROUP BY h1.VehicleId
and then you are joining in on that column, which can cause duplicates to occur (you have duplicate IDs in VehicleHistory).
All you need to do is:
SELECT VehicleId,
MAX(DateUpdated) DateUpdated, --or other aggregate function
--rest of your columns in appropriate aggreagte functions
FROM VehicleHistory
GROUP BY VehicleId

Ordering the results of Inner join

I currently have the following query:
select X.sellerID, x.Category, y.Award_Year, Y.Award
from Y
inner join
X on Y.Seller_ID=X.sellerID
Which give me this results:
How can I write a query to get Top 3 seller and the amount of awards recieved in 2017. Following is expected result:
I guess you want the top 3 sellers by descending order (i.e. most awards in top) and you could do a flashy pivot thing with filtering on year to get a more general query, but I suspect something like this one would be more suitable at this level..
The top 3 clause will differ between different DB Engines
MSSQL : TOP (nr) directly after SELECT
Oracle: FETCH FIRST nr ROWS ONLY last in statement
mysql: LIMIT nr last in statement
etc....
FOR MSSQL it would be something like
SELECT TOP (3) sellerID, Category, Award_Year, COUNT(Award_Year) Awards
from Y y
inner join
X x on y.Seller_ID=x.sellerID
WHERE Award_Year = 2017
GROUP BY x.sellerID, x.Category, y.Award_Year
Order By COUNT(Award_Year) DESC
Try this:
SELECT TOP(3) A.Id, A.category, count(A.Award) AS 'Award in 2017'
FROM (
SELECT X.sellerID AS ID, x.Category, y.Award_Year, Y.Award
FROM Y
INNER JOIN X on Y.Seller_ID=X.sellerID
) A
GROUP BY A.Id, A.category
ORDER BY count(A.Award) DESC
Depending on your actual DBMS version, you may need to amend it.

SQL query last transactions

You can't see on the image but I have many till_id numbers. (1,2,3,4,5).
What I want to do is just showing the last "trans_num" without repeating the till_id.
For example:
till_id trans_num
1 14211
2 14333
3 14555
A typical way to do this is:
select t.*
from t
where t.trans_date = (select max(t2.trans_date)
from t t2
where t2.till_id = t.till_id
);
select till_id ,trans_num, max(transdate) from tableA
group by till_id ,trans_num
Filter the columns you need in outer query or write inner query in where condition
You can use group by till_id in subselect
Select a.till_id a.trans_num
from your_table as a
where (a.trans_date. a.till_id) = (select max(b.trans_date), b-till_id
from your_table as b
group_by b.till_id
);

How to join three select queries which has one common column

I have three select queries as below which gives a respective output
select DATE_FORMAT(table1.value_date,'%b')as Month,
DATE_FORMAT(table1.value_date,'%Y') as Year,
table1.open as Open
from index_main as table1
join ( select min(`value_date`) `value_date`
from index_main
group by month(`value_date`), year( `value_date`)
) as table2 on table1.`value_date` = table2.`value_date`
Output columns - Month,year,open
select DATE_FORMAT(table1.value_date,'%b')as Month,
DATE_FORMAT(table1.value_date,'%Y') as Year,
table1.close as Open
from index_main as table1
join ( select max(`value_date`) `value_date`
from index_main group by month(`value_date`), year( `value_date`)
) as table2 on(table1.`value_date` = table2.`value_date`)
Output columns - Month,year,close
select DATE_FORMAT(table1.value_date,'%b')as Month,
DATE_FORMAT(table1.value_date,'%Y') as Year,
max(table1.high) as High
FROM `index_main` as table1
GROUP BY table1.month,table1.year
ORDER BY year(table1.value_date) desc, month(table1.value_date) desc
Output columns - Month,year,high,low
I want to join these three select queries based on the common columns i.e month & year.
My final result should have the following columns - month,year,open,close,high,low.
Try this.
First create 3 views, one with each query (vw1, vw2 and vw3). Then use a query like this:
SELECT vw1.Month, vw1.Year, Open, Close High FROM vw1 LEFT join vw2 on vw1.Year=vw2.Year and vw1.Month=vw2.Month LEFT JOIN vw3 on vw1.Year=vw3.Year and vw1.Month=vw3.Month
Hope this helps you.

Inner join and Group By error

I have two tabels :
tbl_album and tbl_gallery
How can I select the last image of the last three albums?
these are columns of my tables:
tbl_album: Id,al_name
tbl_gallery: Id,album_id,ga_pic_title,ga_file_name
I use this query:
select al.Id, al.al_name, ga.ga_file_name
from tbl_album al inner join tbl_gallery ga
on al.Id=ga.album_id order by Id desc
I receive an error when I used Group By clause:
select al.Id, al.al_name, ga.ga_file_name
from tbl_album al inner join tbl_gallery ga
on al.Id=ga.album_id group by al.al_name order by Id desc
Msg 8120, Level 16, State 1, Line 1 Column 'tbl_album.Id' is invalid
in the select list because it is not contained in either an aggregate
function or the GROUP BY clause.
I do not want to repeat al_name column.
Is there a better way?
You will have to include all your column in select list to group by like below (I think you are using SQL Server or Other but not MySQL). BTW, why do you need a group by in your posted query?
select al.Id, al.al_name, ga.ga_file_name
from tbl_album al
inner join tbl_gallery ga on al.Id=ga.album_id
group by al.Id, al.al_name, ga.ga_file_name
order by Id desc
Most databases support the row_number() function which really helps with what you want to do:
select id, al_name, ga_filename
from (select al.Id, al.al_name, ga.ga_file_name,
row_number() over (partition by al.id order by ga.id desc) as seqnum,
dense_rank() over (order by al.id desc) as seqnum_album
from tbl_album al inner join
tbl_gallery ga
on al.Id = ga.album_id
) t
where seqnum = 1 and seqnum_album <= 3;
Note that I used the window function dense_rank() to determine the last three albums. You can also do this with an order by and a clause the limits the number of rows. Unfortunately, the latter depends on the database, so it might be top 3, or limit 3, or fetch first 3 rows only, or even something else.