0% found this document useful (0 votes)
242 views10 pages

Query

Here are the steps to calculate sales of Electronics as a percentage of Books: 1) Sum the sales of all Book products 2) Sum the sales of all Electronics products 3) Divide the sum of Electronics sales by the sum of Book sales 4) Multiply the result by 100 to get the percentage The SQL would be: SELECT (SUM(CASE WHEN PRODUCT_GROUP='Electronics' THEN SALES_AMT ELSE 0 END)/ SUM(CASE WHEN PRODUCT_GROUP='Book' THEN SALES_AMT ELSE 0 END))*100 AS "Sales of Electronics as % of Books" FROM SALES_FACT f JOIN PRODUCT_DIM p

Uploaded by

Vamshi Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views10 pages

Query

Here are the steps to calculate sales of Electronics as a percentage of Books: 1) Sum the sales of all Book products 2) Sum the sales of all Electronics products 3) Divide the sum of Electronics sales by the sum of Book sales 4) Multiply the result by 100 to get the percentage The SQL would be: SELECT (SUM(CASE WHEN PRODUCT_GROUP='Electronics' THEN SALES_AMT ELSE 0 END)/ SUM(CASE WHEN PRODUCT_GROUP='Book' THEN SALES_AMT ELSE 0 END))*100 AS "Sales of Electronics as % of Books" FROM SALES_FACT f JOIN PRODUCT_DIM p

Uploaded by

Vamshi Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Question 11 / 11

Consider an Orders table which has 15 years of historical orders and you are loading everyday incremental orders into it on a daily basis(1 million orders per day are loaded and historically the full orders is close to a billion orders). The orders table is range partitioned on order_day.

ORDERS ----------ORDER_ID ORDER_DAY CUSTOMER_ID ADDRESS_ID ITEM_ID PRICE QUANTITY

ORDERS_DAILY_INCREMENT ---------------SNAPSHOT_DAY ORDER_ID ORDER_DAY CUSTOMER_ID ADDRESS_ID ITEM_ID PRICE QUANTITY If you are asked to build a daily aggregate table like the one shown below with "new_customer" metric and "reactivated_customer" metric(a reactivated customer is the

one who did not have a purchase in the last one year but made a purchase today) on each day CUSTOMER_AGGREGATE ---------------------------------SNAPSHOT_DAY NEW_CUSTOMERS REACTIVATED_CUSTOMERS Would you build any interim tables to support this requirement ? if yes, why and what would they look like along with SQLs that you would use on ORDERS and ORDERS_DAILY_INCREMENT table to build those interim tables if no, why not and please provide the SQL that you would use to build the final CUSTOMER_AGGREGATE table using ORDERS and ORDERS_DAILY_INCREMENT

Question 10 / 11

Consider a Phone Log table as below. It records all phone numbers that we dial in a given day. SOURCE_PHONE_NUMBER DESTINATION_PHONE_NUMBER CALL_START_DATETIME 1234 4567 01/07/2011 10:00 1234 2345 01/07/2011 11:00 1234 3456 01/07/2011 12:00 1234 3456 01/07/2011 13:00 1234 4567 01/07/2011 15:00 1222 7890 01/07/2011 10:00 1222 7680 01/07/2011 12:00 1222 2345 01/07/2011 13:00

Please provide an SQL query to display the source_phone_number and a flag where the flag needs to be set to Y if first called number and last called number are the same and N if the first called number and last called number are different.

Desired Output: Source Number 1222 1234 Is_Match N Y

Product Dimension Table: PRODUCT_ID P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 PRODUCT_GROUP Book Book Electronics Electronics Electronics Video DVD Video DVD Video DVD Video DVD Shoes Shoes Shoes Shoes Book Book PRODUCT_NAME Harry Potter 1 Harry Potter 2 Nikon 10 MPS Cannon 8 MPS Cannon 10 MPS Pirates 1 Pirates 2 HP 1 HP 2 Nike 10 Nike 11 Adidas 10 Adidas 09 God Father 1 God Father 2

Sales_fact Table: SNAPSHOT_DAY PRODUCT_ID SALES_AMT

20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

P1 P2 P8 P3 P4 P5 P6 P7 P9 P10 P11

10 5 100 5 25 15 35 5 30 8 45

Glance View Fact Table: SNAPSHOT_DAY 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 PRODUCT_ID P1 P2 P8 P3 P4 P5 P6 P7 P9 P10 P12 P13 P14 GLANCE_VIEWS 1000 800 700 800 500 250 10 1000 1500 600 670 300 230

Inventory Fact Table:

SNAPSHOT_DAY 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

PRODUCT_ID P1 P2 P8 P3 P4

ON_HAND_QUANTITY 100 70 90 10 30

20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

P5 P6 P7 P9

100 120 70 90

Ad_spend_fact Table:

SNAPSHOT_DAY 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

PRODUCT_ID P1 P2 P8 P3 P4 P5 P6 P7 P9 P10 P11

GLANCE_VIEWS 10 5 100 5 25 15 35 5 30 8 45

