Solved

SQL DISTINCT not working

Posted on 2013-12-05
8
304 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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
 

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 32

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

778 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