473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Server Query (complicated)

Folks,

I have the following data in a table:

4 NULL NULL
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:

4 NULL NULL
2 abc NULL

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

Murali
Jul 20 '05 #1
3 4855
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.

Simon

"Murali" <mu*************@hotmail.com> wrote in message
news:62************************@posting.google.com ...
Folks,

I have the following data in a table:

4 NULL NULL
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:

4 NULL NULL
2 abc NULL

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

Murali

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
1 NULL
1 YYYYY
2 NULL
2 abcd

I want the result to be:

1 xxxxx
2 NULL

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

Murali

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

SELECT *
FROM tbl
WHERE COALESCE(col2, 'aaa') =
(SELECT TOP 1 COALESCE(t1.col2, '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
GROUP BY Pno ;

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

SELECT *
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

1
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...
9
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...
26
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...
15
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? ...
6
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...
11
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...
29
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...
3
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...
2
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
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,...

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.