<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ä¸­å›½ç½‘ç»œ(86SQL.COM)ä¼ä¸šé‚®ä»¶ç³»ç»Ÿ &#187; select distinct</title>
	<atom:link href="http://subeijihua.org/category/select-distinct/feed" rel="self" type="application/rss+xml" />
	<link>http://subeijihua.org</link>
	<description></description>
	<lastBuildDate>Fri, 16 Apr 2010 22:31:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQL programming: how to combine these two statements?</title>
		<link>http://subeijihua.org/select-distinct/sql-programming-how-to-combine-these-two-statements</link>
		<comments>http://subeijihua.org/select-distinct/sql-programming-how-to-combine-these-two-statements#comments</comments>
		<pubDate>Mon, 18 Jan 2010 02:18:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/sql-programming-how-to-combine-these-two-statements</guid>
		<description><![CDATA[How can I combine
select distinct ids from studenttable
&#8230;&#8230;&#8230;&#8230;&#8230;then for each id&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;
    select top 1 * from studenttable where id = @id order by timestamp desc
&#8230;&#8230;into one T-SQL statement (IE without using loops/queues)?
BTW I meant cursors not queues
Give this a try:
SELECT a.* FROM studenttable a,
(SELECT id, MAX(timestamp) FROM studenttable GROUP BY id) b
WHERE [...]]]></description>
			<content:encoded><![CDATA[<p>How can I combine</p>
<p>select distinct ids from studenttable<br />
&#8230;&#8230;&#8230;&#8230;&#8230;then for each id&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
    select top 1 * from studenttable where id = @id order by timestamp desc</p>
<p>&#8230;&#8230;into one T-SQL statement (IE without using loops/queues)?<br />
BTW I meant cursors not queues<br />
<br />Give this a try:</p>
<p>SELECT a.* FROM studenttable a,<br />
(SELECT id, MAX(timestamp) FROM studenttable GROUP BY id) b<br />
WHERE a.id = b.id and a.timestamp = b.timestamp</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/sql-programming-how-to-combine-these-two-statements/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Question for SQL gurus: how can I select distinct and sort by number of duplicates?</title>
		<link>http://subeijihua.org/select-distinct/question-for-sql-gurus-how-can-i-select-distinct-and-sort-by-number-of-duplicates</link>
		<comments>http://subeijihua.org/select-distinct/question-for-sql-gurus-how-can-i-select-distinct-and-sort-by-number-of-duplicates#comments</comments>
		<pubDate>Wed, 30 Dec 2009 00:21:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/question-for-sql-gurus-how-can-i-select-distinct-and-sort-by-number-of-duplicates</guid>
		<description><![CDATA[I have a table with 3 fields: zip, city, state. One row for every zip code in the US, it contains about 39,000 rows.
I want to search by city name alone. Some cities, such as &#34;Rockford&#34;, will return many rows, in several different states.
If I do &#34;SELECT * FROM ziplist WHERE city = &#8216;Rockford&#8217;&#34;, I [...]]]></description>
			<content:encoded><![CDATA[<p>I have a table with 3 fields: zip, city, state. One row for every zip code in the US, it contains about 39,000 rows.</p>
<p>I want to search by city name alone. Some cities, such as &quot;Rockford&quot;, will return many rows, in several different states.</p>
<p>If I do &quot;SELECT * FROM ziplist WHERE city = &#8216;Rockford&#8217;&quot;, I might get:</p>
<p>zip &#8211; city &#8211; state<br />
=================<br />
00123 Rockford MI<br />
00124 Rockford MI<br />
00125 Rockford MI<br />
60109 Rockford IL<br />
60110 Rockford IL<br />
60111 Rockford IL<br />
60112 Rockford IL<br />
91832 Rockford CA</p>
<p>3 Rockfords in MI, 4 Rockfords in IL, 1 Rockford in CA.<br />
I need a query that will return a table that looks like this:</p>
<p>city &#8211; state<br />
=================<br />
Rockford IL<br />
Rockford MI<br />
Rockford CA</p>
<p>So that a user can choose which state he meant to search for, with the most probable result at the top. Very simple; distinct state, sorted by the number of times that state was repeated.</p>
<p>I&#8217;m using MySQL Server 4.1.13a.<br />
gman: I think you meant &quot;GROUP BY state&quot;,<br />
otherwise it works great! Thanks!<br />
<br />I&#8217;m not sure I count as a SQL guru, but I have something you could try. </p>
<p>SELECT state, count(city) as cityCount<br />
FROM ziplist<br />
WHERE city=&#8217;Rockford&#8217;<br />
GROUP BY state &#8212; Of course!<br />
ORDER BY cityCount DESC</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/question-for-sql-gurus-how-can-i-select-distinct-and-sort-by-number-of-duplicates/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SQL help please. Thanks.?</title>
		<link>http://subeijihua.org/select-distinct/sql-help-please-thanks</link>
		<comments>http://subeijihua.org/select-distinct/sql-help-please-thanks#comments</comments>
		<pubDate>Thu, 24 Dec 2009 16:46:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/sql-help-please-thanks</guid>
		<description><![CDATA[Hi, I have the query 
select distinct programcode, dept from studentaffairsview where termcode = &#8216;200900&#8242;;
I want to only show results where the programcode appears more than once. Can this be done?
Thanks
This&#8217;ll do it.
Select Count(programcode) as Instances, programcode from studentaffairsview
Where termcode = &#8216;200900&#8242;
Group by programcode
Having Count(programcode) &#62; 1
]]></description>
			<content:encoded><![CDATA[<p>Hi, I have the query </p>
<p>select distinct programcode, dept from studentaffairsview where termcode = &#8216;200900&#8242;;</p>
<p>I want to only show results where the programcode appears more than once. Can this be done?<br />
Thanks<br />
<br />This&#8217;ll do it.</p>
<p>Select Count(programcode) as Instances, programcode from studentaffairsview<br />
Where termcode = &#8216;200900&#8242;<br />
Group by programcode<br />
Having Count(programcode) &gt; 1</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/sql-help-please-thanks/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Oracle question.  Optimizing this query?</title>
		<link>http://subeijihua.org/select-distinct/oracle-question-optimizing-this-query</link>
		<comments>http://subeijihua.org/select-distinct/oracle-question-optimizing-this-query#comments</comments>
		<pubDate>Mon, 14 Dec 2009 04:39:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/oracle-question-optimizing-this-query</guid>
		<description><![CDATA[I have a query that goes something like this:
select * from table1
   where table1.id in(select distinct id from stuff)
union
select * from table2
   where table2.id in(select distinct id from other_stuff);
This query ran like crap.  To change it, all I did was add a &#34;group by id&#34; to the end of the [...]]]></description>
			<content:encoded><![CDATA[<p>I have a query that goes something like this:</p>
<p>select * from table1<br />
   where table1.id in(select distinct id from stuff)<br />
union<br />
select * from table2<br />
   where table2.id in(select distinct id from other_stuff);</p>
<p>This query ran like crap.  To change it, all I did was add a &quot;group by id&quot; to the end of the the nested queries.  The id field can contain duplicates.  I don&#8217;t understand why the second query runs so much better.  Group by and distinct do practically the same thing and give me the same answer.  What is Oracle doing that makes the first query suck so much?</p>
<p>Below is the form that works great.  Why?</p>
<p>select * from table1<br />
   where table1.id in(select distinct id from stuff group by id)<br />
union<br />
select * from table2<br />
   where table2.id in(select distinct id from other stuff group by id);</p>
<p>Check the query plan on both queries and compare them (or post them here). Maybe that will help explain the difference.</p>
<p>BTW, if the values returned from both branches of the queries are different, then you may also be able to speed up the better performing query by using a UNION ALL instead of a UNION.</p>
<p>UNION removes duplicates (requiring more processing) whereas UNION ALL does not.</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/oracle-question-optimizing-this-query/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL &quot;not in&quot; question?</title>
		<link>http://subeijihua.org/select-distinct/sql-not-in-question</link>
		<comments>http://subeijihua.org/select-distinct/sql-not-in-question#comments</comments>
		<pubDate>Wed, 09 Dec 2009 00:58:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/sql-not-in-question</guid>
		<description><![CDATA[select bednr from bed
where bednr not in (select distinct bednr from patient);
This does not seem to work. Its basicly to see if a bed is in use or not.
select bednr from bed
where bednr not in (select distinct bednr from patient
where bednr is not null);
]]></description>
			<content:encoded><![CDATA[<p>select bednr from bed<br />
where bednr not in (select distinct bednr from patient);</p>
<p>This does not seem to work. Its basicly to see if a bed is in use or not.<br />
<br />select bednr from bed<br />
where bednr not in (select distinct bednr from patient<br />
where bednr is not null);</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/sql-not-in-question/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>convert this sql qry to access qry?</title>
		<link>http://subeijihua.org/select-distinct/convert-this-sql-qry-to-access-qry</link>
		<comments>http://subeijihua.org/select-distinct/convert-this-sql-qry-to-access-qry#comments</comments>
		<pubDate>Sun, 06 Dec 2009 17:12:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/convert-this-sql-qry-to-access-qry</guid>
		<description><![CDATA[select mname,id,name from item_mansup where sid= 488 and id in (select distinct itemid from item_tr where itemid not in(select distinct id from item_mansup where sid = 971)
this qry is not running in access any body help me ?
If this is the *exact* query, you&#8217;re missing a second closing ) after the second embedded select.  [...]]]></description>
			<content:encoded><![CDATA[<p>select mname,id,name from item_mansup where sid= 488 and id in (select distinct itemid from item_tr where itemid not in(select distinct id from item_mansup where sid = 971)</p>
<p>this qry is not running in access any body help me ?<br />
<br />If this is the *exact* query, you&#8217;re missing a second closing ) after the second embedded select.  Other than that, I don&#8217;t see anything wrong with it.  Doing two nested INs might not be the most efficient way to do things, but without knowing your table design, I couldn&#8217;t recommend anything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/convert-this-sql-qry-to-access-qry/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How many different license plate numbers can you have?</title>
		<link>http://subeijihua.org/select-distinct/how-many-different-license-plate-numbers-can-you-have</link>
		<comments>http://subeijihua.org/select-distinct/how-many-different-license-plate-numbers-can-you-have#comments</comments>
		<pubDate>Fri, 04 Dec 2009 03:02:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/how-many-different-license-plate-numbers-can-you-have</guid>
		<description><![CDATA[. Assume an automobile license plate number consists of two letters followed by a three digit number. How many distinct license plate numbers can be formed if
(a) there are no restrictions and
(b) the letters O and I are not used?
(c) What is the probability of selecting at random a license plate that ends in an [...]]]></description>
			<content:encoded><![CDATA[<p>. Assume an automobile license plate number consists of two letters followed by a three digit number. How many distinct license plate numbers can be formed if<br />
(a) there are no restrictions and<br />
(b) the letters O and I are not used?<br />
(c) What is the probability of selecting at random a license plate that ends in an even number?<br />
<br />A)<br />
26^2 times 10^3 = 676,000</p>
<p>B)<br />
24^2 times 10^3 = 576,000</p>
<p>C)<br />
All plates end in a digit 0-9, half of which are even.<br />
You have a 1/2 probability of getting an even one at random.</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/how-many-different-license-plate-numbers-can-you-have/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>help with a SQL script?</title>
		<link>http://subeijihua.org/select-distinct/help-with-a-sql-script</link>
		<comments>http://subeijihua.org/select-distinct/help-with-a-sql-script#comments</comments>
		<pubDate>Sun, 29 Nov 2009 08:51:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/help-with-a-sql-script</guid>
		<description><![CDATA[Hello,
I am very much a beginner with SQL, and I was hoping for some help with a script.
I need to pull records from a table with distinct values for one of the fields and a given value for another field.  I want the results to include all of the fields.
In other words&#8230;I need a [...]]]></description>
			<content:encoded><![CDATA[<p>Hello,</p>
<p>I am very much a beginner with SQL, and I was hoping for some help with a script.</p>
<p>I need to pull records from a table with distinct values for one of the fields and a given value for another field.  I want the results to include all of the fields.</p>
<p>In other words&#8230;I need a combination of:</p>
<p>select * from tableABC where name = &#8216;Scott&#8217;<br />
&amp;<br />
select distinct ProductName from tableABC</p>
<p>Thanks for any help.<br />
<br />Try the following SQL statement: </p>
<p>SELECT DISTINCT tableABC.Name,<br />
tableABC.Product<br />
FROM tableABC<br />
WHERE (((tableABC.Name)=&quot;Scott&quot;));</p>
<p>The DISTINCT should only pull unique values for the Product, but allow every instance of Name = Scott.</p>
<p>This is different than using the DISTINCTROW.</p>
<p>Though I would probably have never created a relational database where these two fields were within the same table.  They would have been two separate tables, connected by a Join on some Unique Key.</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/help-with-a-sql-script/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Who can solve these problems about probability?</title>
		<link>http://subeijihua.org/select-distinct/who-can-solve-these-problems-about-probability</link>
		<comments>http://subeijihua.org/select-distinct/who-can-solve-these-problems-about-probability#comments</comments>
		<pubDate>Tue, 17 Nov 2009 10:47:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/who-can-solve-these-problems-about-probability</guid>
		<description><![CDATA[1.A pair of dice is tossed. Find the probability of getting a) a total of 8? B) at most a total of 5?
2.Two cards are drawn in succession from a deck without replacement. What is the probability that both cards are greater than 2 and less than 8?
3.If each coded item in catalog begins with [...]]]></description>
			<content:encoded><![CDATA[<p>1.A pair of dice is tossed. Find the probability of getting a) a total of 8? B) at most a total of 5?<br />
2.Two cards are drawn in succession from a deck without replacement. What is the probability that both cards are greater than 2 and less than 8?<br />
3.If each coded item in catalog begins with 5 different letters and 3 distinct nonzero digits, find the probability of randomly selection one of these coded items with the first letter a vowel and last digit even?<br />
4.In a high school graduating class of 100 students, 54 studied mathematics, 69 studied history, and 35 studied both mathematics and history. If one of these students is selected at random,  find the probability that the student took history but not mathematics?<br />
5.In a poker hand consisting of  cards, find he probability of holding 3 aces?<br />
6.In a game of poker where a player is dealt 5 cards from a deck of 53 playing cards (52 cards + 1 joker), what is the probability of getting a full house?<br />
<br />E= R/N</p>
<p>E is your target event.<br />
R is the total number of ways that event can be achieved<br />
N is the total number of UNIQUE events that can be achieved</p>
<p>1a) E=8 or 2+6,3+5,4+4  R=3 N=21 NOT 36</p>
<p>21 because: 1+1, 1+2, 1+3, 1+4, 1+5, 1+6 = 6 UNIQUE events<br />
                      2+2, 2+3, 2+4, 2+5, 2+6 = 5 UNIQUE events<br />
                      3+3, 3+4, 3+5, 3+6 = 4 UNIQUE events<br />
                      4+4, 4+5, 4+6 = 3 UNIQUE events<br />
                      5+5, 5+6 = 2 UNIQUE events<br />
                      6+6 = 1 UNIQUE event</p>
<p>                      6 + 5 + 4 + 3 + 2 + 1 = 21 TOTAL Unique events possible</p>
<p>Probability is 1/7 or 0.142857 or 14.2857% for rolling an eight.</p>
<p>1b) By saying &quot;at most a total of 5&quot; the assumption I am making is that totals of 2, 3, 4 &amp; 5 are the events.</p>
<p>E =&lt; 5<br />
R = 6<br />
N = 21</p>
<p>Answer is 6/21 or 0.2857 or 28.57% of being a total of 5 or less.</p>
<p>2)  Assuming no jokers and assuming that 2&#8217;s and below and 8&#8217;s and above are NOT included.</p>
<p>First card:<br />
2 &lt; E &lt; 8<br />
R = 20 = 52 &#8211; (4 A&#8217;s + 4 2&#8217;s + 4 8&#8217;s + 4 9&#8217;s + 4 10&#8217;s + 4 J&#8217;s + 4 Q&#8217;s + 4 K&#8217;s)<br />
     N = 52</p>
<p>Probability: 20/52 or 0.3846 or 38.46%</p>
<p>Second card would have 51 cards to pick from, ergo:</p>
<p>Assuming card 1 met criteria: 19/51 or 0.3725 or 37.25%</p>
<p>If card 1 met criteria: 0.3846 * 0.3725 = 0.14326 or 14.326% chance of getting two consecutive cards that met the conditions.</p>
<p>3)  Starting with AAAAA000 and going through to ZZZZZ999 there are a possible 11,881,376,000 combinations.</p>
<p>26 letters in first position * 26 letters in second position * 26 letters in third position * 26 letters in fourth position * 26 letters in fifth position * 1000 different numbers for each letter set</p>
<p>For R then, there are a possible 1,142,440,000 possible codes that meet the condition of a vowel in the first letter position and an even number in the last letter position.</p>
<p>5 vowels in first position * 26 letters in second position * 26 letters in third position * 26 letters in fourth position * 26 letters in fifth position * 500 numbers that are even for each letter set</p>
<p>Probability: 1,142,440,000 / 11,881,376,000 or 0.09615 or 9.615%</p>
<p>4)  69 students took history of which 35 took math as well.  So, 69 &#8211; 35 = 34 students that took history alone.</p>
<p>E = history alone<br />
R = 34<br />
N = 100</p>
<p>Probability: 34/100 or 0.34 or 34% chance of selecting a student that took history alone.</p>
<p>5 ) bragging rights just aren&#8217;t worth it any more&#8230;. LOL, too many questions!  I surrender, gonna go gaming.</p>
<p>Hope those answers helped out a bit! <img src='http://subeijihua.org/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> )</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/who-can-solve-these-problems-about-probability/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Help with SQL Function?</title>
		<link>http://subeijihua.org/select-distinct/help-with-sql-function</link>
		<comments>http://subeijihua.org/select-distinct/help-with-sql-function#comments</comments>
		<pubDate>Sat, 14 Nov 2009 06:17:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[select distinct]]></category>

		<guid isPermaLink="false">http://subeijihua.org/select-distinct/help-with-sql-function</guid>
		<description><![CDATA[Say, for example, I have a table with 28 items. Each item has a cost and a name. I want each distinct item and a total of the tables cost.
I tried a function but I am getting some errors.
Here is the code:
CREATE FUNCTION dbo.GetPricePerPiece
(
@orderID nvarchar(50)
)
RETURNS float
AS
BEGIN
DECLARE @resultSet table
DECLARE @pricePerPiece float
SELECT @resultSet=SELECT DISTINCT [Name], Cost FROM [...]]]></description>
			<content:encoded><![CDATA[<p>Say, for example, I have a table with 28 items. Each item has a cost and a name. I want each distinct item and a total of the tables cost.</p>
<p>I tried a function but I am getting some errors.</p>
<p>Here is the code:</p>
<p>CREATE FUNCTION dbo.GetPricePerPiece<br />
(<br />
@orderID nvarchar(50)<br />
)<br />
RETURNS float<br />
AS<br />
BEGIN<br />
DECLARE @resultSet table<br />
DECLARE @pricePerPiece float<br />
SELECT @resultSet=SELECT DISTINCT [Name], Cost FROM OperationsInProgress WHERE (OrderID = @orderID) GROUP BY Name, Cost<br />
SELECT @pricePerPiece=Sum(@resultSet.Cost)<br />
RETURN @pricePerPiece<br />
END</p>
<p>It says must declare scalarValue @resultSet. I can&#8217;t declare a table as a scalar value?</p>
<p>Here is the code I tried before I tried to write the function</p>
<p>SELECT DISTINCT Name, Cost<br />
FROM         OperationsInProgress<br />
WHERE     (OrderID = &#8216;61316&#8242;)<br />
GROUP BY Name, Cost</p>
<p>But I can&#8217;t seem to get the Sum of the cost columns.</p>
<p>Any help will be appreciated. Thanks!<br />
I updated my function after looking at some more syntax:</p>
<p>CREATE FUNCTION dbo.GetPricePerPiece<br />
(<br />
@orderID nvarchar(50)<br />
)<br />
RETURNS float<br />
AS<br />
BEGIN<br />
DECLARE @resultSet TABLE<br />
(<br />
[Name] varchar(50),<br />
Cost float<br />
)<br />
DECLARE @pricePerPiece float<br />
INSERT INTO @resultSet ([Name],Cost) SELECT DISTINCT [Name], Cost FROM OperationsInProgress WHERE (OrderID = @orderID) GROUP BY [Name], Cost<br />
SELECT @pricePerPiece=Sum(@resultSet.[Cost])<br />
RETURN @pricePerPiece<br />
END</p>
<p>But i still get the MUST DECLARE SCALAR VARIABLE @resultSet<br />
I tried that, but I get multiple rows of data, and the cost column has a sum of each type.<br />
Dave, that was the correct answer. That and the fact that I forgot to put FROM @resultSet. Thanks for the response.</p>
<p>Well, I learned something new today: triggers and udfs!<br />
<br />I think you have incorrect syntax around the @resultSet table variable.  Try something like this</p>
<p>SELECT @pricePerPiece = SUM(Cost)<br />
FROM @resultSet </p>
<p>instead of </p>
<p>SELECT @pricePerPiece = SUM(@resultSet.Cost)</p>
]]></content:encoded>
			<wfw:commentRss>http://subeijihua.org/select-distinct/help-with-sql-function/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

