Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a dataset containing a list of recorded coronavirus cases in countries across the world through a time period of December 2019 to April 2020. I am trying to write an SQL query to give a a cumulative total for the top ten countries through this time period (i.e. the countries with the highest total at the end).
I have started this by writing a query to give a cumulative total for all countries, and then have tried to write a sub-query to limit this list to only include the data for the top ten countries.
SELECT
Virus.dateRep,
Country.countriesAndTerritories,
SUM(Virus.cases) OVER (PARTITION BY Country.geoId ORDER BY Dates.year, Dates.month, Dates.day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS 'cume_cases',
SUM(Virus.deaths) OVER (PARTITION BY Country.geoId ORDER BY Dates.year, Dates.month, Dates.day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS 'cume_deaths'
FROM
Virus
INNER JOIN
Dates ON (Virus.dateRep = Dates.dateRep)
INNER JOIN
Country ON (Virus.geoId = Country.geoId)
WHERE Virus.geoId IN (SELECT
Virus.geoId, sum(Virus.cases) as 'total'
FROM
Virus
GROUP BY
Virus.geoId
ORDER BY
total
DESC
LIMIT 10)
GROUP BY
Country.geoId,
Virus.dateRep
ORDER BY
Dates.year,
Dates.month,
Dates.day,
Country.countriesAndTerritories
ASC;
However, the sub-query only allows for one column to be queried, and so is not working. Any solutions?
You can do this with only one column:
WHERE Virus.geoId IN (SELECT v.geoId
FROM Virus v
GROUP BY v.geoId
ORDER BY sum(Virus.cases) DESC
LIMIT 10
)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
madam,
I would like to make a top 50 of users who received the most points.
So my database table looks like this:
points table:
pid | uid_give | uid | amount | time
(every row indicates an amount given)
Now I want to make a top 50 of users who received the most amount.
I had the sql query but dont remeber to do this.
Can you help? (group by?)
Regards,
in mysql
select id, uid_give, uid, amount, time from points order by amount desc limit 50;
in sql server
select top 50 id, uid_give, uid, amount, time from points order by amount desc ;
Oracle 12 and onward
select id, uid_give, uid, amount, time from points order by amount desc
FETCH FIRST 50 ROWS ONLY;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Currently I'm using below SQL code
Last_Value (dbo.TransactionTable.TransDate)
over (PARTITION BY dbo.TransactionTable.TransNumber
Order by dbo.TransactionTable.TransNumber
Rows between UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING)
as LastPaymentReceived
When I run the above SQL script it gives me 06/11/2021.wherein the most recent successful repayment should be 06/08/2020.The code works fine when if customer not missed the payment, Things get complicated when Customer DD returned with transaction posted "Unpaid DD - Instr Cancelled"
However, last payment date for this is 06/11/2021) but this payment was returned unpaid with a transaction posted to the account on 06/11/2021 with transaction posted "Unpaid DD - Instr Cancelled". Please see the Transactiondetail
Someone can advise me?
Many Thanks
Dan
The code you've given is only a snippet of a single item from a SELECT clause. If you want to return multiple rows for different customers, or multiple fields, the answer below may be difficult to integrate into the rest of your code.
SELECT TOP 1
t2.TransDate
FROM
TransactionTable t2
WHERE
NOT EXISTS (SELECT 1 FROM TransactionTable t1
WHERE t1.TransDate = t2.TransDate
AND t1.Description = 'Unpaid DD - Instr Cancelled')
ORDER BY
t2.TransDate DESC
You will need to add customer filters in the WHERE clause for both the main query and the subquery.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Question was:
average grade for the whole study year of that student.
Select FirstName,LastName,Year,AverageGrade,
Avg AverageGrade Order by YearAverageGrade
from Student
You don't have a PARTITION BY clause. Unless you tell SQL server what the window is, it will use the entire dataset (it can't read your mind, and guess what you're after). I also doubt you need the ORDER BY in the window (as that would default to ORDER BY [Year] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). I assume you therefore want this:
SELECT FirstName,
LastName,
[Year],
AverageGrade,
AVG(AverageGrade) OVER (PARTITION BY [Year]) AS YearAverageGrade
FROM dbo.Student;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to sum data in a table.
Data in my table:
Month Osl Sale
10 8-02-01-01 38440.5
10 8-02-01-03 14961
10 8-03-02-01 10388.3
10 8-05-04-01 81666.6
10 8-05-04-05 29431.8
10 8-07-01-09 9821.4
10 8-09-01-01 7567.5
And my expected output is:
I think union all is the simplest method:
select month, osl, sale
from t
union all
select month, left(osl, 7), sum(sale)
from t
group by month, left(osl, 7);
Not all databases support left(). In those that don't, either substr() or substring() can extract the first seven characters.
Unsure of your RDMS, but I would suggest something along the lines of:
select * from table1
union all
select t.month, left(t.osl, len(t.osl)-3), sum(t.sale)
from table1 t
group by t.month, left(t.osl, len(t.osl)-3)
Change table1 to your table name as appropriate.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have below database table,In here i need group some data and get sum and avg of some data.
In here i need to calculate Item marketers monthly sales(Each Month)
As a Example :
I need to get a Patric Newton's Sales in Each months.
And also i need to calculate AvarageFactor of each month Each employee.
AvgFactor = SUM(Daily Sales)/ SUM(Value Factor) * 100( Each Month )
Database Table
I have tried it like below,
SELECT ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
Group by ItemMarketerName,DailySales,ValueFactor,[Month],[Year]
Order by ItemMarketerName
You should not include your aggregated columns in your GROUP BY.
Try:
SELECT ItemMarketerName, [Month], [Year], SUM(DailySales), SUM(DailySales) / SUM(ValueFactor) * 100
FROM [SR_Hotel].[dbo].[Table_1]
WHERE ItemMarketerName IS NOT NULL
GROUP BY ItemMarketerName, [Month], [Year]
ORDER BY ItemMarketerName