【SQL】仮想WITHかサブクエリか([SQL] virtual WITH or subquery?)

データアナリストは個人レストラン経営に似ている。食材の仕入れ・調理・運営まで、ひととおりできて個性が出てくる。ちなみにデータサイエンティストは料理研究家っぽい。機械学習やモデルを構築する専門家だから。

これまでフロントエンドしか齧っていない私は、ファストフードのレジ打ちバイトのようなものでした。トレイに並べた商品をお客様に手渡すけど、調理はレンチンだし、パテとバンズを重ねたこともない、自動調理器なるBIツールの存在は知ってるけど見てるだけ。

ということでまずはSQLから学んでいます。この後はBIツール、そしてR。kaggle参加したいけどなんだかPython主流ぽい。あとでかんがえよう。


仮想テーブル WITH

本記述より前に来る WITH ●● AS () なひとたち、複数可。

生姜焼き作るとき、先にお肉を生姜とお酒につけこんだりしますね。ソースを混ぜておいたり、下ごしらえしておけば本調理も簡単!そして本記述と別だからわかりやすい。ソースに名前つけとかないと本記述で使えない。SELECTに制約がでるGROUP BY 時に便利な印象あり。


サブクエリ

まるでマトリョーシカ、切り刻んだ野菜をその場でいれてくよ!

本記述の中に (SELECT... ) があったらサブクエリ。SELECT,FROM,WHERE,どこだって出てくるから、SELECT内に千切りキャベツ(の中に畑から出したSELECTキャベツ)、FROM内にカットトマト缶、WHERE内にはみじん切りタマネギ(の前に皮むいたSELECTタマネギ)。深い。もう三つ以上は深い。インデントして。


仮想テーブルとサブクエリ、手法なので人それぞれでよいんだろうけども、勉強中の身としてはどちらが効率的かもまだわかってない。頭の中の組み立て次第だけど…私は結構遠回りしちゃうので、解答見て選択肢を増やすしかないか、と思ってます。

コードが短いのが良き、という人も入れば、丁寧な手順で他人が見てわかりやすければ長くなっても致し方なし、もありき。このへんプログラム言語あるあるですね。顧客にSQL読める人がいて、共有するとなると後者かなあ。


*******


A data analyst is like an individual restaurant owner. I would be start a business and do everything from purchasing ingredients to cooking to running it. By the way, data scientists are like a culinary researchers. Machine learning and model building experts.

With only web front-end experience, I was like a part-time cashier at a fast-food restaurant. Delivering the products arranged on the tray to the customer. I knew BI tools exist, but I was just looking at them.

So I'm learning SQL first. BI tools and R after that. I want to participate in Kaggle, but it seems that Python is mainstream. I'll think about it later.


Virtual table WITH

[WITH ●● AS () ] comes first description. Multiple allowed.

To make shoga-yaki, the meat is first marinated in ginger and sake. The main cooking is easy if you mix the sauce and prepare it!

And it is easy to understand because it is different from main description. If the source name is not mentioned, it cannot be used in main description. I think that it is convenient because [GROUP BY] has restrictions on [SELECT].


subquery

Just like a matryoshka doll, let's put the chopped vegetables into the right place!

If there is [SELECT...] in main description, it is a subquery. [SELECT][FROM][WHERE] and more. They can appear anywhere. Shredded cabbage in [SELECT] ( cabbage from the field another [SELECT]), canned tomatoes in [FROM], chopped onions in [WHERE] ( onions peeled first and [SELECT]) . deep. Three or more are deep. Please indent.


Virtual tables, subqueries and techniques are fine for each person. I'm still learning and don't know which is more efficient. I have a lot of steps and it's a roundabout way. I will study and keep increasing the variety of solutions.

Some people say a short code is better, and some people don't care if it's long enough as long as it's easy for others to see and understand. Common in programming languages. If there's a customer who can write SQL and I share it, I prefer it's the latter.

DATA idm8

Aim for a comprehensive analysis. Data-informed decision making. データ分析/著作権・知的財産マネジメント