I have a problem in query.
Error is: {"Invalid column name 'TotalRecords'."}
I have a table named upload_news in this table there many records and I want retrieve data by country wise where in the distinct countries greater than 20 records.
select count(Distinct country) AS TotalRecords, country from upload_news where TotalRecords > 20";
You need to use Group By and Having:
select count(Distinct country) AS TotalRecords, country from upload_news
group by country
having count(Distinct country) > 20
Try like this
"SELECT *
FROM
(
select count(Distinct country) AS TotalRecords, country from upload_news
group by country
) T
where TotalRecords > 20";
because TotalRecords alias column, you can't access it directly.
Related
When I run both queries individually, they run correctly. But when I try to combine both result sets into one table using the UNION operator, it doesn't run and I get the error message : "Syntax error: Top N option is not allowed in a query connected by set operators."
select
top 1
city,
count(*)
from unicorns
where city is not null and industry = 'Edtech'
group by city
order by 2 desc
union
select
top 1
city,
count(*)
from unicorns
where city is not null and industry = 'Internet software & services'
group by city
order by 2 desc
I would appreciate any help,
Thanks.
Instead you can use window functions to achieve the same:
select
city,
count(*) ccount
from unicorns
where city is not null and industry = 'Edtech'
group by city
QUALIFY ROW_NUMBER() OVER (ORDER BY ccount DESC) = 1
union
select
city,
count(*) ccount
from unicorns
where city is not null and industry = 'Internet software & services'
group by city
QUALIFY ROW_NUMBER() OVER (ORDER BY ccount DESC) = 1
This way you aren't relying on ordering/top an interim result set (outside of partition ordering) and Teradata will be ok with it.
It's probably because the optimizer doesn't know if the 2nd ORDER BY is part of the top or the final ORDER BY of the UNION.
The common workaround for this type of error is to wrap the Selects in Derived Tables/CTEs:
select *
from
(
select
top 1
city,
count(*)
from unicorns
where city is not null and industry = 'Edtech'
group by city
order by 2 desc
) as dt
union
select *
from
(
select
top 1
city,
count(*)
from unicorns
where city is not null and industry = 'Internet software & services'
group by city
order by 2 desc
) as dt
My database structure contains columns: id, name, value, dealer. I want to retrieve row with lowest value for each dealer. I've been trying to mess up with MIN() and GROUP BY, still - no solution.
Solution1:
SELECT t1.* FROM your_table t1
JOIN (
SELECT MIN(value) AS min_value, dealer
FROM your_table
GROUP BY dealer
) AS t2 ON t1.dealer = t2.dealer AND t1.value = t2.min_value
Solution2 (recommended, much faster than solution1):
SELECT t1.* FROM your_table t1
LEFT JOIN your_table t2
ON t1.dealer = t2.dealer AND t1.value > t2.value
WHERE t2.value IS NULL
This problem is very famous, so there is a special page for this in Mysql's manual.
Check this: Rows Holding the Group-wise Maximum/Minimum of a Certain Column
select id,name,MIN(value) as pkvalue,dealer from TABLENAME
group by id,name,dealer;
here you group all rows by id,name,dealer and then you will get min value as pkvalue.
SELECT MIN(value),dealer FROM table_name GROUP BY dealer;
First you need to resolve the lowest value for each dealer, and then retrieve rows having that value for a particular dealer. I would do this that way:
SELECT a.*
FROM your_table AS a
JOIN (SELECT dealer,
Min(value) AS m
FROM your_table
GROUP BY dealer) AS b
ON ( a.dealer= b.dealer
AND a.value = b.m )
Try following:
SELECT dealer, MIN(value) as "Lowest value"
FROM value
GROUP BY dealer;
select id, name, value, dealer from yourtable where dealer
in(select min(dealer) from yourtable group by name, value)
These answers seem to miss the edge case of having multiple minimum values for a dealer and only wanting to return one row.
If you want to only want one value for each dealer you can use row_number partition - group - the table by dealer then order the data by value and id. we have to make the assumption that you will want the row with the smallest id.
SELECT ord_tbl.id,
ord_tbl.name,
ord_tbl.value,
ord_tbl.dealer
FROM (SELECT your_table.*,
ROW_NUMBER() over (PARTITION BY dealer ORDER BY value ASC, ID ASC)
FROM your_table
) AS ord_tbl
WHERE ord_tbl.ROW_NUMBER = 1;
Be careful though that value, id and dealer are indexed. If not this will do a full table scan and can get pretty slow...
I have a database with 58 customers(58 rows) with a column(country). How do I group up all the rows where a country only appears once and change its name to "Other" using a CASE Statement
SELECT
CASE
WHEN (SELECT COUNT(country) FROM customer GROUP BY country)=1 THEN 'Other'
ELSE country
END country
FROM customer
The problem is that when I run my query, all the countries become Other. I only want the countries that appear once to be called Other.
Here's how to do it.
SELECT
CASE WHEN COUNT(country) = 1 THEN 'Other' ELSE country END
FROM customer
GROUP BY country;
To update the actual table:
WITH map AS
(SELECT country,
CASE WHEN COUNT(country) = 1 THEN 'Other'
ELSE country END mapped
FROM customer
GROUP BY country)
UPDATE customer
SET country = (SELECT mapped FROM map WHERE map.country = customer.country);
With a subquery that counts how many times a country appears in the table:
SELECT
CASE
WHEN (SELECT COUNT(*) FROM customer WHERE country = c.country) = 1 THEN 'Other'
ELSE c.country
END country
FROM customer c
Or with a join of the table to the subquery that returns all the counter of all the countries:
SELECT
CASE
WHEN g.counter = 1 THEN 'Other'
ELSE c.country
END country
FROM customer c INNER JOIN (
SELECT country, COUNT(*) counter
FROM customer
GROUP BY country
) g ON g.country = c.country
If you want your table grouped by the result of this CASE statement use an alias to distinguish from the table's column country:
SELECT
CASE
WHEN (SELECT COUNT(*) FROM customer WHERE country = c.country) = 1 THEN 'Other'
ELSE c.country
END _country
FROM customer c
GROUP BY _country
select company,
case when ((select count() from ftmuser where active='0' group by company)>5) Then (select count() from ftmuser where active='0' group by company)
ELSE '0'
END
From ftmuser
I want to display only those records having count > 5 but above query fails and says ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Please guide me on this.
Thanks.
Try this:
select company, case when ((select count(*) from ftmuser where active='0'
group by company where company=ft.company)>5)
Then (select count() from ftmuser <br>where active='0'
group by company where company=ft.company )
ELSE '0' END
From ftmuser ft
Here is my data context:
Photos (ID, Title)
Users (ID, FullName)
Ratings (PhotoID, UserID, Value, Date)
Business rules:
users can rate a photo from 1 to 5
a given user can rate a given photo only once
I want to select the top rated photos by day in the last let's say 3 days. So which photo got the best rating today, yesterday and the day before yesterday? I would like to make the number of days variable if it possible. I have to display the last N days only they rated excluding empty days.
I would like to get the photos in a single query/result because I want to bind it to a ListView to display them on a web form.
I've started this way:
DECLARE #days INT = 3
SELECT TOP (#days) ... FROM Ratings
INNER JOIN Photos ON Photos.ID = Ratings.PhotoID
GROUP BY DATEDIFF(day, [Date], CURRENT_TIMESTAMP)
ORDER BY DATEDIFF(day, [Date], CURRENT_TIMESTAMP) DESC
How can I group my groups by PhotoID, order them by SUM(Value) and select the first one from each group? Thank you very much for your help.
SELECT Date, TotalRating, Photos.*
FROM Photos
INNER JOIN
(SELECT ROW_NUMBER() OVER (ORDER BY Date DESC) AS RowNumber,
PhotoID, Date, TotalRating
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Date, ORDER BY TotalRating DESC) AS inRowNumber,
PhotoID, Date, TotalRating
FROM (SELECT PhotoID, Date, SUM(Value) AS TotalRating
FROM Photos
GROUP BY PhotoID, Date
HAVING SUM(Value) > 0 ) t)
WHERE inRowNumber = 1) t ON Photos.Id = t.PhotoID
WHERE RowNumber <= #days