Simple WHERE clause breaking sqlite query - sql

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;

Related

Column not recognized in select statement

Im getting an error in the Select statement of this code.
SELECT Track.Name, Track.UnitPrice,
Count(*) AS Purchase_count,
Purchase_count * Track.UnitPrice AS Total_per_track
FROM Track
JOIN InvoiceLine ON Track.TrackId = InvoiceLine.TrackId
GROUP BY Track.Name
ORDER BY Total_per_track desc
LIMIT 10;
The Error returning is
Result: no such column: Purchase_count
At line 1:
SELECT Track.Name, Track.UnitPrice,
Count(*) AS Purchase_count,
Purchase_count * Track.UnitPrice AS Total_per_track
FROM Track
JOIN InvoiceLine
ON Track.TrackId = InvoiceLine.TrackId
GROUP BY Track.Name
ORDER BY Total_per_track desc
LIMIT 10;
You can't refer to an alias defined in the SELECT clause in the same clause (nor in the WHERE clause for example). You need to either repeat the original expression, or use a derived table (subquery or cte).
Here the expression is simple enough so repeating seems more relevant:
SELECT
t.Name,
t.UnitPrice,
COUNT(*) AS Purchase_count,
COUNT(*) * t.UnitPrice AS Total_per_track
FROM Track t
INNER JOIN InvoiceLine il ON t.TrackId = il.TrackId
GROUP BY t.TrackId, t.Name, t.UnitPrice
ORDER BY Total_per_track desc
LIMIT 10;
Notes:
I added TrackId and UnitPrice to the GROUP BY clause; TrackId is there to avoid wrongly grouping together two tracks that would have the same Name; UnitPrice appears in the SELECT clause and is not part of an aggregate function, so it is a good practice to have it in the GROUP BY clause too (although, it does seem to be functionally dependant on TrackId)
table aliases make the query shorter to read and write
In order to order by Total_per_track you need to use a table expression.
For example:
select *
from (
SELECT
Track.Name, Track.UnitPrice,
Count(*) AS Purchase_count,
Count(*) * Track.UnitPrice AS Total_per_track
FROM Track
JOIN InvoiceLine
ON Track.TrackId = InvoiceLine.TrackId
GROUP BY Track.Name
) x
ORDER BY Total_per_track desc
LIMIT 10

Hide column in sql query output

SELECT
MProduct.ProductCode, MProduct.ProductName, COUNT(*) AS Ranges
FROM
TProblem
FULL OUTER JOIN
MProduct ON TProblem.ProductCode = MProduct.ProductCode
GROUP BY
MProduct.ProductCode, MProduct.ProductName
ORDER BY
Ranges DESC
This is my query but I want to hide the Ranges column from output
To maintain the order of your results, just move the count from your select to the order by:
SELECT
MProduct.ProductCode, MProduct.ProductName
FROM
TProblem
FULL OUTER JOIN
MProduct ON TProblem.ProductCode = MProduct.ProductCode
GROUP BY
MProduct.ProductCode, MProduct.ProductName
ORDER BY
count(*) DESC
I understand where #marc_s is coming from. It looks like you are trying to get a list of DISTINCT rows
SELECT ProductCode, ProductName
FROM
(
SELECT TOP 100 PERCENT
MProduct.ProductCode, MProduct.ProductName, COUNT(*) AS Ranges
FROM
TProblem
FULL OUTER JOIN
MProduct ON TProblem.ProductCode = MProduct.ProductCode
GROUP BY
MProduct.ProductCode, MProduct.ProductName
ORDER BY
Ranges DESC
) AS DATA
Or alternatively
SELECT DISTINCT
MProduct.ProductCode, MProduct.ProductName
FROM
TProblem
FULL OUTER JOIN
MProduct ON TProblem.ProductCode = MProduct.ProductCode
MProduct.ProductCode, MProduct.ProductName

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

Select count and group by from two tables?

I have two tables Affaire(ID, Obj, ID_TA ...) and TypeAffaire(ID_TA, Label), i want to count number of each ID_TA in Affaire and show the label instead if ID_TA, I tried this but it doesnt work :
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA;
it says that label can't be found
You need to group by T.Label as well. All non-aggegated fields in the select clause, must also be in the group by clause.
Add T.Label in Group By
Try this
SELECT A.ID_TA
,T.Label
,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.TA
GROUP BY A.ID_TA,T.Label;
You need to Insert T.Label in the group by clause, and correct the ON T.ID_TA = A.TA in to ON T.ID_TA = A.ID_TA
SELECT A.ID_TA ,T.Label ,count(*) AS TotalAmount
FROM Affaire AS A
LEFT JOIN TypeAffaire AS T ON T.ID_TA = A.ID_TA
GROUP BY A.ID_TA, T.Label;

How do I join this sql query to another table?

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