 




<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Urgent!Please help: TOP 3 cities only returned based on count() &#8211; result</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/</link>
	<description></description>
	<lastBuildDate>Sat, 25 May 2013 17:28:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: mfleming</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49714</link>
		<dc:creator>mfleming</dc:creator>
		<pubDate>Wed, 30 Mar 2005 12:37:55 +0000</pubDate>
		<guid isPermaLink="false">#comment-49714</guid>
		<description><![CDATA[I think I have it for you, tested in DB2 zOS platform. Give this a try:

-- Gives list ranked by Country-City-restaurant count
-- Ordered by country-rank-city
--
  SELECT P1.COUNTRYNAME,P1.CITYNAME
       , P1.REST_CNT
       , P3.REST_CNT AS RANK
    FROM (
          SELECT CO.COUNTRYID
               , CO.COUNTRYNAME
               , CI.CITYID
               , CI.CITYNAME
               , COUNT(R.RESTAURANTID) AS REST_CNT
            FROM RESTAURANT R
            JOIN COUNTRY CO
              ON R.COUNTRYID=CO.COUNTRYID
            JOIN CITY CI
              ON R.CITYID=CI.CITYID
           WHERE R.ISACTIVE=1
           GROUP BY CO.COUNTRYID
               , CO.COUNTRYNAME
               , CI.CITYID
               , CI.CITYNAME
         ) P1
       , TABLE (
          SELECT COUNT(DISTINCT P2.REST_CNT) AS REST_CNT
            FROM (
                  SELECT CO.COUNTRYID
                       , CI.CITYID
                       , COUNT(R.RESTAURANTID) AS REST_CNT
                    FROM RESTAURANT R
                    JOIN COUNTRY CO
                      ON R.COUNTRYID=CO.COUNTRYID
                    JOIN CITY CI
                      ON R.CITYID=CI.CITYID
                   WHERE R.ISACTIVE=1
                   GROUP BY CO.COUNTRYID
                       , CI.CITYID
                 ) P2
           WHERE P2.REST_CNT &gt;= P1.REST_CNT
             AND P2.COUNTRYID = P1.COUNTRYID
               ) AS P3
   WHERE P3.REST_CNT ]]></description>
		<content:encoded><![CDATA[<p>I think I have it for you, tested in DB2 zOS platform. Give this a try:</p>
<p>&#8211; Gives list ranked by Country-City-restaurant count<br />
&#8211; Ordered by country-rank-city<br />
&#8211;<br />
  SELECT P1.COUNTRYNAME,P1.CITYNAME<br />
       , P1.REST_CNT<br />
       , P3.REST_CNT AS RANK<br />
    FROM (<br />
          SELECT CO.COUNTRYID<br />
               , CO.COUNTRYNAME<br />
               , CI.CITYID<br />
               , CI.CITYNAME<br />
               , COUNT(R.RESTAURANTID) AS REST_CNT<br />
            FROM RESTAURANT R<br />
            JOIN COUNTRY CO<br />
              ON R.COUNTRYID=CO.COUNTRYID<br />
            JOIN CITY CI<br />
              ON R.CITYID=CI.CITYID<br />
           WHERE R.ISACTIVE=1<br />
           GROUP BY CO.COUNTRYID<br />
               , CO.COUNTRYNAME<br />
               , CI.CITYID<br />
               , CI.CITYNAME<br />
         ) P1<br />
       , TABLE (<br />
          SELECT COUNT(DISTINCT P2.REST_CNT) AS REST_CNT<br />
            FROM (<br />
                  SELECT CO.COUNTRYID<br />
                       , CI.CITYID<br />
                       , COUNT(R.RESTAURANTID) AS REST_CNT<br />
                    FROM RESTAURANT R<br />
                    JOIN COUNTRY CO<br />
                      ON R.COUNTRYID=CO.COUNTRYID<br />
                    JOIN CITY CI<br />
                      ON R.CITYID=CI.CITYID<br />
                   WHERE R.ISACTIVE=1<br />
                   GROUP BY CO.COUNTRYID<br />
                       , CI.CITYID<br />
                 ) P2<br />
           WHERE P2.REST_CNT &gt;= P1.REST_CNT<br />
             AND P2.COUNTRYID = P1.COUNTRYID<br />
               ) AS P3<br />
   WHERE P3.REST_CNT </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r937</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49715</link>
		<dc:creator>r937</dc:creator>
		<pubDate>Mon, 28 Mar 2005 08:53:42 +0000</pubDate>
		<guid isPermaLink="false">#comment-49715</guid>
		<description><![CDATA[the fact that this discussion board does not allow code formatting is really a huge p.i.t.a.

create view citycounts
as
select co.countryname
     , ci.cityname
     , count(r.restaurantid) as restaurants
  from restaurant r
inner
  join country co 
    on r.countryid=co.countryid
inner
  join city ci 
    on r.cityid=ci.cityid
 where r.isactive=1
group
    by co.countryname
     , ci.cityname 


select countryname
     , cityname
     , restaurants
  from citycounts as x
 where restaurants
    in ( select top 3
                restaurants
           from citycounts
          where countryname = x.countryname )]]></description>
		<content:encoded><![CDATA[<p>the fact that this discussion board does not allow code formatting is really a huge p.i.t.a.</p>
<p>create view citycounts<br />
as<br />
select co.countryname<br />
     , ci.cityname<br />
     , count(r.restaurantid) as restaurants<br />
  from restaurant r<br />
inner<br />
  join country co<br />
    on r.countryid=co.countryid<br />
inner<br />
  join city ci<br />
    on r.cityid=ci.cityid<br />
 where r.isactive=1<br />
group<br />
    by co.countryname<br />
     , ci.cityname </p>
<p>select countryname<br />
     , cityname<br />
     , restaurants<br />
  from citycounts as x<br />
 where restaurants<br />
    in ( select top 3<br />
                restaurants<br />
           from citycounts<br />
          where countryname = x.countryname )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ronjar</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49716</link>
		<dc:creator>ronjar</dc:creator>
		<pubDate>Mon, 28 Mar 2005 08:17:12 +0000</pubDate>
		<guid isPermaLink="false">#comment-49716</guid>
		<description><![CDATA[Hi,

Thanks to you all so far! I have been testing all those queries you have sent except the last one from Julius. At the moment I&#039;m not able to access to the server but I will test it as soon as possibe. Sorry btw for the delay in this answer, whole easter has been busy!

Those queries have not given the correct result but they have also given some ideas on how to proceed. Let&#039;s see how this goes tomorrow!

I use Ms Sql Server btw.

Thanks,

Ronjar

]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Thanks to you all so far! I have been testing all those queries you have sent except the last one from Julius. At the moment I&#8217;m not able to access to the server but I will test it as soon as possibe. Sorry btw for the delay in this answer, whole easter has been busy!</p>
<p>Those queries have not given the correct result but they have also given some ideas on how to proceed. Let&#8217;s see how this goes tomorrow!</p>
<p>I use Ms Sql Server btw.</p>
<p>Thanks,</p>
<p>Ronjar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: julius</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49717</link>
		<dc:creator>julius</dc:creator>
		<pubDate>Thu, 24 Mar 2005 12:39:31 +0000</pubDate>
		<guid isPermaLink="false">#comment-49717</guid>
		<description><![CDATA[If you use MS SQL Server, maybe you can try this:

select co.countryname, ci.cityname, r.restaurantcount
  from city as ci
    inner join (select top 3 countryid, cityid,
                    count(*) as restaurantcount
                  from restaurant
                  where isactive = 1
                  group by countryid, cityid
                  order by count(*) desc) as r
       on ci.cityid = r.cityid
     inner join country as co
       on r.countryid = co.countryid

Hope it&#039;s work.
]]></description>
		<content:encoded><![CDATA[<p>If you use MS SQL Server, maybe you can try this:</p>
<p>select co.countryname, ci.cityname, r.restaurantcount<br />
  from city as ci<br />
    inner join (select top 3 countryid, cityid,<br />
                    count(*) as restaurantcount<br />
                  from restaurant<br />
                  where isactive = 1<br />
                  group by countryid, cityid<br />
                  order by count(*) desc) as r<br />
       on ci.cityid = r.cityid<br />
     inner join country as co<br />
       on r.countryid = co.countryid</p>
<p>Hope it&#8217;s work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r937</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49718</link>
		<dc:creator>r937</dc:creator>
		<pubDate>Thu, 24 Mar 2005 11:26:22 +0000</pubDate>
		<guid isPermaLink="false">#comment-49718</guid>
		<description><![CDATA[there is no WHILE in SQL

;o)

you guys keep forgetting that ronjar did not mention which database he or she is using

therefore, in my opinion (with which you may freely disagree), you should not propose non-standard solutions involving proprietary syntax like TOP or ROWNUM

my solution is standard SQL only]]></description>
		<content:encoded><![CDATA[<p>there is no WHILE in SQL</p>
<p>;o)</p>
<p>you guys keep forgetting that ronjar did not mention which database he or she is using</p>
<p>therefore, in my opinion (with which you may freely disagree), you should not propose non-standard solutions involving proprietary syntax like TOP or ROWNUM</p>
<p>my solution is standard SQL only</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: catalin67</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49719</link>
		<dc:creator>catalin67</dc:creator>
		<pubDate>Thu, 24 Mar 2005 11:00:18 +0000</pubDate>
		<guid isPermaLink="false">#comment-49719</guid>
		<description><![CDATA[From what I see you need to:
- count the restaurants for each city
- for each country display first top 3 cities

You should use a WHILE statement or an EXIST clause to force a nested query (to get top 3 cities) to run for each country you want.]]></description>
		<content:encoded><![CDATA[<p>From what I see you need to:<br />
- count the restaurants for each city<br />
- for each country display first top 3 cities</p>
<p>You should use a WHILE statement or an EXIST clause to force a nested query (to get top 3 cities) to run for each country you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stevewaltz</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49720</link>
		<dc:creator>stevewaltz</dc:creator>
		<pubDate>Wed, 23 Mar 2005 19:49:22 +0000</pubDate>
		<guid isPermaLink="false">#comment-49720</guid>
		<description><![CDATA[I hope TOP worked for you. If not, I have a variation on r937&#039;s solution that works with a similar situation I have in one of my Oracle databases. That is, if SQL server has something like Oracle&#039;s rownum psuedocolumn (returns a sequential number starting with 1 for each row of a cursor). If so, regular_registration, county and location_code are analogous to your restaurant, country and city tables. The advantage is that it can be easily adjusted to choose the top 2 or 5 or any other number of cities without lengthening the code or increasing the processing appreciably. Hope you don&#039;t need it or the translation to SQl Server and your tables is not to difficult.

select c.county_name,
       l.city_name,
       y.registrations
  from location_code l,
       entpr.county c,
       (
select county_code,
       min(row_num) min_row
  from (
select c.county_code,
       r.city_code,
       r.registrations,
       rownum row_num
  from (
select county_code,
       city_code,
       count(*) registrations
  from regular_registration
 where transaction_id is not null
 group by
       county_code,
       city_code) r,
       county c
 where c.county_code = r.county_code
 order by
       c.county_code,
       r.city_code,
       r.registrations desc)
 group by
       county_code) x,
       (
select c.county_code,
       r.city_code,
       r.registrations,
       rownum row_num
  from (
select county_code,
       city_code,
       count(*) registrations
  from regular_registration
 where transaction_id is not null
 group by
       county_code,
       city_code) r,
       county c
 where c.county_code = r.county_code
 order by
       c.county_code,
       r.city_code,
       r.registrations desc) y
 where x.county_code = y.county_code
   and y.row_num - x.min_row ]]></description>
		<content:encoded><![CDATA[<p>I hope TOP worked for you. If not, I have a variation on r937&#8242;s solution that works with a similar situation I have in one of my Oracle databases. That is, if SQL server has something like Oracle&#8217;s rownum psuedocolumn (returns a sequential number starting with 1 for each row of a cursor). If so, regular_registration, county and location_code are analogous to your restaurant, country and city tables. The advantage is that it can be easily adjusted to choose the top 2 or 5 or any other number of cities without lengthening the code or increasing the processing appreciably. Hope you don&#8217;t need it or the translation to SQl Server and your tables is not to difficult.</p>
<p>select c.county_name,<br />
       l.city_name,<br />
       y.registrations<br />
  from location_code l,<br />
       entpr.county c,<br />
       (<br />
select county_code,<br />
       min(row_num) min_row<br />
  from (<br />
select c.county_code,<br />
       r.city_code,<br />
       r.registrations,<br />
       rownum row_num<br />
  from (<br />
select county_code,<br />
       city_code,<br />
       count(*) registrations<br />
  from regular_registration<br />
 where transaction_id is not null<br />
 group by<br />
       county_code,<br />
       city_code) r,<br />
       county c<br />
 where c.county_code = r.county_code<br />
 order by<br />
       c.county_code,<br />
       r.city_code,<br />
       r.registrations desc)<br />
 group by<br />
       county_code) x,<br />
       (<br />
select c.county_code,<br />
       r.city_code,<br />
       r.registrations,<br />
       rownum row_num<br />
  from (<br />
select county_code,<br />
       city_code,<br />
       count(*) registrations<br />
  from regular_registration<br />
 where transaction_id is not null<br />
 group by<br />
       county_code,<br />
       city_code) r,<br />
       county c<br />
 where c.county_code = r.county_code<br />
 order by<br />
       c.county_code,<br />
       r.city_code,<br />
       r.registrations desc) y<br />
 where x.county_code = y.county_code<br />
   and y.row_num &#8211; x.min_row </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rolaaus</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49721</link>
		<dc:creator>rolaaus</dc:creator>
		<pubDate>Wed, 23 Mar 2005 15:57:46 +0000</pubDate>
		<guid isPermaLink="false">#comment-49721</guid>
		<description><![CDATA[You can embed an SQL statement into another statement, and I am not exactly 100% sure of the syntax.  You would want the embedded statement to pull the 3 city ID&#039;s with the highest COUNT of resturauntID&#039;s.  From there, you could somehow (and here is where I forget the proper syntax) do a join to this query with the simple query of city/country.

Something like
select cityname, cityid from city-table where cityid = (select  county(esturauntid), cityid from resturaunt-table TOP 3)

Actually, the problem I see with this is that there will never be a matched join because you with get a count of resturaunts and the cityid will never be equal with what you are wanting, but there should be a way to get the rusturaunt count without &#039;displaying&#039; it in the sub-query, but again, I forget how to do that.]]></description>
		<content:encoded><![CDATA[<p>You can embed an SQL statement into another statement, and I am not exactly 100% sure of the syntax.  You would want the embedded statement to pull the 3 city ID&#8217;s with the highest COUNT of resturauntID&#8217;s.  From there, you could somehow (and here is where I forget the proper syntax) do a join to this query with the simple query of city/country.</p>
<p>Something like<br />
select cityname, cityid from city-table where cityid = (select  county(esturauntid), cityid from resturaunt-table TOP 3)</p>
<p>Actually, the problem I see with this is that there will never be a matched join because you with get a count of resturaunts and the cityid will never be equal with what you are wanting, but there should be a way to get the rusturaunt count without &#8216;displaying&#8217; it in the sub-query, but again, I forget how to do that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sloopjohnb</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49723</link>
		<dc:creator>sloopjohnb</dc:creator>
		<pubDate>Wed, 23 Mar 2005 15:50:15 +0000</pubDate>
		<guid isPermaLink="false">#comment-49723</guid>
		<description><![CDATA[Have you tried

SELECT TOP 3 ...

HTH,
John Barone]]></description>
		<content:encoded><![CDATA[<p>Have you tried</p>
<p>SELECT TOP 3 &#8230;</p>
<p>HTH,<br />
John Barone</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: timallard</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/urgentplease-help-top-3-cities-only-returned-based-on-count-result/#comment-49724</link>
		<dc:creator>timallard</dc:creator>
		<pubDate>Wed, 23 Mar 2005 15:48:36 +0000</pubDate>
		<guid isPermaLink="false">#comment-49724</guid>
		<description><![CDATA[You need a TOP(3) statement that only applies to resturants ... not tested:

SELECT TOP(3) resturantid, co.countryid, ci.cityid FROM r resturant WHERE r.isactive=1
JOIN co country ON r.countryid = co.countryid 
JOIN ci city ON r.cityid = ci.cityid
GROUP BY co.countryname, ci.cityname
ORDER BY co.countryname, resturantid, ci.cityid DESC]]></description>
		<content:encoded><![CDATA[<p>You need a TOP(3) statement that only applies to resturants &#8230; not tested:</p>
<p>SELECT TOP(3) resturantid, co.countryid, ci.cityid FROM r resturant WHERE r.isactive=1<br />
JOIN co country ON r.countryid = co.countryid<br />
JOIN ci city ON r.cityid = ci.cityid<br />
GROUP BY co.countryname, ci.cityname<br />
ORDER BY co.countryname, resturantid, ci.cityid DESC</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/10 queries in 0.039 seconds using memcached
Object Caching 393/399 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-25 22:28:29 -->