Retrieve All Rows With Largest Value In A Particular Column?

Jul 18, 2012

I am trying to retrieve all rows with the largest value in a particular column. The largest value could return many rows for a particular users. Here is what I have thus far.

SELECT DISTINCT
ID, NAME, FOP, ACCT, CTNUM, ENDDATE, DEBIT, CREDIT, TRANSACTION_DATE, EXPORTED, CALENDAR_YEAR, FISCAL_YEAR, PAYROLL_IDENTIFIER,
PAYROLL_NUMBER, [EARN-SEQNO], EVENT_SEQUENCE_NUMBER
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ID, ACCT, PAYROLL_NUMBER,EVENT_SEQUENCE_NUMBER
ORDER BY EVENT_SEQUENCE_NUMBER DESC) AS RN
FROM PAYROLLYEAREND ) s
WHERE RN = 1 AND ID = '16443' AND PAYROLL_NUMBER ='7'

In the above example, the EVENT_SEQUENCE_NUMBER is populated with values from 0 to 12. Could vary per user and PAYROLL_NUMBER. The query above returns 48 rows. However, all I want are the rows where EVENT_SEQUENCE_NUMBER is equal to the highest, which is in the above example is 12. The result would be 29 rows. The where clause is not part of overall query. Just isolating on one ID.

View 2 Replies


ADVERTISEMENT

Finding The Highest Or Largest Number In A Column Problem

Dec 19, 2006

Example data
CA1000
CA10001
CA10002
CA10003
CA11597
CA11603
CA1001
CA998
CA999
As you can see, CA11603 is the largest number in this list. 
 When I try the follow sql code,
SELECT
MAX([MyCode])
FROM
[MyTable]
WHERE (SUBSTRING([MyCode], 1, 2) = 'CA')
The largest number comes back as CA997
When I try
MAX([MyCode])
FROM
[MyTable]
WHERE [MyCode] LIKE 'CA%'
 The largest number comes back as CA997
 
SELECT
TOP 1 (SchoolMasterCode)
FROM
SchoolMaster
WHERE (SUBSTRING(SchoolMasterCode, 1, 2) = 'CA') ORDER BY Schoolmastercode
 The largest comes back as CA10001
When I try....
SELECT
TOP 1 (SchoolMasterCode)
FROM
SchoolMaster
WHERE (SUBSTRING(SchoolMasterCode, 1, 2) = 'CA')
 The largest comes back as CA1278
What am I doing wrong?

View 3 Replies View Related

Determine Largest Numeric Datatype Scale Of A Column?

Oct 25, 2012

I would like to determine the largest numeric datatype scale of a column.

Decimal Largest Scale Solution found here.

Sample Data:

2222.0002430
2222.0000245
2222.0023455

View 1 Replies View Related

SQL Server Admin 2014 :: Get Average Of Two Largest Number Amount Three Column For Particular Identity

May 3, 2015

ID A B C AVG
------------------------
1 08 09 10 -
------------------------
2 10 25 26 -
------------------------
3 09 15 16 -
------------------------

I want to calculate the average of the larges two number from the column A,B & C for particular identity and store that average in the AVG column....

View 9 Replies View Related

How Can I Retrieve Some Rows ...

Oct 18, 2007

Hi
I have a table with a column 'value' as nvarchar
I want to select some rows from this table that the value of 'value' can be converted to decimal ...
it's possible that value of 'Value' contains some other characters other than digits .

View 10 Replies View Related

Transact SQL :: Selecting Rows As Column Name And Other Column Values As Rows

Jun 25, 2015

I have questions and answers from one table, I need to select questions as column names and answers column values as the results for the questions column.

View 28 Replies View Related

How To Retrieve Rows Which Immediately After A ID

May 26, 2008

Hello..

Can any one you help me in retrieving the rows which immediately after a ID 9.
i.e, My condition is to retrieve the Record which immediately follows the event 9.
The retrieved record event should be in (24,25,26)

