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
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:
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;
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
This question already has answers here:
How can I remove duplicate rows?
(43 answers)
Closed 8 years ago.
suppose you have a data set having 3 columns: ID, user, date.
is it possible to filter the data based on the minimum date even if some of the rows have identical IDs?
sorry if the question is a bit unclear. hopefully the image below will help clear things.
there are two records having ID=1, with different users as well as dates. what i want to retrieve is the record having an ID=1, USER=A, DATE=2013-01-20 because its date is earlier than that of the second record (ID=1, USER=A, DATE=2013-01-21)
i want to achieve the same effect for the three records having an ID=2. the desired record is ID=2,USER=C,DATE=2013-10-20
basically i want to group these records by their IDs and then from that grouping, get the one with the lowest date
SELECT id, user, date
FROM OriginalData od
WHERE date = (SELECT MIN(date)
FROM OriginalDate od1
WHERE od.id = od1.id)
Select * from table_name where date in
( select MIN(date) from table_name)
You have tyo use Group By clause on Id attribute.
Use the following syntax
select * from tab1 where (id, date) in (select id, min(date) from tab1 group by(id))