| Author |
Topic |
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-05-02 : 15:44:20
|
Hi I am learning sql and am using the famous northwind database, I am trying to learn to use aggregate functions such as count()I am able to get the required result using this query which simply gives me a city and a count of occurences in the db, I get 69 records :)SELECT COUNT(city) as NoCity, cityFROM CustomersGROUP BY City I need a query that further gives me the country in which these cities are found, also in the db! however when I use the query below I get different results to what I require, The result from this query treats every occurence of the city as one and I get 91 records from the table, I would like to write a query that gives me the 69 from above with the countries and the count of the city occurence!Please point me in the right direction! |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-05-02 : 15:47:45
|
heres the query that is giving me unexpected resultsSELECT COUNT(city)As NoOfCity , city, ContactNameFROM CustomersGROUP BY city, ContactName it is counting but treating each occurence with a different ContactName as new city and thus giving me 91 records, |
 |
|
|
shilpash
Posting Yak Master
103 Posts |
Posted - 2012-05-02 : 17:14:18
|
| is it contactname or countryname |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-05-02 : 17:19:08
|
| doestnt really matter, point is it still returns an unexpected result, but yes it is countryname |
 |
|
|
shilpash
Posting Yak Master
103 Posts |
Posted - 2012-05-02 : 17:32:35
|
| can u post a screenshot of ur result |
 |
|
|
shilpash
Posting Yak Master
103 Posts |
Posted - 2012-05-02 : 22:01:51
|
quote: Originally posted by soulchyld21 doestnt really matter, point is it still returns an unexpected result, but yes it is countryname
I dont know how you got 91 with countryyou get 91 with contact name onlybut you get 69 from country.City (No column name) ContactNameMadrid 1 Alejandra CaminoLeipzig 1 Alexander FeuerMéxico D.F. 1 Ana TrujilloSao Paulo 1 Anabela DominguesCampinas 1 André FonsecaLondon 1 Ann DevonToulouse 1 Annette RouletMéxico D.F. 1 Antonio MorenoSao Paulo 1 Aria CruzLander 1 Art BraunschweigerRio de Janeiro 1 Bernardo BatistaNantes 1 Carine SchmittBarquisimeto 1 Carlos GonzálezSan Cristóbal 1 Carlos HernándezBruxelles 1 Catherine DeweyLuleå 1 Christina BerglundVersailles 1 Daniel ToniniMadrid 1 Diego RoelParis 1 Dominique PerrierBarcelona 1 Eduardo SaavedraLondon 1 Elizabeth BrownTsawassen 1 Elizabeth LincolnI. de Margarita 1 Felipe IzquierdoPortland 1 Fran WilsonMéxico D.F. 1 Francisco ChangStrasbourg 1 Frédérique CiteauxSalzburg 1 Georg PippsBergamo 1 Giovanni RovelliMéxico D.F. 1 Guillermo FernándezMannheim 1 Hanna MoosLondon 1 Hari KumarCowes 1 Helen BennettKirkland 1 Helvetius NagyKöln 1 Henriette PfalzheimCunewalde 1 Horst KlossEugene 1 Howard SnyderLisboa 1 Isabel de CastroSan Francisco 1 Jaime YorresRio de Janeiro 1 Janete LimeiraNantes 1 Janine LabruneMontréal 1 Jean FresnièreWalla Walla 1 John SteelStavern 1 Jonas BergulfsenBoise 1 Jose PavarottiSevilla 1 José Pedro FreyreKobenhavn 1 Jytte PetersenMünster 1 Karin JosephsSeattle 1 Karl JablonskiMarseille 1 Laurence LebihanLisboa 1 Lino RodriguezButte 1 Liu WongPortland 1 Liz NixonSao Paulo 1 Lúcia CarvalhoCaracas 1 Manuel PereiraBerlin 1 Maria AndersBräcke 1 Maria LarssonParis 1 Marie BertrandRio de Janeiro 1 Mario PontesMadrid 1 Martín SommerLille 1 Martine RancéLyon 1 Mary SaveleyHelsinki 1 Matti KarttunenReggio Emilia 1 Maurizio MoroniGenève 1 Michael HolzMéxico D.F. 1 Miguel Angel PaolinoÅrhus 1 Palle IbsenTorino 1 Paolo AccortiCharleroi 1 Pascale CartrainCork 1 Patricia McKennaBuenos Aires 1 Patricio SimpsonReims 1 Paul HenriotResende 1 Paula ParenteAlbuquerque 1 Paula WilsonSao Paulo 1 Pedro AfonsoMünchen 1 Peter FrankenBrandenburg 1 Philip CramerOulu 1 Pirkko KoskitaloFrankfurt a.M. 1 Renate MessnerAnchorage 1 Rene PhillipsStuttgart 1 Rita MüllerGraz 1 Roland MendelBuenos Aires 1 Sergio GutiérrezLondon 1 Simon CrowtherAachen 1 Sven OttliebLondon 1 Thomas HardyLondon 1 Victoria AshworthBern 1 Yang WangElgin 1 Yoshi LatimerVancouver 1 Yoshi TannamuriBuenos Aires 1 Yvonne MoncadaWarszawa 1 Zbyszek Piestrzeniewicz |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-03 : 07:17:07
|
quote: Originally posted by shilpash is it contactname or countryname
Can you provide the sample data ..In case of Group by clause what exactly happens this group clause group the data on the basis of columns you have specified...If you send the Sample data we can provide you the answer for it. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-05-03 : 07:51:01
|
This gives the 69 rows:SELECT Country, City, count(*)FROM CustomersGROUP BY Country,Cityorder by Country,City No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-05-03 : 09:13:26
|
| Hi, thanks I seem to have found a way around it all, It was my misunderstanding of the group by clause, when using it we have to add every column not in an aggregate function to the group by clause, if one wanted to have the data from other columns but didnt want the result grouped on those grounds is there a way to work around it? say if one only wanted to group by the ID (PK) and needed to sum() say library visits but also needed the phone number? |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-05-03 : 09:29:27
|
you can use max(Phone) or min(Phone) if you like No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|