Table structure is
---------------------------------------------
MatchID SegmentID Event Player1 Player1ClubID
---------------------------------------------
1 0 9 1 88
1 0 24 1 89
1 0 10 1 88
1 0 9 1 89
1 0 25 1 88
1 0 10 1 88
1 0 10 1 89
1 0 10 1 89
----------------------------------------------

My out put should have
---------------------------------------------
MatchID SegmentID Event Player1 Player1ClubID
---------------------------------------------
1 0 24 1 89
1 0 25 1 88
---------------------------------------------

How to get this,
Can any one help me.

Thanks
Ganesh

Solutions are easy. Understanding the problem, now, that's the hard part

View 5 Replies View Related

Retrieve Rows That Are Less Than One Week Old

May 14, 2006

I have a table MyTable that contains two fields MyData and CreationTime. I would like to write a query
SELECT MyData FROM MyTable
WHERE CreationTime < less than one week from now
Does anyone know the real syntax for that?

Thanks in advance,
Joannès

View 3 Replies View Related

Retrieve Deleted Rows

Jan 7, 2008

Hello all,

I've just deleted some rows by error in one of my tables. I'd like to find out a quick way to retrieve them.
I'm using MSSQL server 2000, version 8.00.194

Thank you in advance

View 9 Replies View Related

Transact SQL :: How To Retrieve Affected Rows

Aug 31, 2015

I have a database table where upon inserting a new record, I get 1 row(s) affected twice. But when deleting the record, I only get one instance of that message. I'm not really an expert, neither have I designed the database, but how can I retrieve the actual affected rows or determine what table does the other row pertain to?

View 2 Replies View Related

SQL Statement To Retrieve Rows With More Than One Purchase On Any Particular Day

Sep 10, 2007

Hi,
I am new at SQL hopefully this would be a rather easy question for you guys to help me out with.
I have a table called PRODUCT with the following fields:
a. Product Name
b. Product Dept.
c. Purchase Date.

I would like to run a query to obtain all rows tha has more than one purchases on any particular day.

Any replies would be great.

View 1 Replies View Related

Retrieve Distinct Rows From Select Statement

Jan 10, 2014

Is it possible to retrieve Distinct rows from this Select Statement?

I'm getting the correct results, but duplicate rows because some customers place more than one order on the same day.

Code:
SELECT dbo.Customers.CustomerID, dbo.Customers.Title, dbo.Customers.FirstName, dbo.Customers.LastName, dbo.Customers.CustomerEmail, dbo.Customers.DateCreated,
CONVERT(char, dbo.Customers.DateCreated, 103) AS [DD/MM/YYYY], dbo.loyalty_points.LPoints, dbo.Orders.OrderID
FROM dbo.Customers INNER JOIN
dbo.loyalty_points ON dbo.Customers.CustomerID = dbo.loyalty_points.CustomerID INNER JOIN
dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID
WHERE (CONVERT(char, dbo.Customers.DateCreated, 103) = CONVERT(char, GETDATE() - 6, 103))

View 8 Replies View Related

Not Able To Retrieve Rows From Small Table-So Many Time For So Long

Mar 8, 2005

Dear Participants,

We are using merge replication for multi locational database but we are facing one problem in only one table which is not included in replication-

Table name is xxxxmast has only 39 row static information, but it used by every users for all task as select only information from this table like-

Select fin_year into mem_variable from xxxxmast where co_name = :global_variable
go

Code validation from here only.

Right validation from here only.

Report retrival validation from here only.

It means its usage for select from every user frequentely for so many times but we have to only fetch information from this table.

It was working prior fine but rightnow get problem for while-

Today Dated 08-March this table not accessible fro three times in eight hours-

1st time for 10 minute.

2nd time 10 minutes

3rd time 52 minutes.

Users want to login but at the login time years and other validation from this table, so users awaited for above mention time.

We had have do following by yesterday-

Drop table xxxxmast.

Create table xxxxmast.

Insert required data.


This is realy trouble for our application.

Any help realy great for us.

Thanks

R.Mall

