[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL DISTINCT not working

Posted on 2013-12-05
8
Medium Priority
?
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

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

Assisted Solution

by:Jim Horn
Jim Horn earned 1500 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1500 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1500 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 49

Accepted Solution

by:
PortletPaul earned 500 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

649 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