Solved

Trying to pull distinct IDs

Posted on 2008-10-07
3
178 Views
Last Modified: 2010-03-19
I am trying to pull a list of distinct IDs based on maximum ContEnd_Switch date. If I change the ContEnd_Switch in the select statement to a MAX(ContEnd_Switch) and then take out the ContEnd_Switch in the Group by clause, then it won't work because I need a group by because I have ContEnd_Switch in the Order by clause. If I add Max(ContEnd_Switch) to the Having clause, for example Having Max(ContEnd_Switch) = ContEnd_Switch, then this will not work either because there are rows in the data with null ContEnd_Switch dates. Please help!
SELECT ProspectID, Prospect, ContEnd_Switch, TargetPrice, SUM(MwhAct * Total) / SUM(MwhAct) AS WAverage, 
                                GM_MarRate, Comm_1_Rate, Comm_2_Rate, Comm_3_Rate, Comm_4_Rate, Comm_5_Rate, Comm_6_Rate 
                FROM Prospect_Cogs 
                WHERE (Fwd_Mth BETWEEN 1 AND 12) and SRID = 176 and (Realtime in (select Realtime from tbl_realtime) ) 
                GROUP BY ProspectID, Prospect, TargetPrice, ContEnd_Switch, GM_MarRate, Comm_1_Rate, Comm_2_Rate, Comm_3_Rate, Comm_4_Rate, Comm_5_Rate, Comm_6_Rate 
                having SUM(MwhAct * Total) / SUM(MwhAct) > 0
                order by CASE 0
                                          when 1 then cast(ContEnd_Switch as varchar)  -- by end contract  
                                          when 2 then cast(TargetPrice as varchar)   -- Target Price 
                                          when 3 then cast(SUM(MwhAct * Total) / SUM(MwhAct) as varchar) --by quoted Price
                                          else Prospect    --by Prospect Name 
                                 END

Open in new window

0
Comment
Question by:utlonghornjulie
  • 2
3 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22661889
i don't understand your query but i think the row_number() function can help you here
0
 
LVL 3

Author Comment

by:utlonghornjulie
ID: 22662199
I am trying to pull a list of distinct ProspectIDs. For the duplicate ProspectIDs, then I want the record that has the maximum ContEnd_Switch date.
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22662438
so you need

with a as (
select *, row_number() over (partition by ProspectIDs order by ContEnd_Switch desc) as rown
from your_table)

select * from a where rown = 1


the partition by is like group by
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set the max value for a column 7 40
INSERT DATE FROM STRING COLUMN 18 59
Query group by data in SQL Server - cursor? 3 48
Need help in debugging a UDF results 7 25
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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