View 3 Replies View Related

What Is The Maximum Number Of Rows Retrieve From A Table To Reports

Apr 5, 2007

hi friends,

i got a error while retrieving more than 100000 rows (records) from a table .. can any one tell me what is the maximum number of rows retrieve from a database to reports... and how can i overcome this issue...

View 3 Replies View Related

SQL Server 2012 :: Recurrences - Retrieve Only Rows Where Item Not Change For Given Value

Sep 16, 2015

I have a table with a datetime and an Item column.

I want to retrieve only the rows where item didn't change for a given value.

In the example below, given the value of 5 I only want the rows starting at 19:14:50 to 19:26:06.

Dateteime Item
2015-06-05 19:05:03.0002
2015-06-05 19:08:31.0002
2015-06-05 19:14:50.0001
2015-06-05 19:19:33.0001
2015-06-05 19:20:46.0001

[Code] ....

View 9 Replies View Related

How To Show Distinct Rows Of The Column Of The Dataset And Number Of Distinct Rows Of That Column

Mar 29, 2007

suppose i have aDataset with 11 rows. field1 with 5 rows of aaa, 6 rows of "bbb"

I want's some thing like

field1 rowcount
aaa 5
bbb 6

View 1 Replies View Related

Retrieve 1 Column

Apr 19, 2007

i have a table that has 9 columns that belongs to other tables.
i need to retrieve one column from this table. In order to do this, do i have to join all the other tables that has similar columns?

View 3 Replies View Related

3rd Largest

Sep 21, 2000

I want to find 3rd largest value from the table. i.e

Table Data:

Col1 Col2 col3

1 Name 1000
2 Name 2000
3 Name 1500
4 Name 500


so i want sql statement that gives me output....

Col1 Col2 col3

2 Name 2000

Thanks
-Binoy

View 4 Replies View Related

How To Retrieve Value From Particular Column By Using Parameter

Jan 5, 2008

suppose a table has 4 column,
 
Name     x         y             z        z1
A          1          2             3        3
B          0          2              0       3
C         1           0             3        3
if i pass parameter as @param=1, only column of x should be displayed, if i send parameter as @param=2 only y column should be displayed, if @param=3 is parameter then   Z column And Z1 column should be displayed. plz help to create sql query.
 
 

View 3 Replies View Related

SQL To Retrieve Column Names

Apr 3, 2008



Hi Guys

Does anybody know of an SQL query that will bring back a list of column names from a particular table?

Ive done it in Oracle before, but i cant seem to find how to do it in SQL Server 2005?

Thanks in advance.

View 5 Replies View Related

Largest Datatype

Apr 30, 2001

just needed to know the datatype that had the largest amount of characters that could be allowed within the field. thanx.

View 1 Replies View Related

Largest Tables

Apr 5, 2004

Hi,

Is there an easier way to determine which tables in a db are the largest other than running sp_spaceused for each table? Interested mainly in physical size.

Thanks

View 1 Replies View Related

How Can I Retrieve Description And Other Column Information?

Jun 2, 2004

Hi, all..
I want to know the query to retrieve Column information that we can see from table Design view of Enterprise manager, such as Column name, Pk or not, FK table, Data Type, Null or not, Description(Specially Descrition).

Is there any sp for this or any query for this?

Thank you all...

View 14 Replies View Related

How To Create And Retrieve A Calculated Column

Jul 6, 2006

Table: Names
Columns: Name_RID char(10)
Name_Type smallint
Name_Last char(50)
Name_First char(25)
Name_MI char

I have search for and see how to put the columns for the last, first mi together (Name_Last + ', ' + Name_First + ' ' + Name_MI) as Name
But how can I test the value of the Name_Type field to determine how the Name column looks
if Name_Type = 1 then
Name = (Name_Last + ', ' + Name_First + ' ' + Name_MI)
else
Name = Name_Last

The Name_Type represents Individual versus an Entity
0,ABC Pipeline,,
1,Williams, John,A

View 3 Replies View Related

