BookmarkSubscribeRSS Feed
Hagay
SAS Employee

אחת מהפונקציות הכי נפוצות ב –  SQL היא count, שכשמה, כן היא, מחזירה לנו את מספר הרשומות בכל קבוצת group by. למרות שלא נראה שצריך דוגמא אביא אחת בכל זאת:

proc sql ;
	select 
		type,
		count(*) as Records
	from 
		SASHELP.CARS
	group by 
		type;
quit;

הכוכבית שאנחנו מעבירים כפרמטר לפונקציה הזאת מנחה אותה לספור את השורות בטבלה. אפשר להביא במקום הכוכבית כל שדה אחר בטבלה אבל יש להיזהר מדבר אחד – פונקציית ה – count לא כוללת בספירה ערכים חסרים:

proc sql ;
	select 
		type,
		count(*) as Records,
		count(cylinders) as Cyliners /* Note the Sports type*/
	from 
		SASHELP.CARS
	group by 
		type;
quit;

עוד שימוש נפוץ ב – count הוא לספירת הערכים השונים הקיימים בשדה מסויים ע"י הוספת המילה distinct בתוך הפונקציה לפני שם העמודה:

proc sql ;
	select 
		type,
		count(*) as Records,
		count(distinct make) as Makes
	from 
		SASHELP.CARS
	group by 
		type;
quit;

גם במקרה הזה פונקציית ה – count לא סופרת ערכים חסרים. אנחנו יכולים להשתמש בתכונה זאת ליצירה של חישובים מורכבים יותר בצורה פשוטה וקלה. למשל, כדי לספור את כמות היצרנים בכל קטגוריה עם רכבים שעולים יותר מ – 30,000 דולר:

proc sql ;
	select 
		type,
		count(*) as Records,
		count(distinct make) as Makes,
		count(distinct case when msrp>30000 then make else '' end) as Expensive_Make
	from 
		SASHELP.CARS
	group by 
		type;
quit;

חגי

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Discussion stats
  • 0 replies
  • 288 views
  • 1 like
  • 1 in conversation