Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Help with Query
Hi,

I have the following table:

Column 1   Column 2
A            A
A            B
A            C
B            F
B            F
C            G
C            A

I need a query to return the following:
A     null
B      F
C     null

How can I do that?

Thanks,
ALex


Report this thread to moderator Post Follow-up to this message
Old Post
Masterpop
10-27-05 02:24 PM


Re: Help with Query
Why did you return an "F" for one row and NULL for the others? I'm
guessing that you only want to return a value in the second column
where that value is the same for every instance of the value in the
first column. I'm also guessing that both columns are not nullable in
your table (because you didn't specify any other columns that could be
a key). If that's correct, try this:

SELECT col1,
CASE WHEN MIN(col2)=MAX(col2) THEN MIN(col2) END AS col2
FROM your_table
GROUP BY col1;

(untested)

--
David Portas
SQL Server MVP
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
10-27-05 02:24 PM


Re: Help with Query
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.  It is very hard to debug code when you do not let us
see it.

After that ABSOLUTELY BASIC PIECE OF NETIQUETTE, did you read what you
posted?  What the @!$% specs for this??    Wait, wait, let me read your
mind and guess that you want to see only those rows whose col_1 groups
have one and only one value for col_2 !!

SELECT F1.col1, F2.col2
FROM Foobar AS F1
LEFT OUTER JOIN
(SELECT  col1, MIN(col2)
FROM Foobar
GROUP BY col1
HAVING MIN(col2) = MAX(col2))
AS F2 (col1, col2)
ON F1.col1 = F2.col1;

If you plan to stay in this trade, please learn minimal Netiquette and
how to write a minimal specification.


Report this thread to moderator Post Follow-up to this message
Old Post
--CELKO--
10-27-05 02:24 PM


Re: Help with Query
--CELKO--  (jcelko212@earthlink
.net)  writes:
> After that ABSOLUTELY BASIC PIECE OF NETIQUETTE, did you read what you
> posted?  What the @!$% specs for this??    Wait, wait, let me read your
> mind and guess that you want to see only those rows whose col_1 groups
> have one and only one value for col_2 !!
>...
> If you plan to stay in this trade, please learn minimal Netiquette and
> how to write a minimal specification.

No, this is not what minimal nettiquette is about. Although, including
CREATE TABLE is nice, it's mainly a good idea, because it helps to
get a good answer.

But minimal and basic nettiquette is about something else: that is about
being polite and friendly. Something you are miserable poor in.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
10-27-05 02:24 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 01:15 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006