This question already has answers here:
sql ORDER BY multiple values in specific order?
(12 answers)
Closed 2 years ago.
I have a large amount of data in data base.Consider this i have a table of student records and i need to get all the student with A grade first and then the B grade and so on..
It sounds like you want order by:
select t.*
from t
order by t.grade;
Related
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Closed last year.
I have two Tables, one where Project Information is stored, and a separate one for Store Activity log on each project. I'm trying to create a SQL to retrieve 1 line from each project displaying the project Information and the latest project update only but I'm not being able to get that information.
My Tables are set up the following way:
Project Data
Project_ID (PK)
Project Name
Project Status
Project Milestone
Project_ID (FK to Project DATA)
Milestone_Type
DATE
I was trying the following query but without luck.
select PROJECT_DATA.PROJECT_NAME as PROJECT_NAME,
PROJECT_DATA.PROJECT_STATUS as PROJECT_STATUS,
PROJECT_MILESTONE.MILESTONE as MILESTONE,
PROJECT_MILE`enter code here`STONE.DATE as DATE
from PROJECT_MILESTONE PROJECT_MILESTONE
Left Join PROJECT_DATA PROJECT_DATA on PROJECT_DATA.ID=PROJECT_MILESTONE.PROJECT_ID
where
PROJECT_MILESTONE.DATE = (
Select MAX (PROJECT_MILESTONE.DATE)
FROM PROJECT_MILESTONE
WHERE PROJECT_DATA PROJECT_DATA on PROJECT_DATA.ID=PROJECT_MILESTONE.PROJECT_ID)
Table "setup" and query you posted differ so - I'm going to do the same - post query which should return data you need, but with my own table and column names.
select d.project_id, d.name, d.status, m.datum, m.type
from project_data d join milestone m on m.project_id = d.project_id
where m.datum = (select max(m1.datum)
from milestone m1
where m1.project_id = m.project_id
);
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 1 year ago.
Write a query to display the student names and the maximum mark scored by them in any subject, ordered by name in ascending order. Give an alias to the maximum mark as MAX_MARK.
I am not able to find the logic for this. Kindly help me with it. Do it in oracle SQL I am at beginner level in SQL.
SELECT MAX(M.VALUE), S2.SUBJECT_ID,M.STUDENT_ID, S2.SUBJECT_NAME,S2.SUBJECT_CODE
from Mark M INNER JOIN SUBJECT S2
ON M.SUBJECT_ID=S2.SUBJECT_ID group BY S2.SUBJECT_ID,
S2.SUBJECT_CODE, S2.SUBJECT_NAME;
I am getting error with this query if I get this student id with the help of the above query then I can easily solve this question using subquery concept.
You don't need subject there. Question asks Max mark per student, regardless of subject:
SELECT s.Student_Name, MAX(M.VALUE) as MAX_MARK
from Student s
inner Join Mark M on m.student_id = s.student_id
group by s.student_id, s.student_name
order by s.student_name;
This question already has answers here:
Get top 1 row of each group
(19 answers)
sql server select first row from a group
(2 answers)
Closed 3 years ago.
I have 2 columns in a table where column A has distinct records and column b does not i.e. column b could have multiple items of the same value in each row.
What I require is to only show distinct records based on column B. I do need both columns though as it will be an input in Crystal Reports but I don't care what value column A has.
CreatedBy column has multiple different values and I need to get those distinct values.
Query
SELECT DISTINCT nh.InNeoHistoryId, nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy, nh.InNeoHistoryId
Result
Try below query
SELECT nh.CreatedBy,min(nh.InNeoHistoryId)
FROM NeoHistory nh group by nh.CreatedBy
Just make a small tweak to your query:
SELECT MIN(nh.InNeoHistoryId), nh.CreatedBy
FROM NeoHistory nh
GROUP BY nh.CreatedBy
This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 4 years ago.
I need to create a different column of total sales which is the total of the sales units till that row. As our sales unit will increase, the total sales will increase row by row. I have attached the image to get the clear idea.
Screenshot
One method is use to use of subquery with correlation approach
select *,
(select sum(sales) from table where product = t.product and ? <= t.?) TotalSales
from table t
However, you would required ? (i.e. rowid or id) column that could specify your column ordering.
You can use the following query, which adds up all the sales upto that rowId and for the specific product.
select (select sum(ids)
from TBL_NAME T1
where T1.rowid <= TBL_NAME.rowid and PRODUCT=TBL_NAME.PRODUCT
) as TotalSales
from TBL_NAME;
Following answer is for Oracle DB, you can understand the query and easily convert it to the product you want.
This question already has answers here:
How to get the last record per group in SQL
(10 answers)
Closed 6 years ago.
I have a big log table with 2 million rows give or take.
I am looking to look for the last entry for each id.
The 3 columns of importance are
Userid
Actiontype
Actiontime
Text2
Some userids show up thousands of times some just show up once. I need the most recent of each userid. I tried to use 'Group By' but it wont work because text2 is different for each entry which is really the data I need. So it needs to be ordered by actiontime, actiontype needs to be 103. I am really at a loss how to do this.
Any help would be appreciated.
Select B.*
From (
Select UserID,ActionTime=max(ActionTime)
From SomeTable
Group By UserID
) A
Join SomeTable B on A.UserID=B.UserID and A.ActionTime=B.ActionTime