473,511 Members | 16,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How could I do that (Query)

Hello all,

I'm using SS 2000

Based on the following query, how could I get only one row for each
different field "F1"? I don't want to use temp table.

----------
SELECT ???
FROM (
SELECT 'A' AS F1, 1 AS F2, 10 AS F3
UNION ALL
SELECT 'B', 2, 12
UNION ALL
SELECT 'B', 3, 11
UNION ALL
SELECT 'A', 4, 10) T
----------

One of the possible answer could be:

F1 F2 F3
---- --------- -----------
B 3 11 /* One row for "B" */
A 4 10 /* One row for "A" */

TIA

Yannick
Jul 20 '05 #1
7 2218
Your question is not clear. Please tell us the logic you want to follow. How
do you define the value of F2 and F3?

Shervin

"Yannick Turgeon" <no****@nowhere.com> wrote in message
news:N%*********************@news20.bellglobal.com ...
Hello all,

I'm using SS 2000

Based on the following query, how could I get only one row for each
different field "F1"? I don't want to use temp table.

----------
SELECT ???
FROM (
SELECT 'A' AS F1, 1 AS F2, 10 AS F3
UNION ALL
SELECT 'B', 2, 12
UNION ALL
SELECT 'B', 3, 11
UNION ALL
SELECT 'A', 4, 10) T
----------

One of the possible answer could be:

F1 F2 F3
---- --------- -----------
B 3 11 /* One row for "B" */
A 4 10 /* One row for "A" */

TIA

Yannick

Jul 20 '05 #2
Do you mean taht this query represents some data in a table?

If so, and assuming that (f1,f2) is unique:

SELECT T.*
FROM
(SELECT f1, MAX(f2) AS f2
FROM T
GROUP BY f1) AS X
JOIN T
ON T.f1 = X.f1 AND T.f2 = X.f2

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #3
Yannick Turgeon (no****@nowhere.com) writes:
I'm using SS 2000

Based on the following query, how could I get only one row for each
different field "F1"? I don't want to use temp table.

----------
SELECT ???
FROM (
SELECT 'A' AS F1, 1 AS F2, 10 AS F3
UNION ALL
SELECT 'B', 2, 12
UNION ALL
SELECT 'B', 3, 11
UNION ALL
SELECT 'A', 4, 10) T
----------


This could do it:

SELECT F1, MIN(F2), MIN(F3)
FROM (
SELECT 'A' AS F1, 1 AS F2, 10 AS F3
UNION ALL
SELECT 'B', 2, 12
UNION ALL
SELECT 'B', 3, 11
UNION ALL
SELECT 'A', 4, 10) T
GROUP BY F1

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
In my real situation, the SELECT ... UNION clause is replaced by a SELECT sub
query which return rows without unique key. Two rows could be exactly
the same. It does not matter which row is returned for a F1 but I want one
and only one row for each F1, and F2 and F3 must be from the same row.
This exclude "SELECT F1, MIN(F2), MIN(F3) ..."

I hope is clearer. Thanks for your help.

Yannick
Le Fri, 26 Sep 2003 14:17:09 -0700, Shervin Shapourian a écrit*:
Your question is not clear. Please tell us the logic you want to follow. How
do you define the value of F2 and F3?

Shervin

Jul 20 '05 #5
Davis,

I haven't been clear enough. T is a subquery which return rows without
unique key. Two rows could be exactly the same.

Thanks for your time.

Yannick
Le Fri, 26 Sep 2003 22:22:33 +0100, David Portas a écrit*:
Do you mean taht this query represents some data in a table?

If so, and assuming that (f1,f2) is unique:

SELECT T.*
FROM
(SELECT f1, MAX(f2) AS f2
FROM T
GROUP BY f1) AS X
JOIN T
ON T.f1 = X.f1 AND T.f2 = X.f2

--
David Portas
------------
Please reply only to the newsgroup


Jul 20 '05 #6
> I haven't been clear enough. T is a subquery

Then post the actual subquery, the DDL for the base tables and some sample
data as INSERT statements. Without that it's difficult to give a full
answer.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #7
OK, assuming F2 and F3 are integer values less than 10,000,000,000 this qury
returns what you want. SF2 and SF3 columns of the result set are string
fields, you can convert them back to integer values if you want.

select f1,
left(max(convert(char(10),f2) + convert(char(10),f3)), 10) as SF2,
right(max(convert(char(10),f2) + convert(char(10),f3)), 10) as SF3
from YourSubQuery
group by f1

Shervin

"Yannick Turgeon" <no****@nowhere.com> wrote in message
news:pa****************************@nowhere.com...
In my real situation, the SELECT ... UNION clause is replaced by a SELECT sub query which return rows without unique key. Two rows could be exactly
the same. It does not matter which row is returned for a F1 but I want one
and only one row for each F1, and F2 and F3 must be from the same row.
This exclude "SELECT F1, MIN(F2), MIN(F3) ..."

I hope is clearer. Thanks for your help.

Yannick
Le Fri, 26 Sep 2003 14:17:09 -0700, Shervin Shapourian a écrit :
Your question is not clear. Please tell us the logic you want to follow. How do you define the value of F2 and F3?

Shervin

Jul 20 '05 #8

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

Similar topics

3
5379
by: joemyre | last post by:
Hi everyone, What I'm trying to do is take php variables i got from user input, and pass them as the MySQL query terms. $query = "select * from ident where ".$searchtype1."=".$searchterm1."";...
2
2321
by: James Perry | last post by:
Hi, I have been trying to build an custom report interface for a charity manangement system; which is part of my dissertation. This interface will allow the chairty to input a SQL query and...
8
8204
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
2
5771
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i...
3
4565
by: Tom Mitchell | last post by:
All: I'm stumped on a query. How do I find duplicates in a table where one of the duplicates has values is a certain field and the other doesn't. For example, I have the following table: ...
1
1518
by: Greg Daniels | last post by:
I have the following table with fourfields: Inc Length Width Cost 1 5 5 2.10 2 5 10 4.20 3 5 5 1.70 4 5 5 1.10 5 5 10 2.10 I need to get to this: Inc Length Width Cost
3
1494
by: Steve Housechild | last post by:
I have a table of 'Customers', a table of 'Newsletters' and a table for storing which customers have recieved which letters 'CustLett'. By using a query, I have pulled a list of customers who...
4
11315
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
5
5566
by: ????? | last post by:
I have an access query which gets data from a number of different tables. Although the tables have primary key fields, the order in which the records are returned means that none of these are in...
4
2501
by: torontolancer | last post by:
Hi there how r u .I would really appriciate ur concern regarding checking out this code. its beind a command button and i am have a combo box selecing a query criteria and then pressing the button...
0
7138
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
7355
Oralloy
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,...
0
7423
jinu1996
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...
1
7081
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
5668
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,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1576
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
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...

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.