• 1

[SQL] Select multiple columns with only one distinct column


Question

How do you select multiple columns from a table while ensuring that one specific column doesn't contain duplicate values?

SELECT DISTINCT col1,col2,col3,col4 from table

This doesn't work, because DISTINCT here applies to all columns so columns as a whole are distinct. But I want a single column to have distinct values.

I'm using SQL Server. :)

6 answers to this question

Recommended Posts

  • 0

You're looking for GROUP BY. If you wanted to only have the value for col1 show up once in the dataset:

SELECT col1,col2,col3,col4 from table GROUP BY col1

Note that this will compact all the results that would have been display into one row. So you'll end up having to use sums or some other aggregate function to display the correct values if you can expect a different value in a row each time it shows up. Otherwise it will just pick the value from the first row.

  • 0

Thanks for your answer, it actually does serve my purpose. :)

This is the query I ran:

SELECT col1,max(col2), max(col3), max(col4) from table GROUP BY col1

Max functions used here don't mean much, they'er just there to pick the biggest value of the column in the row set.

As I said, in this case it serves my purpose. But what if there were, let's say 4 or more rows with a common col1 value, and you wanted to retrieve columns of some specific middle row among the grouped rows; How would you go about that?

I'm thinking cursors can be one way to do that, right?

  • 0
  On 07/02/2011 at 19:32, Code149 said:

Thanks for your answer, it actually does serve my purpose. :)

This is the query I ran:

SELECT col1,max(col2), max(col3), max(col4) from table GROUP BY col1

Max functions used here don't mean much, they'er just there to pick the biggest value of the column in the row set.

As I said, in this case it serves my purpose. But what if there were, let's say 4 or more rows with a common col1 value, and you wanted to retrieve columns of some specific middle row among the grouped rows; How would you go about that?

I'm thinking cursors can be one way to do that, right?

I believe you could (SQL Server is not my strongest database skill), but if you can it would be better if you can weed out the row you want using the WHERE clause of the SQL statement.

  • 0

It sounds like you need to do something like the following:

select
* --should specify your specific columns
from table1 t1
inner join 
(select DISTINCT distinctColumn from table) t2
on t1.col = t2.distinctColumn

This code should get your distinct column and then join on it from another (or even the same) table.

  • 0
  On 13/02/2011 at 00:05, spartyjohnson said:

It sounds like you need to do something like the following:

select
* --should specify your specific columns
from table1 t1
inner join 
(select DISTINCT distinctColumn from table) t2
on t1.col = t2.distinctColumn

This code should get your distinct column and then join on it from another (or even the same) table.

No, doesn't work. It returns duplicate rows.

It doesn't matter tho, as my problem is solved, even ignoring the @kjordan2001's answer. I just needed to omit those non-duplicate valued columns from the select list of a distinct clause, which were trivial anyways. The result set would then only include entirely distinct rows. And FYI, the table in question is named Customer and is part of the AdventureWorks2008R2 sample databases.

Thanks for answers though. :)

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Get this 27-inch ASUS VA279QG 120Hz monitor for dirt-cheap by Taras Buria If you are on a very tight budget but you still want to upgrade your monitor to something more exciting than a standard 60Hz office monitor, ASUS has a perfect deal for you. The VA279QG is currently available for as little as $109, and for this money, you get quite a lot of a monitor. The ASUS VA279QG is a big 27-inch IPS monitor with a classic FullHD resolution and a wide 178-degree viewing angle. It can operate with a refresh rate of 120Hz, which is more than enough for buttery-smooth gaming. And since the monitor is FullHD, you will be able to see high refresh rates in more games since your GPU will have to render fewer pixels at 120Hz. Besides, the monitor supports variable refresh rate (VRR), a feature that can further reduce stutters by dynamically adjusting the refresh rate to your FPS. Other display specs include a 1ms MPRT response time, 16.7 million colors, 99% sRGB coverage, and a typical brightness of 300 nits. It is also covered with an anti-glare coating to reduce reflections. Ports-wise, you get one DisplayPort 1.2, one HDMI 1.4, one VGA, and one headphone jack. There are also two 2W speakers, but set your expectation right—these are unlikely to blow your mind with high-quality audio. Finally, there is a VESA 100 mount and a cutout in the base, which lets you mount your phone, namecard, or other small items for extra convenience. 27-inch ASUS VA279QG 120Hz IPS Gaming Monitor - $109 | 22% off on Amazon US This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • lol See... one is only 100% when it's by itself. I live for exceptions to the rule... our talk point was in reference in comparing Vista (not SP1/2) release vs. 7. Any OS will run stable once enough work has been put into it... hell, even Windows 95 had six versions before she was finally complete(d)... just about, what, six or seven months before 98 became available?
    • 3uTools 3.26.007 by Razvan Serea 3uTools is a powerful iOS management tool that allows users to efficiently handle their iPhone or iPad without relying on iTunes. It offers seamless data transfer, one-click backup and restore, firmware updates, and a built-in file explorer for easy access to system files. Users can also manage apps, contacts, and media, including photos, videos, and ringtones, all within a simple and intuitive interface. For optimization and maintenance, 3uTools provides battery health monitoring, real-time device information, and system cleanup tools. It also includes features like screen recording, video conversion, and iCloud backup management, making it an essential tool for iOS users looking for greater control over their devices. Key features of 3uTools: iOS device management App installation and removal Firmware downloads and updates Backup and restore functions Data transfer between iOS and PC Ringtone creation Custom wallpaper management Device flashing File system explorer Battery health monitoring Screen capture and recording Video and photo management iCloud backup support Flashing and restoring iOS without data loss 3uTools 3.26.007 changelog: Easy Flash supports iOS 26.0 flashing. Optimized Virtual Location. Fixed some known bugs. Download: 3uTools 3.26.007 | 186.0 MB (Freeware) Download: 3uTools 32-bit | 192.0 MB View: 3uTools Home Page | macOS | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The development time of this Operating System will have competition with the development time of Star Citizen 😂
    • I saw the 300 in the image and thought it was the number of cores! 🤣
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      654
    2. 2
      ATLien_0
      271
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!