Solved

SQL DISTINCT not working

Posted on 2013-12-05
8
301 Views
Last Modified: 2014-01-30
I have a query that I'm added ROW_NUMBER() to it, but when I try to add DISTINCT or GROUP BY it does not work.

Here is the query.

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY fpacode) AS Rownumber, fpaCode, listname, fpaID, emailTo, status, cfid FROM fpaBasket WHERE cfid = 'D1W12978461') AS Sample WHERE Rownumber < 11

Open in new window

0
Comment
Question by:Medrise
8 Comments
 

Author Comment

by:Medrise
ID: 39698241
I need to group all fpaCode
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 375 total points
ID: 39698249
ROWNUMBER adds a 1, 2, 3, ... to all rows.

Which means, the rows would by definition all become DISTINCT, which defeats the purpose of having DISTINCT in your query.

>I need to group all fpaCode
So if a specific ROWNUNBER has 4, 5, and 6 for fpaCode = 1, then which ROWNUMBER do you want returned in the single row for fpaCode = 1?
0
 

Author Comment

by:Medrise
ID: 39698266
I want to add the ROW_NUMBER to this query, so should do a SELECT with ROW_NUMBER() with this query?

SELECT DISTINCT(fpaCode), listname, fpaID, emailTo, status, cfid FROM fpaBasket WHERE cfid = 'D1W12978461'

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 375 total points
ID: 39698291
Please answer this question:
>I need to group all fpaCode
So if a specific ROWNUNBER has 4, 5, and 6 for fpaCode = 1, then which ROWNUMBER do you want returned in the single row for fpaCode = 1?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Medrise
ID: 39698350
I can return ROWNUMBER 4 that would be fine...
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 375 total points
ID: 39698473
So to clarify, you want to return the minimum ROWNUMBER value for a given grouping of fpaCode?

Example:

fpaCode  rownumber
1              1                  --say there are three rows with fpaCode = 1, but only one row is displayed here
2              4                  --say there are 13 rows with fpaCode = 2, but only one row is displayed here
3              17
0
 
LVL 31

Expert Comment

by:awking00
ID: 39698558
Can you provide some sample data and your expected output?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 125 total points
ID: 39700339
If you want to "group all fpaCode" why are you using row_number()?
Do you mean something like this perhaps?
This would give you one row per fpaCode by using "partition by fpaCode"
SELECT TOP 10
      *
FROM (
            SELECT
                  ROW_NUMBER() OVER (PARTITION BY fpaCode ORDER BY cfid) AS Rownumber
                , fpaCode
                , listname
                , fpaID
                , emailTo
                , status
                , cfid
            FROM fpaBasket
            WHERE cfid = 'D1W12978461'
      ) AS Sample
WHERE Rownumber = 1

Open in new window

Note while I have chosen to "order by cfid" but that is purely an arbitrary choice.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now