[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

SQL DISTINCT not working

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
Medrise
Asked:
Medrise
4 Solutions
 
MedriseAuthor Commented:
I need to group all fpaCode
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
MedriseAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
MedriseAuthor Commented:
I can return ROWNUMBER 4 that would be fine...
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
awking00Commented:
Can you provide some sample data and your expected output?
0
 
PortletPaulCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now