{"id":121,"date":"2021-01-13T00:35:26","date_gmt":"2021-01-13T00:35:26","guid":{"rendered":"https:\/\/datasciencebasic.com\/?p=121"},"modified":"2021-04-29T00:14:21","modified_gmt":"2021-04-29T00:14:21","slug":"sql-voi-bigquery","status":"publish","type":"post","link":"https:\/\/datasciencebasic.com\/?p=121","title":{"rendered":"SQL v\u1edbi BigQuery"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQL l\u00e0 m\u1ed9t trong nh\u1eefng k\u1ef9 n\u0103ng ph\u1ed5 bi\u1ebfn nh\u1ea5t trong Khoa h\u1ecdc d\u1eef li\u1ec7u. Trong b\u00e0i vi\u1ebft n\u00e0y t\u00f4i s\u1ebd tr\u00ecnh b\u00e0y c\u00e1ch s\u1eed d\u1ee5ng SQL trong X\u1eed l\u00fd d\u1eef li\u1ec7u b\u1eb1ng c\u00f4ng c\u1ee5 BigQuery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1edbi thi\u1ec7u<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SQL (Structured Query Language &#8211; Ng\u00f4n ng\u1eef truy v\u1ea5n c\u00f3 c\u1ea5u tr\u00fac) l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy v\u1ea5n v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7. C\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 \u0111\u01b0\u1ee3c h\u00ecnh th\u00e0nh b\u1edfi t\u1eadp h\u1ee3p c\u00e1c b\u1ea3ng hai chi\u1ec1u (v\u00ed d\u1ee5: T\u1eadp d\u1eef li\u1ec7u, B\u1ea3ng t\u00ednh Excel). M\u1ed7i b\u1ea3ng n\u00e0y sau \u0111\u00f3 \u0111\u01b0\u1ee3c t\u1ea1o th\u00e0nh b\u1edfi m\u1ed9t s\u1ed1 c\u1ed9t c\u1ed1 \u0111\u1ecbnh v\u00e0 m\u1ed9t s\u1ed1 h\u00e0ng b\u1ea5t k\u1ef3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">H\u00e3y l\u1ea5y v\u00ed d\u1ee5 v\u1ec1 c\u00e1c nh\u00e0 s\u1ea3n xu\u1ea5t \u0111i\u1ec7n tho\u1ea1i di \u0111\u1ed9ng. M\u1ed7i nh\u00e0 s\u1ea3n xu\u1ea5t \u0111i\u1ec7n tho\u1ea1i c\u00f3 th\u1ec3 c\u00f3 m\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u bao g\u1ed3m nhi\u1ec1u b\u1ea3ng kh\u00e1c nhau (v\u00ed d\u1ee5: m\u1ed9t b\u1ea3ng cho t\u1eebng m\u1eabu s\u1ea3n ph\u1ea9m \u0111i\u1ec7n tho\u1ea1i). Trong m\u1ed7i b\u1ea3ng n\u00e0y s\u1ebd c\u00f3 c\u00e1c s\u1ed1 li\u1ec7u kh\u00e1c nhau v\u1ec1 doanh s\u1ed1 b\u00e1n t\u1eebng m\u1eabu \u0111i\u1ec7n tho\u1ea1i \u1edf c\u00e1c qu\u1ed1c gia kh\u00e1c nhau.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C\u00f9ng v\u1edbi Python v\u00e0 R, SQL hi\u1ec7n \u0111\u01b0\u1ee3c coi l\u00e0 m\u1ed9t trong nh\u1eefng k\u1ef9 n\u0103ng \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u nhi\u1ec1u nh\u1ea5t trong Khoa h\u1ecdc d\u1eef li\u1ec7u. M\u1ed9t s\u1ed1 l\u00fd do khi\u1ebfn cho SQL l\u00e0 k\u1ef9 n\u0103ng c\u1ea7n thi\u1ebft hi\u1ec7n nay:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Kho\u1ea3ng 2,5 ngh\u00ecn t\u1ef7 byte d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c t\u1ea1o ra m\u1ed7i ng\u00e0y. \u0110\u1ec3 l\u01b0u tr\u1eef m\u1ed9t l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u nh\u01b0 v\u1eady, c\u1ea7n ph\u1ea3i s\u1eed d\u1ee5ng c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u (database).<\/li><li>C\u00e1c c\u00f4ng ty ng\u00e0y c\u00e0ng coi tr\u1ecdng gi\u00e1 tr\u1ecb c\u1ee7a d\u1eef li\u1ec7u h\u01a1n. V\u00ed d\u1ee5, d\u1eef li\u1ec7u c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3: ph\u00e2n t\u00edch v\u00e0 gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 kinh doanh, \u0111\u01b0a ra d\u1ef1 \u0111o\u00e1n v\u1ec1 xu h\u01b0\u1edbng th\u1ecb tr\u01b0\u1eddng v\u00e0 hi\u1ec3u v\u1ec1 nhu c\u1ea7u c\u1ee7a kh\u00e1ch h\u00e0ng.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1ed9t trong nh\u1eefng \u01b0u \u0111i\u1ec3m ch\u00ednh c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng SQL l\u00e0 khi th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c v\u1edbi d\u1eef li\u1ec7u, d\u1eef li\u1ec7u s\u1ebd \u0111\u01b0\u1ee3c truy c\u1eadp tr\u1ef1c ti\u1ebfp (kh\u00f4ng c\u1ea7n sao ch\u00e9p tr\u01b0\u1edbc). Do \u0111\u00f3 th\u1eddi gian th\u1ef1c hi\u1ec7n c\u00f4ng vi\u1ec7c gi\u1ea3m \u0111\u00e1ng k\u1ec3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">C\u00f3 nhi\u1ec1u c\u01a1 s\u1edf d\u1eef li\u1ec7u SQL kh\u00e1c nhau nh\u01b0: SQLite, MySQL, Postgres, Oracle v\u00e0 Microsoft SQL Server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong b\u00e0i vi\u1ebft n\u00e0y, t\u00f4i s\u1ebd gi\u1edbi thi\u1ec7u c\u00e1ch b\u1eaft \u0111\u1ea7u mi\u1ec5n ph\u00ed v\u1edbi SQL b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng Google BigQuery \u0111\u01b0\u1ee3c t\u00edch h\u1ee3p tr\u00ean Kaggle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Google gi\u1edbi thi\u1ec7u v\u1ec1 BigQuery nh\u01b0 sau:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">BigQuery l\u00e0 kho d\u1eef li\u1ec7u \u0111\u1ec3 doanh nghi\u1ec7p gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 b\u1eb1ng c\u00e1ch cho ph\u00e9p truy v\u1ea5n SQL si\u00eau nhanh, s\u1eed d\u1ee5ng s\u1ee9c m\u1ea1nh x\u1eed l\u00fd c\u1ee7a c\u01a1 s\u1edf h\u1ea1 t\u1ea7ng c\u1ee7a Google. Ch\u1ec9 c\u1ea7n di chuy\u1ec3n d\u1eef li\u1ec7u c\u1ee7a b\u1ea1n v\u00e0o BigQuery v\u00e0 \u0111\u1ec3 ch\u00fang t\u00f4i x\u1eed l\u00fd c\u00f4ng vi\u1ec7c kh\u00f3 kh\u0103n c\u00f2n l\u1ea1i.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ngu\u1ed3n: <a href=\"https:\/\/cloud.google.com\/bigquery\/docs\/introduction\">https:\/\/cloud.google.com\/bigquery\/docs\/introduction<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B\u1eaft \u0111\u1ea7u v\u1edbi BigQuery<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0110\u1ec3 s\u1eed d\u1ee5ng BigQuery, tr\u01b0\u1edbc h\u1ebft ch\u00fang ta c\u1ea7n t\u1ea1o m\u1ed9t t\u00e0i kho\u1ea3n <a href=\"https:\/\/cloud.google.com\/blog\/products\/data-analytics\/query-without-a-credit-card-introducing-bigquery-sandbox\">Google Cloud Platform<\/a> mi\u1ec5n ph\u00ed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">L\u1ef1a ch\u1ecdn v\u00e0o m\u1ee5c BigQuery tr\u00ean Google Cloud Platform, b\u1ea1n c\u00f3 th\u1ec3 l\u00e0m vi\u1ec7c tr\u00ean c\u00e1c b\u1ed9 d\u1eef li\u1ec7u (dataset) m\u1eabu s\u1eb5n c\u00f3 thu\u1ed9c nhi\u1ec1u l\u0129nh v\u1ef1c kh\u00e1c nhau (H\u00ecnh 1), t\u1ea3i l\u00ean file d\u1eef li\u1ec7u, ho\u1eb7c k\u1ebft n\u1ed1i v\u1edbi c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 MySQL ho\u1eb7c PostgreSQL.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"464\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=1024%2C464&#038;ssl=1\" alt=\"\" class=\"wp-image-122\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=1024%2C464&amp;ssl=1 1024w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=300%2C136&amp;ssl=1 300w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=768%2C348&amp;ssl=1 768w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=1536%2C696&amp;ssl=1 1536w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?resize=720%2C326&amp;ssl=1 720w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/dataset-GPC.png?w=1780&amp;ssl=1 1780w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Ngu\u1ed3n: <a href=\"https:\/\/console.cloud.google.com\/\">https:\/\/console.cloud.google.com\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ngo\u00e0i ra, khi s\u1eed d\u1ee5ng Kaggle Kernels (phi\u00ean b\u1ea3n tr\u1ef1c tuy\u1ebfn c\u1ee7a Jupyter Notebooks \u0111\u01b0\u1ee3c t\u00edch h\u1ee3p trong h\u1ec7 th\u1ed1ng Kaggle), c\u00f3 m\u1ed9t t\u00f9y ch\u1ecdn \u0111\u1ec3 b\u1eadt Google BigQuery. Kaggle cung c\u1ea5p d\u1ecbch v\u1ee5 BigQuery mi\u1ec5n ph\u00ed l\u00ean \u0111\u1ebfn 5 terabyte (5TB) m\u1ed9t th\u00e1ng cho m\u1ed7i ng\u01b0\u1eddi d\u00f9ng (n\u1ebfu b\u1ea1n d\u00f9ng h\u1ebft gi\u1edbi h\u1ea1n n\u00e0y, b\u1ea1n s\u1ebd ph\u1ea3i \u0111\u1ee3i \u0111\u1ebfn th\u00e1ng ti\u1ebfp theo).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tr\u01b0\u1edbc h\u1ebft ch\u00fang ta c\u1ea7n c\u00f3 m\u1ed9t t\u00e0i kho\u1ea3n <a href=\"https:\/\/cloud.google.com\/blog\/products\/data-analytics\/query-without-a-credit-card-introducing-bigquery-sandbox\">Google Cloud Platform<\/a> v\u00e0 m\u1ed9t b\u1ea3n sao c\u1ee7a d\u1ef1 \u00e1n tr\u00ean Google Service c\u1ee7a m\u00ecnh. B\u1ea1n c\u00f3 th\u1ec3 t\u00ecm th\u1ea5y<a> <\/a><u><a href=\"https:\/\/www.youtube.com\/watch?v=_YYqfS7rLUo&amp;utm_medium=email&amp;utm_source=intercom&amp;utm_campaign=sql-summer-camp\">\u1edf \u0111\u00e2y<\/a><\/u> h\u01b0\u1edbng d\u1eabn v\u1ec1 c\u00e1ch b\u1eaft \u0111\u1ea7u ch\u1ec9 trong v\u00e0i ph\u00fat.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sau khi t\u1ea1o d\u1ef1 \u00e1n BigQuery tr\u00ean Google Account Platform, ch\u00fang ta s\u1ebd nh\u1eadn \u0111\u01b0\u1ee3c m\u1ed9t Project ID. Trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y t\u00f4i s\u1eed d\u1ee5ng b\u1ed9 d\u1eef li\u1ec7u tr\u00ean Kaggle c\u00f3 t\u00ean l\u00e0 \u201cE-commerce Shipping Data\u201d (<a href=\"https:\/\/www.kaggle.com\/prachi13\/customer-analytics\">https:\/\/www.kaggle.com\/prachi13\/customer-analytics<\/a>). Project ID c\u1ee7a t\u00f4i tr\u00ean Google Cloud l\u00e0 &#8220;huy-nguyen-kaggle&#8221;. T\u00f4i t\u1ea3i v\u1ec1 b\u1ed9 d\u1eef li\u1ec7u n\u00e0y t\u1eeb Kaggle v\u00e0 t\u1ea3i l\u00ean Google Cloud d\u01b0\u1edbi d\u1ea1ng file .csv, \u0111\u1eb7t t\u00ean b\u1ed9 d\u1eef li\u1ec7u l\u00e0 \u201cecommerce\u201d. B\u00e2y gi\u1edd t\u00f4i c\u00f3 th\u1ec3 k\u1ebft n\u1ed1i Kaggle Kernel c\u1ee7a m\u00ecnh v\u1edbi BigQuery b\u1eb1ng c\u00e1ch ch\u1ea1y m\u1ed9t v\u00e0i \u0111o\u1ea1n code nh\u01b0 sau tr\u00ean Kaggle Notebook.<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Ch\u1ec9 ra Project ID c\u00f3 ch\u1ee9a b\u1ed9 d\u1eef li\u1ec7u:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># Identify the project ID containing the desired dataset for analysis in this kernel<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">PROJECT_ID = 'huy-nguyen-kaggle'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Import BigQuery API t\u1eeb th\u01b0 vi\u1ec7n c\u1ee7a google.cloud<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Import the BQ API Client library<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">from google.cloud import bigquery<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">client = bigquery.Client(project=PROJECT_ID, location='US')<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Li\u00ean k\u1ebft t\u1edbi b\u1ed9 d\u1eef li\u1ec7u ecommerce c\u00f3 trong d\u1ef1 \u00e1n BigQuery<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Construct a reference to the ecommerce dataset that is within the project<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">dataset_ref = client.dataset('ecommerce', project=PROJECT_ID)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. L\u1ea5y d\u1eef li\u1ec7u t\u1eeb b\u1ed9 d\u1eef li\u1ec7u tr\u00ean<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Make an API request to fetch the dataset<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">dataset = client.get_dataset(dataset_ref)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Xem tr\u01b0\u1edbc T\u1eadp d\u1eef li\u1ec7u c\u1ee7a b\u1ea1n<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u00e2y gi\u1edd b\u1ea1n c\u00f3 m\u1ed9t t\u1eadp d\u1eef li\u1ec7u bi\u1ebfn c\u00f3 ch\u1ee9a t\u1eadp d\u1eef li\u1ec7u BigQuery m\u00e0 b\u1ea1n mu\u1ed1n s\u1eed d\u1ee5ng trong kernel n\u00e0y. \u0110\u1ed1i v\u1edbi v\u00ed d\u1ee5 n\u00e0y, \u0111i\u1ec1u \u0111\u00f3 c\u00f3 ngh\u0129a l\u00e0 b\u00e2y gi\u1edd t\u00f4i \u0111\u00e3 c\u00f3 b\u1ed9 d\u1eef li\u1ec7u th\u01b0\u01a1ng m\u1ea1i \u0111i\u1ec7n t\u1eed ecommerce c\u1ee7a m\u00ecnh. B\u00e2y gi\u1edd t\u00f4i c\u00f3 th\u1ec3 xem tr\u01b0\u1edbc c\u00e1c n\u1ed9i dung c\u1ee7a b\u1ed9 d\u1eef li\u1ec7u b\u1eb1ng c\u00e1c \u0111o\u1ea1n code sau:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Xem danh s\u00e1ch t\u00ean c\u00e1c b\u1ea3ng (table) trong b\u1ed9 d\u1eef li\u1ec7u<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\"># Make a list of all the tables in the dataset<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">tables = list(client.list_tables(dataset))<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;# Print names of all tables in the dataset<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">for table in tables:&nbsp; <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;print(table.table_id)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>K\u1ebft qu\u1ea3 (output):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Ecommerce<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Xem tr\u01b0\u1edbc 5 d\u00f2ng \u0111\u1ea7u ti\u00ean c\u1ee7a b\u1ed9 d\u1eef li\u1ec7u<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Preview the first five lines of the table<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">client.list_rows(table, max_results=5).to_dataframe()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>K\u1ebft qu\u1ea3 (output):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"139\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?resize=1024%2C139&#038;ssl=1\" alt=\"\" class=\"wp-image-123\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?resize=1024%2C139&amp;ssl=1 1024w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?resize=300%2C41&amp;ssl=1 300w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?resize=768%2C104&amp;ssl=1 768w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?resize=720%2C97&amp;ssl=1 720w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/5-first-rows.png?w=1285&amp;ssl=1 1285w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Vi\u1ebft v\u00e0 ch\u1ea1y truy v\u1ea5n SQL (ho\u1eb7c nhi\u1ec1u truy v\u1ea5n SQL)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">B\u00e2y gi\u1edd ch\u00fang ta \u0111\u00e3 c\u00f3 d\u1eef li\u1ec7u c\u1ee7a m\u00ecnh, ta c\u00f3 th\u1ec3 b\u1eaft \u0111\u1ea7u th\u1ef1c hi\u1ec7n c\u00f4ng vi\u1ec7c ch\u00ednh: vi\u1ebft v\u00e0 ch\u1ea1y m\u1ed9t truy v\u1ea5n SQL. C\u00f3 th\u1ec3 b\u1ea1n mu\u1ed1n vi\u1ebft m\u1ed9t truy v\u1ea5n \u0111\u1ec3 l\u1ea5y m\u1ed9t t\u1eadp con c\u1ee5 th\u1ec3 c\u1ee7a t\u1eadp d\u1eef li\u1ec7u. Ho\u1eb7c c\u00f3 th\u1ec3 b\u1ea1n c\u00f3 m\u1ed9t c\u00e2u h\u1ecfi c\u1ee5 th\u1ec3 v\u1ec1 d\u1eef li\u1ec7u v\u00e0 b\u1ea1n mu\u1ed1n tr\u1ea3 l\u1eddi n\u00f3 th\u00f4ng qua m\u1ed9t truy v\u1ea5n. T\u1ea5t c\u1ea3 \u0111\u1ec1u c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n v\u1edbi kernel. \u1ede \u0111\u00e2y, t\u00f4i s\u1ebd l\u1ea5y v\u00ed d\u1ee5 m\u1ed9t truy v\u1ea5n tr\u1ea3 l\u1eddi c\u00e1c c\u00e2u h\u1ecfi c\u1ee5 th\u1ec3 v\u1ec1 t\u1eadp d\u1eef li\u1ec7u ecommerce.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5 ch\u00fang ta mu\u1ed1n bi\u1ebft ph\u01b0\u01a1ng th\u1ee9c v\u1eadn chuy\u1ec3n n\u00e0o l\u00e0 ph\u1ed5 bi\u1ebfn nh\u1ea5t v\u1edbi m\u1ed7i m\u1ee9c \u0111\u1ed9 quan tr\u1ecdng c\u1ee7a h\u00e0ng h\u00f3a, ch\u00fang ta c\u00f3 th\u1ec3 d\u00f9ng truy v\u1ea5n sau:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># What is the most common mode of transport with different levels of product importance?<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">query = \"\"\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISTINCT Product_importance AS Product_importance,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Mode_of_Shipment AS Mode_of_Shipment, <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COUNT(ID) AS Count<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`huy-nguyen-kaggle.ecommerce.ecommerce`<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GROUP BY <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mode_of_Shipment, <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product_importance<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Product_importance,<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Count DESC<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">query_job = client.query(query)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">query_job.to_dataframe()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>K\u1ebft qu\u1ea3 (output):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"280\" src=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/query-result.png?resize=325%2C280&#038;ssl=1\" alt=\"\" class=\"wp-image-124\" srcset=\"https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/query-result.png?w=325&amp;ssl=1 325w, https:\/\/i0.wp.com\/datasciencebasic.com\/wp-content\/uploads\/2021\/04\/query-result.png?resize=300%2C258&amp;ssl=1 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">T\u1eeb b\u1ea3ng k\u1ebft qu\u1ea3 truy v\u1ea5n tr\u00ean, ch\u00fang ta c\u00f3 th\u1ec3 th\u1ea5y ph\u01b0\u01a1ng th\u1ee9c v\u1eadn chuy\u1ec3n b\u1eb1ng t\u00e0u th\u1ee7y (Ship) l\u00e0 ph\u01b0\u01a1ng th\u1ee9c v\u1eadn chuy\u1ec3n ph\u1ed5 bi\u1ebfn nh\u1ea5t v\u1edbi c\u1ea3 3 m\u1ee9c \u0111\u1ed9 quan tr\u1ecdng c\u1ee7a h\u00e0ng h\u00f3a.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>K\u1ebft lu\u1eadn<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tr\u00ean \u0111\u00e2y l\u00e0 gi\u1edbi thi\u1ec7u kh\u00e1i qu\u00e1t v\u1ec1 c\u00e1ch b\u1eaft \u0111\u1ea7u s\u1eed d\u1ee5ng BigQuery SQL \u0111\u1ec3 truy v\u1ea5n d\u1eef li\u1ec7u. N\u1ebfu b\u1ea1n mu\u1ed1n t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 SQL, b\u1ea1n c\u00f3 th\u1ec3 theo d\u00f5i c\u00e1c kh\u00f3a h\u1ecdc mi\u1ec5n ph\u00ed <a href=\"https:\/\/www.kaggle.com\/learn\/intro-to-sql\">Kaggle Intro to SQL<\/a> v\u00e0 <a href=\"https:\/\/sqlbolt.com\/\">SQLBolt<\/a>. Ch\u00fac b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>SQL l\u00e0 m\u1ed9t trong nh\u1eefng k\u1ef9 n\u0103ng ph\u1ed5 bi\u1ebfn nh\u1ea5t trong Khoa h\u1ecdc d\u1eef li\u1ec7u. Trong b\u00e0i vi\u1ebft n\u00e0y t\u00f4i [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-phan-mem-cong-cu"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":4,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":149,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions\/149"}],"wp:attachment":[{"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datasciencebasic.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}