Solved

Trying to pull a list of distinct IDs

Posted on 2008-10-07
3
266 Views
Last Modified: 2010-03-19
I am trying to pull a list of distinct IDs. This list should contain a list of unique ProspectIDs. Currently the select statement pulls a list that contains duplicate ProspectIDs. For the duplicate ProspectIDs pulled, I want to only pull the ProspectID with the maximum ContEnd_Switch date. 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 = 1 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
[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
3 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 22659882
Does this do it?

SELECT ProspectID, Prospect, Max(ContEnd_Switch) as Max_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 = 1 and (Realtime in (select Realtime from tbl_realtime) ) 
                GROUP BY ProspectID, Prospect, TargetPrice, 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
 
LVL 3

Author Comment

by:utlonghornjulie
ID: 22659913
Yes it worked. Thanks!
0
 
LVL 10

Expert Comment

by:dwe761
ID: 22659926
Without a sample of your data, it's difficult to give a complete solution.  But I can offer suggestions.   Have you tried adding ContEnd_Switch  to the Having clause?  Such as below.
If this doesn't work, you may need to do it in multiple steps because you may also get duplication if there are other fields causing a problem.  You can either create a temp table or do a sub-select to pull out the duplicates and then complete your other aggregation.  
But try the simple solution first.

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 = 1 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 MAX(ContEnd_Switch) = ContEnd_Switch AND 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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i get data when i use where clause with group by? 3 50
Help  needed 3 54
Loops and updating in SQL Query 9 72
SSRS 2013 - Finding Datasets/StoredProcedures 4 54
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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