सबक्वेरीज़

Snowflake में डेटा मैनिपुलेशन

Jake Roach

Field Data Engineer

सबक्वेरीज़ क्या हैं?

सबक्वेरीज़ एक टूल हैं जिनसे एक क्वेरी का परिणाम दूसरी क्वेरी में इस्तेमाल होता है.

$$

  • कई क्वेरियों को जोड़ें
  • पठनीयता पर ध्यान दें
  • मॉड्यूलैरिटी सक्षम करें
  • डेटा मैनिपुलेशन आसान बनाएँ!

$$

FROM ( ... ) या WHERE ... IN ( ... )

सबक्वेरीज़ से डेटा को अधिक पठनीय और मॉड्यूलर तरीके से मैनिपुलेट करने की लॉजिकल प्रक्रिया

Snowflake में डेटा मैनिपुलेशन

सबक्वेरीज़ और FROM

SELECT
    ...
-- Pull from query, not from a table
FROM (

    -- Create a result set that will 
    -- be used by the main query
    SELECT 
        <fields>
    FROM <table>
    WHERE ...

);

टेबल से सीधे लेने के बजाय किसी दूसरी क्वेरी के परिणाम से डेटा प्राप्त करें.

$$

  • मैनिपुलेशन को एनालिसिस से अलग करें
  • क्वेरियाँ पढ़ना और समझना आसान होता है
  • "portability" और बदलाव आसान बनते हैं
  • JOIN, WHERE, आदि
Snowflake में डेटा मैनिपुलेशन

सबक्वेरी से पहले

SELECT
    month_num,
    -- windchill - temperature has to be used twice here. What if this changes?
    AVG(windchill - temperature) AS avg_differential
    MIN(windchill - temperature) AS most_differential
FROM weather
WHERE 
    -- Filtering is taking place in the same query as aggregation/analysis
    season = 'Winter' AND
    temperature < 32
GROUP BY month_num;
Snowflake में डेटा मैनिपुलेशन

सबक्वेरी के बाद

-- Start with the subquery, then aggregate

SELECT month_num, AVG(differential) AS avg_differential MIN(differential) AS most_differential FROM (
SELECT month_num, windchill - temperature AS differential FROM weather WHERE season = 'Winter' AND temperature < 32
) GROUP BY month_num;
          | month_num | differential |
          | --------- | ------------ |
          |     12    |      -12     |
          |     1     |      -3      |
          |     1     |       0      |
          |     2     |      -7      |
| month_num | avg_differential | most_differential |
| --------- | ---------------- | ----------------- |
|     12    |       -5.77      |        -14        |
|     1     |       -1.91      |        -8         |
|     2     |       -8.13      |        -22        |

डेटा साफ होने पर विश्लेषण समझना और बदलना आसान हो जाता है.

Snowflake में डेटा मैनिपुलेशन

WHERE ... IN ( ... )

...

-- Filter by records with a value in 
-- the subquery result set
WHERE <field> IN (

    SELECT <other-field> FROM ... 

);

ट्रांसफॉर्म, फ़िल्टर, या डेटा मैनिपुलेट करते समय उपयोग के लिए एक छोटा परिणाम सेट बनाएँ.

$$

  • ऐसे रिकॉर्ड फ़िल्टर करें जो IN एक non-constant रिजल्ट सेट हों
  • क्वेरी के अन्य हिस्सों में भी उपयोग हो सकता है
  • AVG, MAX, MIN, आदि
Snowflake में डेटा मैनिपुलेशन

WHERE ... IN( ... )

SELECT
    todays_date,
    temperature,
    status
FROM weather
WHERE todays_date IN (  -- Filter by all days with home games that were won

    SELECT
        game_date
    FROM game_schedule
    WHERE stadium = 'Home' AND did_win = TRUE

);
Snowflake में डेटा मैनिपुलेशन

WHERE ... IN ( ... )

हर होम गेम के लिए मौसम डेटा खोजने हेतु सबक्वेरी इस्तेमाल करने वाली क्वेरी का परिणाम सेट

Snowflake में डेटा मैनिपुलेशन

अभ्यास करते हैं!

Snowflake में डेटा मैनिपुलेशन

Preparing Video For Download...