Write a SQL that will give the top product by sales in each of the product groups and additionally gather GV, Inventory, and ad spend measures also for the product. Desired Output: Book Electronics Shoes Video DVD P1 P4 P11 P8 10 25 45 100 1000 500 0 700 100 30 0 90 0 30 0 0

Product Dimension Table: PRODUCT_ID PRODUCT_GROUP PRODUCT_NAME P1 Book Harry Potter 1 P2 Book Harry Potter 2

P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15

Electronics Electronics Electronics Video DVD Video DVD Video DVD Video DVD Shoes Shoes Shoes Shoes Book Book

Nikon 10 MPS Cannon 8 MPS Cannon 10 MPS Pirates 1 Pirates 2 HP 1 HP 2 Nike 10 Nike 11 Adidas 10 Adidas 09 God Father 1 God Father 2

Sales_fact Table: SNAPSHOT_DAY PRODUCT_ID SALES_AMT 20-JUL-11 P1 10 20-JUL-11 P2 5 20-JUL-11 P8 100 20-JUL-11 P3 5 20-JUL-11 P4 25 20-JUL-11 P5 15 20-JUL-11 P6 35 20-JUL-11 P7 5 20-JUL-11 P9 30 20-JUL-11 P10 8 20-JUL-11 P11 45

Glance View Fact Table: SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS 20-JUL-11 P1 1000 20-JUL-11 P2 800 20-JUL-11 P8 700 20-JUL-11 P3 800 20-JUL-11 P4 500 20-JUL-11 P5 250

20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

P6 P7 P9 P10 P12 P13 P14

10 1000 1500 600 670 300 230

Inventory Fact Table:

SNAPSHOT_DAY PRODUCT_ID ON_HAND_QUANTITY 20-JUL-11 P1 100 20-JUL-11 P2 70 20-JUL-11 P8 90 20-JUL-11 P3 10 20-JUL-11 P4 30 20-JUL-11 P5 100 20-JUL-11 P6 120 20-JUL-11 P7 70 20-JUL-11 P9 90

Ad_spend_fact Table:

SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS 20-JUL-11 P1 10 20-JUL-11 P2 5 20-JUL-11 P8 100 20-JUL-11 P3 5 20-JUL-11 P4 25 20-JUL-11 P5 15 20-JUL-11 P6 35 20-JUL-11 P7 5 20-JUL-11 P9 30 20-JUL-11 P10 8 20-JUL-11 P11 45

For the table structures as above, write a SQL to give all Products that have glance views but no sales. Desired Output: P12 P13 P14

Product Dimension Table: PRODUCT_ID PRODUCT_GROUP PRODUCT_NAME P1 Book Harry Potter 1 P2 Book Harry Potter 2 P3 Electronics Nikon 10 MPS P4 Electronics Cannon 8 MPS P5 Electronics Cannon 10 MPS P6 Video DVD Pirates 1 P7 Video DVD Pirates 2 P8 Video DVD HP 1 P9 Video DVD HP 2 P10 Shoes Nike 10 P11 Shoes Nike 11 P12 Shoes Adidas 10 P13 Shoes Adidas 09 P14 Book God Father 1 P15 Book God Father 2

Sales_fact Table: SNAPSHOT_DAY PRODUCT_ID SALES_AMT 20-JUL-11 P1 10 20-JUL-11 P2 5 20-JUL-11 P8 100 20-JUL-11 P3 5 20-JUL-11 P4 25 20-JUL-11 P5 15 20-JUL-11 P6 35

20-JUL-11 20-JUL-11 20-JUL-11 20-JUL-11

P7 P9 P10 P11

5 30 8 45

Glance View Fact Table: SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS 20-JUL-11 P1 1000 20-JUL-11 P2 800 20-JUL-11 P8 700 20-JUL-11 P3 800 20-JUL-11 P4 500 20-JUL-11 P5 250 20-JUL-11 P6 10 20-JUL-11 P7 1000 20-JUL-11 P9 1500 20-JUL-11 P10 600 20-JUL-11 P12 670 20-JUL-11 P13 300 20-JUL-11 P14 230

Inventory Fact Table:

SNAPSHOT_DAY PRODUCT_ID ON_HAND_QUANTITY 20-JUL-11 P1 100 20-JUL-11 P2 70 20-JUL-11 P8 90 20-JUL-11 P3 10 20-JUL-11 P4 30 20-JUL-11 P5 100 20-JUL-11 P6 120 20-JUL-11 P7 70 20-JUL-11 P9 90

Ad_spend_fact Table:

SNAPSHOT_DAY PRODUCT_ID GLANCE_VIEWS 20-JUL-11 P1 10 20-JUL-11 P2 5 20-JUL-11 P8 100 20-JUL-11 P3 5 20-JUL-11 P4 25 20-JUL-11 P5 15 20-JUL-11 P6 35 20-JUL-11 P7 5 20-JUL-11 P9 30 20-JUL-11 P10 8 20-JUL-11 P11 45

For the same above table structure, Write a SQL to give the sales of Electronics as a Percentage of Books Desired Output:

You might also like