Example 1 :Sheet 1 :
Nested sort : Region desc, Category asc, subcategory desc
(Order by Dimension )
select distinct region,category,subcategory
from orders
order by Region desc ,Category asc,subcategory desc;
Example 2:Sheet2:
Sort by Sales measure
select region, category,subcategory,round(sum(sales))
from orders
group by region,category,subcategory
order by (sum(sales)) desc;
Example 3:Sheet3:
Region,Category,Subgateory wise sales (Region pivoted)
select * from
(
select region, category,subcategory,round(sum(sales)) sales
from orders
group by region,category,subcategory
)
pivot (
sum(sales) for region in ('Central' Central,'East' East,'South' South,'West' West)
)
order by 1,2;
Example 4:Sheet4:
Show Grand total and Sub total Regin,Category,Sub category wise
select case when region is null then 'Grand Total' else region end reggion ,
case when category is null and region is not null then 'Total' else category end category,
case when subcategory is null and category is not null then 'Total' else subcategory end
subcategory ,
round(sum(sales)) sales,
GROUPING_id(region) region_grouping,
GROUPING_id(region,category) region_cat_grouping,
GROUPING_id(category,subcategory) region_cat_grouping
from orders
group by grouping sets ((),(region),(region,category),(region,category,subcategory))
order by region,category,subcategory;
Example 5:Sheet5:
Show Grand total only
select region ,
category,
subcategory ,
round(sum(sales)) sales
from orders
group by grouping sets ((),(region,category,subcategory))
order by 1,2;
Example 6/7/8:Sheet6/7/8:
-- Rank based on sum(sales) in desc order –-SHEET 8
-- Rank based on sum(sales) in desc order WITHIN region –SHEET 7
-- Rank based on sum(sales) in desc order WITHIN region category –SHEET 6
select region ,
category,
subcategory ,
round(sum(sales)) sales,
rank() OVER (ORDER BY sum(sales) DESC) rank_sales,
rank() OVER (partition by region ORDER BY sum(sales) DESC) rank_regionwise_sales,
rank() OVER (partition by region,category ORDER BY sum(sales) DESC) rank_region_cat_sales –SHEET 6
from orders
group by region,category,subcategory
--order by rank_sales;-- Rank by sum(sales)
--order by region,rank_regionwise_sales; --rank by sum sales within region
order by region,category ,rank_region_cat_sales;
Sheet 6
Sheet 7
Sheet 8

Tableau sql tips

  • 1.
    Example 1 :Sheet1 : Nested sort : Region desc, Category asc, subcategory desc (Order by Dimension ) select distinct region,category,subcategory from orders order by Region desc ,Category asc,subcategory desc;
  • 4.
    Example 2:Sheet2: Sort bySales measure select region, category,subcategory,round(sum(sales)) from orders group by region,category,subcategory order by (sum(sales)) desc;
  • 7.
    Example 3:Sheet3: Region,Category,Subgateory wisesales (Region pivoted) select * from ( select region, category,subcategory,round(sum(sales)) sales from orders group by region,category,subcategory ) pivot ( sum(sales) for region in ('Central' Central,'East' East,'South' South,'West' West) ) order by 1,2;
  • 10.
    Example 4:Sheet4: Show Grandtotal and Sub total Regin,Category,Sub category wise select case when region is null then 'Grand Total' else region end reggion , case when category is null and region is not null then 'Total' else category end category, case when subcategory is null and category is not null then 'Total' else subcategory end subcategory , round(sum(sales)) sales, GROUPING_id(region) region_grouping, GROUPING_id(region,category) region_cat_grouping, GROUPING_id(category,subcategory) region_cat_grouping from orders group by grouping sets ((),(region),(region,category),(region,category,subcategory)) order by region,category,subcategory;
  • 13.
    Example 5:Sheet5: Show Grandtotal only select region , category, subcategory , round(sum(sales)) sales from orders group by grouping sets ((),(region,category,subcategory)) order by 1,2;
  • 16.
    Example 6/7/8:Sheet6/7/8: -- Rankbased on sum(sales) in desc order –-SHEET 8 -- Rank based on sum(sales) in desc order WITHIN region –SHEET 7 -- Rank based on sum(sales) in desc order WITHIN region category –SHEET 6 select region , category, subcategory , round(sum(sales)) sales, rank() OVER (ORDER BY sum(sales) DESC) rank_sales, rank() OVER (partition by region ORDER BY sum(sales) DESC) rank_regionwise_sales, rank() OVER (partition by region,category ORDER BY sum(sales) DESC) rank_region_cat_sales –SHEET 6 from orders group by region,category,subcategory --order by rank_sales;-- Rank by sum(sales) --order by region,rank_regionwise_sales; --rank by sum sales within region order by region,category ,rank_region_cat_sales;
  • 18.
  • 19.
  • 20.