473,581 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Server Query (complicated)


I have the following data in a table:

2 abc NULL
2 aaa NULL
4 xyz NULL
4 xyz pqr
4 pyz xqr

I want to get only one record for each record number. that is, the
result set should be like this:

2 abc NULL

Please suggest how the query should be built. Thanks for the help.

Jul 20 '05 #1
3 4864
Unfortunately, it's not easy to give a good answer without more information.
From what you have posted below, it's not clear why you want those two those
records and not one of the others, unless perhaps there is another column
which you didn't include and that you use to order by.

I suggest you post a CREATE TABLE for your table, with some INSERT
statements to add test data, and then a sample result set that you would
like to see.


"Murali" <mu************ *@hotmail.com> wrote in message
news:62******** *************** *@posting.googl e.com...

I have the following data in a table:

2 abc NULL
2 aaa NULL
4 xyz NULL
4 xyz pqr
4 pyz xqr

I want to get only one record for each record number. that is, the
result set should be like this:

2 abc NULL

Please suggest how the query should be built. Thanks for the help.


Jul 20 '05 #2
Thanks Anith & others.

I think I confused you. I'm sorry. What I want exactly is, ONLY ONE
RECORD for each <col1>. I don't care whether it's first or last. But
it's better if I get the first record.

Ex: Say I've this data in test table:
Pno Pname
1 xxxx
2 abcd

I want the result to be:

1 xxxxx

Hope, I'm clear this time. Thanks for the help.


"Anith Sen" <an***@bizdatas olutions.com> wrote in message news:<6P******* **************@ bgtnsc04-news.ops.worldn et.att.net>...
First, add keys to your tables. Then, if possible, try not to use NULLs as
logical values. In any case, do:

FROM tbl
WHERE COALESCE(col2, 'aaa') =
(SELECT TOP 1 COALESCE(t1.col 2, 'aaa')
FROM tbl t1
WHERE t1.col1 = tbl.col1
ORDER BY t1.col2);

Jul 20 '05 #3
>> But it's better if I get the first record. <<

There is no 'first' & 'last' rows in SQL. In fact logically a table is a set
of rows & by definition of sets the rows have no order. Data is retrieved
based on the values in the table, not by position of rows.

For the example in your second post, you can do:

SELECT Pno, MAX(Pname) --- or MIN
FROM tbl

If you have other columns, here is a generalized soln :

FROM tbl
WHERE keycol = ( SELECT TOP 1 t1.keycol
FROM tbl t1
WHERE t1.dupcol = tbl.dupcol
ORDER BY t1.uniqueCol DESC ) ;

In the above, uniquecol is the column or set of columns which can uniquely
identify a row per group (pname in your case) in the table & dupcol is the
column which is being duplicated (pno in your case )

- Anith
( Please reply to newsgroups only )
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Andrew James | last post by:
Gentlemen, I'm currently in the process of designing a language which will be used to specify sets of files on a WebDAV server, encoded in a URL. The aims of the language are to (in no particular order): * Be concise, aesthetic and easy to type * Be as similar as possible to existing query languages * Allow for (nested) boolean operations *...
by: wiredog | last post by:
I am struggling rewriting my query from MS Access' IIF, Then to SQL Servers TSQL language. I am hoping some one can give me some guidance. I believe I have the first portion of the query correct but do believe this requires a "NESTED" argument. This is where I am lost. My Original MS ACCESS Query reads-- SELECT DISTINCTROW...
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will lead to a lot of use of the database and the adding of quite a lot of new data (though I can't conceive of them adding more than than 10s of...
by: brettclare | last post by:
I have linked a large SQL Server table to Access, however 'only' 2,195,439 records are shown and are available to query. Can I increase the size (cache??)/number of records showing in Access? Thank you.
by: fumanchu | last post by:
I've got to let end users (really just one person) load billing batch files into a third party app table. They need to specify the billing cycle name, the batch name, and the input file name and then I can use these values to execute a SQL Server stored procedure to load them into the batch table from the input file (the stored procedure uses...
by: Alexander Bosch | last post by:
Hi, I'm having a problem similar to the one that's stated in this KB http://support.microsoft.com/default.aspx?scid=kb;en-us;839521 When I'm posting a page to itself with the bool value as true it falls into an infinite loop and later a StackOverflow Exception. I need to do this and not a Response.Redirect or a transfer with the bool in...
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL server back-end. I'm doing my best to learn about SQL server, and I plan to leave the front-end more or less as-is, just linking to the SQL server...
by: Brad | last post by:
I have been trying to do a query(view) in Enterprise Manger and Query Analyzer with a timeout result everytime. In EM in happens in about 30 seconds and in QA it happens in about 45 seconds. I am running the query directly on a W2003SBS server with SQL Server 2000 (V 8.00.2039 )running on the server. The message is simply Timeout expired
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.