none
שגיאה לא מובנת RRS feed

תשובות

  • הי אדוה,

    כפי שגרי פירט ה- group by מקבץ את ה- Result set ש"נוצר" ע"י ה- Select לפי העמודות המפורטות, במקרה שלך, Country ו- City.

    כל שדה שהוא לא חלק מהשדות שמוכלים במשפט ה group by חייב להיות בתוך פונקציה אגריגטיבית כלשהי, sum, min, max, avg וכו...

    אם אני מבין נכון את השאילתא שאת מעוניינת לבצע זה:

    להביא את רשימת כל האנשים לפי מדינה ועיר או מכל מדינה ועיר כמה אנשים שונים מופעים.

    צירפתי דוגמא שאולי תעזור להבין את ה - group by וגם לינק

    Create table dbo.Persons
    
    (
    
    	country varchar(20),
    
    	City	varchar(20),
    
    	Person	varchar(50)	
    
    )
    
    Go
    
    
    
    Insert dbo.Persons(country, City, Person)
    
    Select 'Israel', 'Haifa', 'Avi' Union All
    
    Select 'Israel', 'Haifa', 'Sharon' Union All
    
    Select 'Israel', 'Tel-Avib', 'Yarden' Union All
    
    Select 'Israel', 'Haifa', 'Gal' 
    
    Go
    
    
    
    
    
    -- Summarize how many person exists in the table by Country and City
    
    Select country,City, count(*) As Tota_per_Country_And_City
    
    From dbo.Persons
    
    group by country,City 
    
    
    
    -- Let's insert Avi twice in Tel-Aviv
    
    Insert dbo.Persons(country, City, Person)
    
    Select 'Israel', 'Tel-Aviv', 'Avi' Union All
    
    Select 'Israel', 'Tel-Aviv', 'Avi' 
    
    Go
    
    
    
    -- If I want to get summary of the distinct names per city and country 
    
    Select country,City, Person, count(*) As TheCountOfThisNameInTheCountryAndCity 
    
    From dbo.Persons
    
    group by country,City , Person

     

    http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server

    בהצלחה


    אסף שלם
    • הוצע כתשובה על-ידי Moshe Atlow יום ראשון 10 יולי 2011 20:18
    • סומן כתשובה על-ידי Shirly11 יום שני 11 יולי 2011 07:00
    יום ראשון 10 יולי 2011 19:30

כל התגובות

  • אפשר פירוט?

    מה בדיוק השגיאה?

    יום ראשון 10 יולי 2011 18:23
  • אם עמודת Person אינה מופיע ברשימת ה-Group By - יש לצרף לה פונקציה אגרגטיבית (למשל- MAX).
    Geri Reshef http://gerireshef.wordpress.com
    יום ראשון 10 יולי 2011 18:59
  • הי אדוה,

    כפי שגרי פירט ה- group by מקבץ את ה- Result set ש"נוצר" ע"י ה- Select לפי העמודות המפורטות, במקרה שלך, Country ו- City.

    כל שדה שהוא לא חלק מהשדות שמוכלים במשפט ה group by חייב להיות בתוך פונקציה אגריגטיבית כלשהי, sum, min, max, avg וכו...

    אם אני מבין נכון את השאילתא שאת מעוניינת לבצע זה:

    להביא את רשימת כל האנשים לפי מדינה ועיר או מכל מדינה ועיר כמה אנשים שונים מופעים.

    צירפתי דוגמא שאולי תעזור להבין את ה - group by וגם לינק

    Create table dbo.Persons
    
    (
    
    	country varchar(20),
    
    	City	varchar(20),
    
    	Person	varchar(50)	
    
    )
    
    Go
    
    
    
    Insert dbo.Persons(country, City, Person)
    
    Select 'Israel', 'Haifa', 'Avi' Union All
    
    Select 'Israel', 'Haifa', 'Sharon' Union All
    
    Select 'Israel', 'Tel-Avib', 'Yarden' Union All
    
    Select 'Israel', 'Haifa', 'Gal' 
    
    Go
    
    
    
    
    
    -- Summarize how many person exists in the table by Country and City
    
    Select country,City, count(*) As Tota_per_Country_And_City
    
    From dbo.Persons
    
    group by country,City 
    
    
    
    -- Let's insert Avi twice in Tel-Aviv
    
    Insert dbo.Persons(country, City, Person)
    
    Select 'Israel', 'Tel-Aviv', 'Avi' Union All
    
    Select 'Israel', 'Tel-Aviv', 'Avi' 
    
    Go
    
    
    
    -- If I want to get summary of the distinct names per city and country 
    
    Select country,City, Person, count(*) As TheCountOfThisNameInTheCountryAndCity 
    
    From dbo.Persons
    
    group by country,City , Person

     

    http://www.sqlteam.com/article/how-to-use-group-by-in-sql-server

    בהצלחה


    אסף שלם
    • הוצע כתשובה על-ידי Moshe Atlow יום ראשון 10 יולי 2011 20:18
    • סומן כתשובה על-ידי Shirly11 יום שני 11 יולי 2011 07:00
    יום ראשון 10 יולי 2011 19:30
  • אההה!! עזרתם לי מאוד !! תודה ויום טוב !!
    יום שני 11 יולי 2011 07:01