Retrieve Data Into 3 Column Table

Feb 3, 2014

What would be the most efficient method of constructing a sql statement to retrieve certain record into (this gets tricky) rows of a table with 3 columns date.desc.

<table>
@foreach(var row in maytable){
<tr>
<td>@row.Most_Recent_Record1</td>
<td>@row.Most_Recent_Record2</</td>
<td>@row.Most_Recent_Record3</</td>
</tr>
}
</table>

View 1 Replies View Related

How To Retrieve The Value Of Last Saved Identity Column ?

Oct 13, 2007

Hello Everyone,

I'm using Nhibernate and SQL Server 2005.

I have two tables something like this :
Table : Account
AccountID
UserName
Password

Table : Address
AddressID
AccountID
Line1
Line2
State

My NHibernate mapping file looks like this :
For Table Account
<id name="ID" type="long" column="AccountID" access="property">
<generator class="identity"/>
</id>

<property name="Username" column="Username" type="String" />
<property name="Password" column="Password" type="String" />

For Table : Address
<id name="ID" type="long" column="AddressID" access="property">
<generator class="identity"/>
</id>

<many-to-one name="Account" column="AccountID" class="..., ...." fetch="select"/>
<property name="Line1" column="Line1" type="String" />
<property name="Line2" column="Line2" type="String" />

My Problem is it does entry in the Account table perfectly, but when it comes to do entry in Address Table it says AccountID can't be null, I'm wondering shouldn't it pick it up from the Account table as I'm passing in that object.

For the Workaround I was thinking maybe I can get the identity of the last entry saved and somehow save it to the other table myself.

I was hoping if someone can please help me.

View 1 Replies View Related

Retrieve Identity Column After Doing An Insert

May 14, 2008

I have a table with an Identity column. Is there a way to get the automatically generated primary key after I insert something to the table? If so, what is the syntax?

View 5 Replies View Related

Top 20 Largest Tables In The Database

Sep 6, 2001

Hi,

I want to know the top 20 largest tables in my database, by the size NOT by the number of rows. Can anyone give me a query which does it for me?

Thanks in advance,
David

View 1 Replies View Related

Select Nth Largest Record - How To?

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

2nd Largest Or 2nd Smallest Element ?

Dec 20, 2007

Hey
Its easy to get the MAX or MIN from a table,
but what if you want to get the 2nd largest ? or 2nd smallest ?

View 7 Replies View Related

Retrieve 'x' Number Of Characters From A Database Column

Jan 10, 2008

Is there any way to retrieve partial contents of a database column? For example, say a column holds 5,000 characters, but I only wish to retrieve the first 50.
Thanks

View 2 Replies View Related

Retrieve Part Of Address - Column Is Varchar?

Mar 25, 2015

I want to just retrieve the numbers left of the space in this address column what can I use? The Address column is Varchar.

Address:
6501 Red Hook Place #201
32 Orchard St
1 Hardy Rd ste 210
379 E Center St.
170 Bridge St
399 Interpace Pkwy
1800 Valley Rd
2210 Harding Hwy
41 Maple Ct

Expected Results:

Address
6501
32
1
379
170
399
1800
2210
41

I tried using this:

Left([ADDRESS], Charindex(' ',[ADDRESS] - 1)) as 'Address'

But i received an error:

Conversion failed when converting the nvarchar value '6501 Red Hook Place #201' to data type int.

View 2 Replies View Related

Failed To Retrieve Long Data For Column

Mar 23, 2007

Hi,

We have a SSIS project where we load lot of image files using an OLE DB Source component and put it across the database as varbinary into OLEDB Destination component.

Things were fine until now where am getting an error like this. alongside the log also reads like,

There is not enough space on the disk. Long data was retrieved and can't be added to the data flow task buffer.

SSIS Error Code: DTS_E_InducedTransformFailureOnError.

Is this method of loading the files using an OleDb Souce not the efficient one ? If not is there a way I can get this done. Comments are highly appreciated!!

Thanks in Advance



View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved