How To Select Earliest Record
Mar 17, 2012
im trying to get the earliest record. data is below
note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
2332/ 20090106<---- / 20081219/ 20090106
2332/ 20090323/ 20081219/ 20090323
2332/ 20090413/ 20081219/ 20090413
2332/ 20090507/ 20081219/ 20090507
because the bankruptcy_date date are all equal i would need to pull one record with the earliest date from the doc_received_date. The date with the arrow is the record i want. So basically i would like to show this result
note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
2332 20090106 20081219 20090106
View 5 Replies
ADVERTISEMENT
Feb 9, 2007
Hello all,Quick sql syntax question:I have this table:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[REQUESTS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[REQUESTS]GOCREATE TABLE [dbo].[REQUESTS] ([ROW_ID] [uniqueidentifier] NULL ,[REQUEST_DATE] [datetime] NULL ,[STATUS] [tinyint] NULL) ON [PRIMARY]GOwith these values:insert into REQUESTS (REQUEST_DATE, STATUS)values (getdate(), 0)insert into REQUESTS (REQUEST_DATE, STATUS)values (getdate(), 1)insert into REQUESTS (REQUEST_DATE, STATUS)values (getdate(), 0)I need to select the single record with a STATUS = 0 with the earliestREQUEST_DATEI am using this query:SELECT TOP 1 ROW_ID FROM REQUEST_LOG WHERE STATUS = 0 ORDER BYREQUEST_DATEnot sure if this is the way to go...pointer appreciatedthanks
View 3 Replies
View Related
May 25, 2005
I'm no SQL whizz yet but I'm learning hard, and need to get some information from our DB rather urgently so have resorted to this fantastic forum, only I can't find what I'm looking for.
Basically I'm selecting a whole load of entries that have a (admission)date field after 2001, but I only want to return the Earliest (admission) for each (patients number).
Here is the script I have created to select all the data, but how can I limit the results to just the earliest (admission date) for each (patient).
SELECT
Admission_Year, Admission_Month, Age_On_Admission, [Length of stay(continuing)], [Patient's Number], [Cons epis seq no], Sex, [Main Primary Pas Diag], [Date of Death], [Epi duration], [OP Code1], [Admission date], [Date of Death] - [Admission date] AS [days before death],[Intended Management]
FROM dbo.Admissions
WHERE (Admission_Year > 2001) AND (Age_On_Admission > '64') AND ([Intended Management] = 'inpatient') AND ([Date of Death] IS NULL)
I would really appreciate it if anyone can help with this, I'm sorry I can't really contribute to this forum as an SQL expert as .net is really my forte and I usually spend my time contributing to the asp.net forums. :)
View 1 Replies
View Related
Jun 12, 2008
I'm having a mental block on a select statement.I have a table with the following columns:KitId int
LotNo varchar(10)
DateReceived smalldatetimeIt is possible to have many rows with the same LotNo but a differing DateReceived I need to write a select statement that returns the KitId for a given LotNo with the earliest DateReceivedso if I had rows:KitId =1, LotNo = 123, DateReceived = 11th May 2008KitId =2, LotNo = 123, DateReceived = 28th May 2008KitId =3, LotNo = 125, DateReceived = 28th May 2008KitId =4, LotNo = 127, DateReceived = 28th May 2008KitId =5, LotNo = 123, DateReceived = 12th June 2008I would want to retrieve KitId=1 if I provided LotNo 123 as a parameter Whilst it should always be the case that the LotNo with the earliest date will have the lowest KitId I cannot guarantee that will be the case so going for the lowest KitId isn't an optionCan one of you SQL gurus provide me with the statement I need?ThanksNeil
View 2 Replies
View Related
May 7, 2008
Hi I am having trouble and I don't know why.
I have this query which I pasted below. I need to find the earlist effective date (pcsp_eff), but I need to show all of the fields below in my report like a flat file. I know ususaly when you use Max/Min you have to have a group by, would I group by everything that is in my select statement?
SELECT Distinct
dbo.pcs.pcs_id1,
dbo.pcs.pcs_lname AS [Last Name],
dbo.pcs.pcs_fname AS [First Name],
dbo.pcsp.pcsp_eff AS [Effective Date for Provider],
Min(pcsp_eff) as "earliestEffectivedate",
dbo.pcst.pcst_trm1 AS [Tracking Thru Date],
dbo.pcst.pcst_dat3 AS [Re-cred Letter Sent Date],
dbo.pcst.pcst_dat7 AS [Re-cred Complete Date],
PRO_STATE as "State",
PRO_COUNTY as "County"--, PCSP_NET
FROM dbo.pcs INNER JOIN
dbo.pcst ON dbo.pcs.pcs_id1 = dbo.pcst.pcst_id1 INNER JOIN
pcsp ON pcs.pcs_id1 = pcsp.pcsp_id1 left Join
dbo.pro ON pcs.pcs_id1 = pro.pro_id1
WHERE
(CONVERT(CHAR(10), dbo.pcst.pcst_dat3, 110) <>'03-23-1977') and
(CONVERT(CHAR(10), dbo.pcst.pcst_dat7, 110) = '03-23-1977') and
(pcsp.pcsp_prd = 'DGH') AND
--(pcsp.pcsp_id2 = '0001') and
PCS_CTL = 'I' and
pcsp_NET <> 'DACFP' and
pcs_id1 = '00004307'
Group by pcs_id1
View 2 Replies
View Related
Nov 26, 2013
I want to find out the earliest [First_Post_Date] for any parentdid
My query (See below)
Produces the following results
SELECT
ParentID
,[First_Post_Date]
,[FDMSAccountNo]
FROM [FDMS].[dbo].[Dim_Outlet]
where ParentID = '878595212886'
Order by ParentID desc
[code]....
View 1 Replies
View Related
Jul 20, 2005
Hi,I have a table (SQL Server 2000) with several date columns in it, all ofwhich are individually NULLable, but in any one row, not all the dates canbe NULL.I want a query which ORDERs BY the earliest date it finds in each row. I'mguessing I have to do this in two steps:STEP 1Using a UDF, find the earliest date and stick it in a new calculatedcolumn "earliest date"STEP 2ORDER BY this UDF-created columnIf this is the right way to go about this, is there a simple SQL way ofdetermining which is the lowest of several dates? (ie of doing STEP 1).Or am I looking at this the wrong way, and missing an easy *one-step* way ofgetting what I want?TIA,JON
View 5 Replies
View Related
Mar 1, 2007
Hi,
How to get the latest date from ColumnDateTime?
and the earlier as well ?
View 5 Replies
View Related
Dec 10, 2014
I have two dates. How do I get the one that is the lowest. One may be null. I don't want null unless they are both null
I tried..
DECLARE @Handle date
SELECT @Handle = dbo.getTrkLeastDate('2014-12-09',NULL)
print @Handle
ALTER FUNCTION [dbo].[getTrkLeastDate] (@d1 date, @d2 date)
RETURNS datetime
[Code] .....
View 4 Replies
View Related
Apr 21, 1999
I would like to exec a select statement in VB/C++ to return first 100 records? What is the SQL statement should be?
Thanks,
Sam
View 1 Replies
View Related
Nov 30, 2011
I have to find the earliest date for any existing calendar year in a table...
For instance.....
ClientID Year
1 1/1/2000
1 2/1/2000
2 3/1/2000
2 4/1/2001
2 5/1/2001
The results I need are....
ClientID Year
1 1/1/2000
2 3/1/2000
2 4/1/2001
It is all contained in one table. I have got the earliest years by using the YEAR() function and grouping by it. The only problem is that I am having a problem joining the table back onto itself with the subselect because of it's an aggregate.
Here is the first join I have....
SELECT DT.ClientID, Year1 FROM
(
SELECT ClientID, YEAR(MyDate) as Year1
FROM MyTable
) AS DT
GROUP BY ClientID, Year1
ORDER BY ClientID, Year1
I want to use that as my derived table and then join back to it... but it's not working...
View 1 Replies
View Related
Mar 12, 2007
Finding the earliest datetime entry, and then updating the database on these reults.
I need to query a table of data that has multiple datetime entries in it relating to individual customer records. So one customer could have 1 entry, or it could have 10 entries. I need to be able to identify the earliest of these records, and then on this earliest record update a field value. Example:
My Fields in the table are as follows.
Log_ID, Cust_Ref_No, ActionDateTime, Action_Code
The Log_ID is the primary key, and I need to update the Action_Code field on only the earliest entry against a customer record i.e.
12345,ABCDEF,01/01/2007 00:00:01.000,6
12346,ABCDEF,01/01/2007 09:00:00.000,6
12347,ABCDEF,01/01/2007 17:00:00.000,2
In the above I need to change the first record Action_Code from a 6 to a 1, but leave all other records unaffected.
All help greatly appreciated, Thanks : o )
View 10 Replies
View Related
Oct 13, 2005
Hi,
I'm having problems with a stored procedure, that i'm hoping someone can help me with.
I have a table with 2 columns - Username (varchar), LastAllocation (datetime)
The Username column will always have values, LastAllocation may have NULL values. Example
Username | LastAllocation
------------------------
Greg | 02 October 2005 15:30
John | 02 October 2005 18:00
Mike | <NULL>
My stored procedure needs to pull back a user name with the following criteria:
If any <NULL> dates send username of first person where date is null, sorted alphabetically, otherwise send username of person with earliest date from LastAllocation
Then update the LastAllocation column with GETDate() for that username.
This SP will be called repeatedly, so all users will eventually have a date, then will be cycled through from earliest date. I wrote an SP to do this, but it seems to be killing my server - the sp works, but I then can't view the values in the table in Enterprise Manager. SP is below - can anyone see what could be causing the problem, or have a better soln?
Thanks
Greg
------------------------------------------------------------------------------
------------------------------------------------------------------------------
CREATE PROCEDURE STP_GetNextSalesPerson AS
DECLARE @NextSalesPerson varchar(100)
BEGIN TRAN
IF (SELECT COUNT(*) FROM REF_SalesTeam WHERE LeadLastAllocated IS NULL) > 0
BEGIN
SELECT TOP 1 @NextSalesPerson = eUserName FROM REF_SalesTeam WHERE LeadLastAllocated IS NULL ORDER BY eUserName ASC
END
ELSE
BEGIN
SELECT TOP 1 @NextSalesPerson = eUserName FROM REF_SalesTeam ORDER BY LeadLastAllocated ASC
END
SELECT @NextSalesPerson
UPDATE REF_SalesTeam SET LeadLastAllocated = GETDATE() WHERE eUserName = @NextSalesPerson
COMMIT TRAN
GO
View 2 Replies
View Related
Nov 14, 2001
Hi
Here is my example: I have the following table
ID Name Phone
--- ---- -----
1 John 1234567
1 John H.
2 Dave 9876543
2 Dave Smith
I want to select only one record of each ID, the output should be
ID Name Phone
--- ---- -----
1 John 1234567
2 Dave 9876543
It doesn't matter which record to select, but I need just a single complete record. I can't use select distinct ID because it will not show the other fieldnames.
Thanks
View 4 Replies
View Related
Nov 8, 2004
I have a sql query that looks for values in a few different databases....
is there a way to select the last record in the table b/c I am pulling the hours worked on jobs and one person may have 3 job titles but i want it to show the balance of hours under the most recent job... Instead all the jobs are showing the same value even though only one of them had those hours under it. I think a way to get around this is to select the last value b/c that is under the job title most recently worked and they are in order by timesheets which can be ordered by date...any ideas?
View 2 Replies
View Related
Sep 18, 2013
I have view with Patients name and Appointment table where I save those patients. How to create store procedure if I would like to select one patient record based on Patient_Id value?
View 3 Replies
View Related
Jun 19, 2007
Hai frendz,
I am having a table named Employee(int EID, float Salary)...
Now I want to select the highest salary in the table and the query is-
"select top 1 EID, Salary from Employee ORDER BY Salary DESC"
Now I need to write a query which selects the second highest salary.
So how to achieve this?..
thanx
View 6 Replies
View Related
May 29, 2008
hi
I want to lock the record on select query so that no other user can update this record, is it possible and i want to unlock record when use stop view this record.
View 1 Replies
View Related
Apr 26, 2005
i have two tables A and B. relation is one to many for A. i want to select from A only if there are more than two records of A in table B and also checking some condition in table B. if the question's not very clear please let me know.
View 1 Replies
View Related
Feb 18, 2006
how to select record from the table where the data between a range. example between 2/16/2005 and 12/16/2005. the data record in the table formated like this ( 2/16/2005 11:44:38 PM). help me with some sql code, thanks
View 2 Replies
View Related
Mar 20, 2002
Hi,
I need to select first record from a table.
This can be comfortably achieved by usibg set rowcount 1.
I need to do this with out using rowcount.
This is urgent.
Thanks
Krishna
View 2 Replies
View Related
Aug 28, 2001
Hi!
I need to select a spesific record using the recordkey and then select the previous and the next record as well. (which leaves me with a recordset containing three records)
Is this possible?
View 3 Replies
View Related
Nov 7, 2001
hi friends,
I am having a table with student marks in that i need to select a student who is the 5 th rank based on total marks obtained by the student.how can write sql for that.
name, totmarks
-----------------
kumar 145
ravi 300
jude 189
geeetha 320
rajesh 251
guru 190
ramesh 99
----------------------
i am having records like this.pl help me. thank u
regards
raj
View 1 Replies
View Related
Feb 14, 2006
Hello, everyone:
I have two tables like:
Table A
IDCodeANumA
1D1289
2C4300
3C9409
Table B
IDCOdeBNumB
1D3266
2C4300
3C7101
How to find out the records that is in Table A not in Table B, and in Table B not in Table A? That means C4/300 should not be selected.
Thanks
ZYT
View 3 Replies
View Related
Sep 14, 2006
The developers of frond end ask to select only one row from one table, but many times, such as 10. That means the output has 10 rows and same.
Any suggestion will be appreciated.
ZYT
View 4 Replies
View Related
Dec 21, 2006
Every hour we capture some values from our factory (position of pumps, valves, ...) in our sql server 2000 db.
Normally 1 record is added to the db.
00:00:00
01:00:00
02:00:00
...
21:00:00
22:00:00
23:00:00
All of these values are displayed in an Excel sheet. One sheet contains all the data from one month.
I noticed a problem last week when 2 records were added: first one at 19:00:00 and the second one at 19:00:01
We only want to keep the most recent record (19:00:01) in a situation like this but I can't seem to work out an SQL-statement :o
This is what we have know. It used to work fine untill we had 2 record being added instead of one.
SELECT TimeOfCapture, Value1, Value2, Value3
FROM FactoryTable
WHERE (MONTH(TimeOfCapture) = MONTH(GETDATE())) AND (YEAR(TimeOfCapture) = YEAR(GETDATE()))
View 1 Replies
View Related
Apr 24, 2007
Hi everyone,
I am having a header table and details table and I want to display first 2 records against each header from details, whatever number of records are there in details against each header.
Example :
=======
Details table is as follows
HeaderID DetailsID
1 1
1 2
1 3
2 4
3 5
3 6
3 7
3 8
output should be:
TransDate SupplierName HeaderID DetailsID
1/1/2000 abc 1 1
1/1/2000 dsds 1 2
2/3/2003 fgd 2 4
2/4/2005 sdsd 3 5
1/1/2006 fgfdg 3 6
I am using the following query
SELECT H.TransDate, H.SupplierName, D.DetailsID FROM
Header H, Details D
WHERE H.HeaderID = D.DetailsID
AND D.DetailsID IN (SELECT TOP 2 DetailsID FROM Details WHERE HeaderID = H.HeaderID)
As I am dealing with very huge data it is taking very long time to execute.
Is there any better way to accomplish the task?
Thanks.
Riyaz
View 14 Replies
View Related
May 18, 2012
query to show last record/Partner or PartnerName.
select 'PURCHASE' as EntityName, d.PartnerName, h.*
from (
select MAX([TimeStamp]) [Data import], COUNT(1) [Numar de inregistrari], StartDate, EndDate, DistributorId
from DataImport.PurchaseHistory
group by DistributorId, StartDate, EndDate
) h
inner join Partner d on d.PartnerId = h.DistributorId
where d.Active = 1
order by DistributorId, StartDate desc, EndDate desc
View 4 Replies
View Related
Sep 17, 2012
I am wondering how to select the Max date within a table where a timestamp is within 2 second of the other
IDTimestamp
1NULL
209/17/2012 11:04:32
309/17/2012 11:05:44
409/17/2012 11:05:45
So I need a query to return
IDTimestamp
409/17/2012 11:05:45
as 11:05:45 is the max of 09/17/2012 11:05:44 and 09/17/2012 11:05:45 and they are within 2 seconds of eachother.
View 6 Replies
View Related
Jan 24, 2004
I'm looking for some sql syntax that will return the last entry per group in a secondary table. MEANING: have 2 tables, one with names and the other with visits. I need to be able to display all the patients with there last visits.
TABLE1 info
ID1 fname1 lname1 DOB1
ID2 fname2 lname2 DOB2
ID3 fname3 lname3 DOB3
TABLE2 info
ID1 Visit2
ID1 Visit3
ID1 Visit1
ID1 Visit4
ID2 Visit1
ID2 Visit2
ID3 Visit1
I need a view or SP to return the following:
ID1 fname1 lname1 dob1 visit4
ID2 fname2 lname2 dob2 visit2
ID3 fname3 lname3 dob3 visit1
It seems like it should be a smiple process, only I just can't get the syntax to work.... Any Help would be GREAT!
thx
View 2 Replies
View Related
Feb 3, 2004
How do i select first name of the lets say 6th highest salary of an employee?
is it use Select TOP statement?
p/s: I just want the 6th largest fname not the top 6. so it will only return one value.
Thank you in advance for your reply.
View 1 Replies
View Related
Apr 27, 2004
Hi,
I want to construct a query that would give the most recent record. Table data looks like -
F1F2F3 F4
20016395074/7/2004 15:40:55
23516395074/6/2004 9:31:54
All columns are strings as the data is obtained from text file. Column F3 can have same or different dates. In the above data select 1st record as it is the most recent.
If data is like -
F1F2F3 F4
20016395074/7/2004 15:40:55
23516395074/7/2004 19:31:54
Select the 2nd record.
How do I construct the SQL query?
Let me know.
View 1 Replies
View Related
Sep 18, 2013
I have view with Patients name and Appointment table where I save those patients. In form I have 2 comboboxes. For PatientComboBox source is store procedure based on Patient view. For PatientAppintmentComboBox I would like to create store procedure.
How to create store procedure to display only one PatierntName record in PatientAppointmentComboBox if Patient_Id selected from PatientComboBox exist or not exist in Appointment?
View 3 Replies
View Related