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;
Related
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 4 months ago.
Improve this question
I have two tables with many columns. One contains 50k custIDs, date & Payments received for every id and the other table contains 5k custIDs. I need to find How much payment received per CustId on monthly basis for these 5k CustIDs ?
Assuming your dates are stored as date...
select i.custId, i.custName,
to_char(p.payment_date,'yyyy-mm') as payment_month,
sum(p.amount_paid) as total_amount_paid
from customers i
join payments p
on i.custId = p.custId
group by i.custId, i.custName, to_char(p.payment_date,'yyyy-mm')
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 5 years ago.
Improve this question
The question is to Produce a list of the latest movies by genres for the current month. How to find the current month from the Date???
You are looking for DATEPART()
Select
*
From YourTable
Where
datepart(month,ReleaseDate) = datepart(month,getdate())
and datepart(year,ReleaseDate) = datepart(year,getdate())
Order by Genre, ReleaseDate desc
You can try this:
SELECT SUBSTRING(TO_CHAR(now(),'YYYYMMDD'),5,2)
In which:
Format to date and you get only the month
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Need help to select a number of data per month, there are 4 retailers and I need to select a total of transactions per month and the total amount for that month showing each month separately in sql.
Extract the month from the date and group by it. Then you can use aggregate functions to sum and count
select month(date_column),
sum(amount) as total_amount,
count(*) as transaction_count
from your_table
group by month(date_column)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to display hotel bookings from the 1st of every month.
Hotel (hotelNo,hotelName,hotelAddress)
Room (hotelNo,roomNo,type,price)
Guest (guestNo,guestName,guestAddress)
Booking (hotelNo,guestNo,dateFrom,dateTo,roomNo)
Here's what I have so far. Obviously it isn't working. How do I select the first of any given month?
SELECT hotelNo, dateFrom
FROM booking
WHERE datefrom >= to_date('01', 'dd');
If you want records that are on the first day of any month, then you can EXTRACT the day part of the date and compare it with 1:
SELECT hotelNo, dateFrom
FROM booking
WHERE EXTRACT(DAY FROM datefrom) = 1;