How do I join this sql query to another table? - sql

I have the following SQL query and so far it works the way it should and gets the top 40 tag ids that I have stored in the tagmap table.
SELECT TOP 40
tbrm_TagMap.TagID,
Count(*)
FROM tbrm_TagMap
GROUP BY tbrm_TagMap.TagID
ORDER BY COUNT(tbrm_TagMap.TagID) DESC
I also want to join to the Tags table which contains the actual name of each TagID. Each attempt I make comes back with an error. How can I achieve this? I am using SQL 2008.

SELECT *
FROM (
SELECT TOP 40
tbrm_TagMap.TagID, COUNT(*) AS cnt
FROM tbrm_TagMap
GROUP BY
tbrm_TagMap.TagID
ORDER BY
COUNT(*) DESC
) q
JOIN Tags
ON Tags.id = q.TagID
ORDER BY
cnt DESC

My guess is that when you were joining tags, you weren't including it in the group by clause, which will always through an error in SQL Server. Every column not aggregated but returned needs to be in the group by.
Try something like this:
SELECT TOP 40
tbrm_TagMap.TagID,
t.Tag,
Count(*)
FROM
tbrm_TagMap
INNER JOIN tags t ON
tbrm_TagMap.TagID = t.TagID
GROUP BY
tbrm_TagMap.TagID,
t.Tag
ORDER BY 3 DESC

SELECT TOP 40
tbrm_TagMap.TagID, Tags.TagName Count(*)
FROM tbrm_TagMap INNER JOIN Tags ON tbrm_TagMap.TagID = Tags.TagID
GROUP BY tbrm_TagMap.TagID, Tags.TagName
ORDER BY COUNT(tbrm_TagMap.TagID) DESC

Try this..
SELECT top 40 tags.TagDescription, tbrm_TagMap.TagID, Count(*)
FROM tbrm_TagMap
INNER JOIN Tags
ON TagMap.TagID = Tags.TagId
GROUP BY tags.TagDescription, tbrm_TagMap.TagID
ORDER BY COUNT(tbrm_TagMap.TagID) DESC

Related

JOIN 2 tables ORDER BY SUM value

I have 2 tables: 1st is comment, 2nd is rating
SELECT * FROM comment_table a
INNER JOIN (SELECT comment_id, SUM(rating_value) AS total_rating FROM rating_table GROUP BY comment_id) b
ON a.comment_id = b.comment_id
ORDER BY b.total_rating DESC
I tried the above SQL but doesn't work!
Object is to display a list of comments order by rating points of each comments.
SELECT s.* FROM (
SELECT * FROM comment_table a
INNER JOIN (SELECT comment_id, SUM(rating_value) AS total_rating FROM rating_table GROUP BY comment_id) b
ON a.comment_id = b.comment_id
) AS s
ORDER BY s.total_rating DESC
Nest it inside an another select. It will then output the data in the correct order.

Simple WHERE clause breaking sqlite query

I have a working query here:
SELECT tlt.name AS theme, COUNT(*) AS nsets
FROM tlt
INNER JOIN sets
ON tlt.id = sets.theme_id
GROUP BY tlt.name
ORDER BY nsets DESC;
But when I add a WHERE clause sqlite throws a syntax error "near" the WHERE clause
SELECT tlt.name AS theme, COUNT(*) AS nsets
FROM tlt
INNER JOIN sets
ON tlt.id = sets.theme_id
GROUP BY tlt.name
WHERE nsets > 50
ORDER BY nsets DESC;
I'm confused why this simple WHERE clause breaks this query.
A WHERE clause must be before GROUP BY, not after. But you cannot use an alias or an aggregate function in a WHERE clause, so you need to use HAVING COUNT(*) > 50 where you currently have your WHERE clause.
SELECT tlt.name AS theme, COUNT(*) AS nsets
FROM tlt
INNER JOIN sets
ON tlt.id = sets.theme_id
GROUP BY tlt.name
HAVING COUNT(*) > 50
ORDER BY nsets DESC;

Return only the highest-valued row

I'm trying to find a solution to only returns the highest-valued row from a SQL query
I have a query that joins two tables together and then checks how many times the id matches within the different tables (within 'athelete' the id param is unique).
SELECT t.athlete_id, count(a.id) as 'Number of activities' FROM training_session t
INNER JOIN athlete a ON t.athlete_id = a.id
WHERE t.athlete_id = a.id
GROUP BY a.id
The following table is returned
athlete_id Number of activities
1 4
2 1
3 1
4 1
5 1
6 1
The issued problem is that I only want to return the row with the highest number of activities. According to the table above this should be
athlete_id = 1 since it has the greatest amount of activities.
I would appreciate some pointers on how I could improve my query to match these queries.
Use ORDER BY and LIMIT:
SELECT t.athlete_id, count(*) as `Number of activities`
FROM training_session t INNER JOIN
athlete a
ON t.athlete_id = a.id
GROUP BY t.athlete_id
ORDER BY COUNT(*) DESC
LIMIT 1;
I don't think a JOIN is needed for this query:
SELECT t.athlete_id, COUNT(*) as `Number of activities`
FROM training_session t
GROUP BY t.athlete_id
ORDER BY COUNT(*) DESC
LIMIT 1;
And if you want all rows in the event of ties, then this requires a bit more work. I would recommend ranking functions:
SELECT *
FROM (SELECT t.athlete_id, COUNT(*) as `Number of activities`,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM training_session t
GROUP BY t.athlete_id
) t
WHERE seqnum = 1;

how to get single row in select count(*) in following query postgresql

select count(*) from ordrer
inner join ordrelinjer on ordrelinjer.ordrenr = ordrer.ordrenr
group by ordrelinjer.varetekst
This query return 4 rows, but I want to return 4 in count(*), how to do so?
You are getting 4 row because of group by. If you need distinct group count, you can try subquery.
select count(*)
from (
select count(*)
from ordrer
inner join ordrelinjer on ordrelinjer.ordrenr=ordrer.ordrenr
group by ordrelinjer.varetekst
) t
It seem that you're looking for the distinct number of values for ordrelinjer.varetekst, which would be:
select count(distinct ordrelinjer.varetekst)
from ordrer
join ordrelinjer on ordrelinjer.ordrenr = ordrer.ordrenr;
invoke without group by
select count(*) from ordrer inner join ordrelinjer on ordrelinjer.ordrenr=ordrer.ordrenr

SQL Query for top 10 items from two relations

I'm struggling to right a SQL command to get the top 10 names from the following (using standard SQL, cant use TOP) for the following 2 relations:
Orders (customer_email, item_id, date)
Items(id, name, store, price)
Any advice on how to do this? I think I would need to group them, but then what do I do to get the top 10 groupings based on count?
select *
from (select x.*, row_number() over(order by num_orders desc) as rn
from (select i.name, count(*) as num_orders
from orders o
join items i
on o.item_id = i.id
group by i.name) x) x
where rn <= 10
SELECT
COUNT(*) count_per_item
, i.id
, i.name
FROM
Orders o
JOIN
Items i
ON (o.item_id = i.id)
GROUP BY
i.id
, i.name
ORDER BY
count_per_item DESC
LIMIT 10;