473,396 Members | 1,963 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,396 software developers and data experts.

Beginner's Question on SQL-DISTINCT

Hi.

I did this
SELECT BOTTLEA, BOTTLEB, BOTTLEC FROM MYPRODUCTS

But, I would like to have the result returned only for distinct
BOTTLEB. BOTTLEA and C can be anything. But the result that I get from
the statement must have BOTTLEB as distinct. How do I do that?

I tried

SELECT DISTINCT BOTTLEA, BOTTLEB, BOTTLEC FROM MYPRODUCTS

The result returned include duplicated BOTTLEB. I'm stuck. Appreciate
any help. Thanks in advance!
Jul 20 '05 #1
4 4516
SELECT MAX(BOTTLEA), BOTTLEB, MAX(BOTTLEC) FROM MYPRODUCTS
GROUP BY BOTTLEB

(MAX being a subset of "anything" ;-)
If SQL Server allows TOP in subqueries you could also do a correlated join:
SELECT ANY.BOTTLEA, DIST.BOTTLEB, ANY.BOTTLEC
FROM (SELECT DISTINCT BOTTLEB FROM MYPRODUCTS) AS DIST,
(SELECT TOP 1 BOTTLEA, BOTTLEB, BOTTLEC FROM MYPRODUCTS
WHERE BOTTLEB = DIST.BOTTLEB) AS ANY

(hope I got the TOP syntax right)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Jul 20 '05 #2
>> I tried > SELECT DISTINCT BOTTLEA, BOTTLEB, BOTTLEC FROM
MYPRODUCTS; The result returned include duplicated BOTTLEB. <<

The SELECT DISTINCT clause works on whole rows and removes redundant
duplicates; it has nothing to do with columns.

Do a GROUP BY on Bootle_B and some aggregatge function on the other
two.
Jul 20 '05 #3
Thanks for the pointer. It works!

But if I want to include a requirement that says where BOTTLEB >= 20
dollars, it doesnt seem to work.

Doesnt work->
SELECT MAX(BOTTLEA), BOTTLEB, MAX(BOTTLEC) FROM MYPRODUCTS GROUP BY
BOTTLEB WHERE BOTTLEB>= 20

Any advise please? Thank you once again!

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<cb**********@hanover.torolab.ibm.com>...
SELECT MAX(BOTTLEA), BOTTLEB, MAX(BOTTLEC) FROM MYPRODUCTS
GROUP BY BOTTLEB

(MAX being a subset of "anything" ;-)
If SQL Server allows TOP in subqueries you could also do a correlated join:
SELECT ANY.BOTTLEA, DIST.BOTTLEB, ANY.BOTTLEC
FROM (SELECT DISTINCT BOTTLEB FROM MYPRODUCTS) AS DIST,
(SELECT TOP 1 BOTTLEA, BOTTLEB, BOTTLEC FROM MYPRODUCTS
WHERE BOTTLEB = DIST.BOTTLEB) AS ANY

(hope I got the TOP syntax right)

Cheers
Serge

Jul 20 '05 #4
Never mind about my last message.
I just have to reverse the order and it works again.

SELECT MAX(BOTTLEA), BOTTLEB, MAX(BOTTLEC) FROM MYPRODUCTS WHERE
BOTTLEB>= 20 GROUP BY BOTTLEB

Thanks!!!

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<cb**********@hanover.torolab.ibm.com>...
SELECT MAX(BOTTLEA), BOTTLEB, MAX(BOTTLEC) FROM MYPRODUCTS
GROUP BY BOTTLEB

(MAX being a subset of "anything" ;-)
If SQL Server allows TOP in subqueries you could also do a correlated join:
SELECT ANY.BOTTLEA, DIST.BOTTLEB, ANY.BOTTLEC
FROM (SELECT DISTINCT BOTTLEB FROM MYPRODUCTS) AS DIST,
(SELECT TOP 1 BOTTLEA, BOTTLEB, BOTTLEC FROM MYPRODUCTS
WHERE BOTTLEB = DIST.BOTTLEB) AS ANY

(hope I got the TOP syntax right)

Cheers
Serge

Jul 20 '05 #5

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

Similar topics

10
by: JAK | last post by:
Hi, I'm finishing up a beginning SQL class where we learned on an Oracle database and the transition to working on SQL Server is easy. The next more advanced course will be in PL/SQL, but I know...
4
by: ljubo lecic via AccessMonster.com | last post by:
I am an experienced ORACLE software developer who met ACCESS few months ago and I must say that ,so far, I am delighted. I rapidly developed a few aplications and everything is working extremly...
2
by: Andrzej Magdziarz | last post by:
Hello, (sorry for my English...) Could you help me with a SQL Server 2005 problem? I had installed SQL Server 2005 and then I tried to setup some application using SQL Server. Unfortunatelly...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
2
by: Riaaaa | last post by:
I m beginner for ASP.Net 2005 with C# and SQL Server 2005. I have created the form which contains many asp standard controls on it like label,textbox,checkbox,dropdownlist,command button. The...
2
by: Matt | last post by:
I've coded using VBA on top of MS Excel for the last 2 years (40 hours a week). I've stepped up to VS C# 2005 Express / SQL Server 2005 Express a couple months ago, but the videos that I downloaded...
3
by: milton | last post by:
hi how to navigate through the records in sql DB programatically...?
2
by: malcster2 | last post by:
hello, i am a beginner to ajax. i have created a mysql database, which i would like to access from a web page. i have created 3 files, a html to display the data, a php file to extract the data,...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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.