Тёмный

SQL Tutorial - Additive CASE statements 

BeardedDev
Подписаться 11 тыс.
Просмотров 16 тыс.
50% 1

Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.
If you like the videos you can support me on Patreon, / beardeddev
In this video I talk about how to use additive CASE statements in SQL Server.
For an introduction to CASE statements check out this video: • SQL Tutorial - CASE St...
If you are new to CTEs check out this video: • SQL TUTORIAL - CTEs Pa... , there is also a playlist on my channel
It is important to note that in the first case statement:
CASE WHEN Cat.EnglishProductCategoryName = 'Bikes'
THEN 0.10 END
This will cause any product categories that are not Bikes to be NULL, if this is not the desired result you can edit the query by adding ELSE:
CASE WHEN Cat.EnglishProductCategoryName = 'Bikes'
THEN 0.10 ELSE 0.00 END
CASE Statements were introduced to SQL Server in 2008. They are used to evaluate conditions and return a result based on those conditions. CASE Statements can be used in SELECT, UPDATE, DELETE, WHERE, HAVING.
In this CASE Tutorial we look at a common use of CASE statements within SELECT and also the benefit and common downfalls when writing CASE queries.
CASE Statement Syntax:
CASE WHEN [condition] THEN [value if true] ELSE [value if fales] END
SQL Queries in this video:
SELECT
Cat.EnglishProductCategoryName
, Subcat.EnglishProductSubcategoryName
, Prod.EnglishProductName
, CASE WHEN Cat.EnglishProductCategoryName = 'Bikes'
THEN 0.10 END
+ CASE WHEN Subcat.EnglishProductSubcategoryName = 'Road Bikes'
THEN 0.10 ELSE 0.00 END
+ CASE WHEN Subcat.EnglishProductSubcategoryName = 'Road Bikes'
AND (Prod.EnglishProductName LIKE '%Red%'
OR Prod.EnglishProductName LIKE '%Yellow%')
THEN 0.05 ELSE 0.00 END
AS Discount
FROM dbo.DimProductCategory AS Cat
INNER JOIN dbo.DimProductSubcategory AS Subcat
ON Cat.ProductCategoryKey = Subcat.ProductCategoryKey
INNER JOIN dbo.DimProduct AS Prod
ON Subcat.ProductSubcategoryKey = Prod.ProductSubcategoryKey
WHERE Cat.EnglishProductCategoryName = 'Bikes'
--AND Subcat.EnglishProductSubcategoryName = 'Road Bikes'
Please feel free to post any comments.

Наука

Опубликовано:

 

24 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 25   
@johnbreslin4521
@johnbreslin4521 3 года назад
That was really good, didn't know you could do this with CASE statements
@gilenomascarenhas6693
@gilenomascarenhas6693 3 года назад
Okay, that was brilliant, didactic and rare. And that goes for both the example and the performance. Thank you, sir!
@BeardedDevData
@BeardedDevData 3 года назад
Thanks so much for the positive feedback.
@vikkichang232
@vikkichang232 Год назад
Finally... something that was helpful THANK YOU!
@chandrarama1970
@chandrarama1970 4 года назад
You are back !! :) Your videos helped me a lot in my college assignments. especially the window function series.
@BeardedDevData
@BeardedDevData 4 года назад
Thanks so much, if there is anything you would like to see a tutorial on please let me know.
@chandrarama1970
@chandrarama1970 4 года назад
@@BeardedDevData Sure!!!!!!!
@surbhikabra519
@surbhikabra519 4 года назад
@@BeardedDevData Hi, I loved your window function series too. I would like to see how we could solve questions like 'Earliest order id for each other, each day of the month' How do you dig through different dimensions'?
@newbieuserguy6899
@newbieuserguy6899 2 года назад
It would help students / neophytes if the 3 discounts were unique values. By using 10%, 10%, and 5%, there's an opportunity to misunderstand which of the two "10%" occurrences is being applied. I suggest using 13%, 10%, and 5%.
@BeardedDevData
@BeardedDevData 2 года назад
That's a fair point.
@padhisambit
@padhisambit 3 года назад
Really nice information...helps a lot !!
@subhanmehdizade2852
@subhanmehdizade2852 2 года назад
Thanks, sir. Many many thanks! wish u best.
@TomerBenDavid
@TomerBenDavid 4 года назад
Great video and great speaker!
@superfreiheit1
@superfreiheit1 Год назад
LIke is expensive, you could also use the Color column.
@hamiali
@hamiali 3 года назад
Awesome..Great speaker..!!!
@modhua4497
@modhua4497 3 года назад
Excellent example
@fulvioalario6986
@fulvioalario6986 3 года назад
Great video. Thanks. Just a question. Could I use the AND operator instead of + ?
@BeardedDevData
@BeardedDevData 3 года назад
Not in this case as we actually want to perform addition.
@pson3672
@pson3672 4 года назад
Thanks for your tutorial. My doubt is can we add one more columns to group all the discounts together. For example: when the discount is 10% it should be an "Avg discount" mentioned in another column, when discount is 25% it should be "Max discount".
@BeardedDevData
@BeardedDevData 4 года назад
If my understanding of your question is correct then you mean instead of CASE WHEN Cat.EnglishProductCategoryName = 'Bikes' THEN 0.10 END we have CASE WHEN Cat.EnglishProductCategoryName = 'Bikes' THEN AverageDiscount END (providing AverageDiscount is a column) then yes we don't have to use literal values within CASE statements, can refer to other columns
@MNSHARIF623466
@MNSHARIF623466 2 года назад
helpful
@bamii_a
@bamii_a 4 года назад
love the videos mate, where can I download the db ?
@BeardedDevData
@BeardedDevData 4 года назад
Thanks so much, you can download the .bak files for Adventure Works from here, docs.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver15&tabs=tsql, I used AdventureWorksDW2016 in the vido
@JD_018
@JD_018 2 года назад
Great Video. I was wondering, can I use similar case statements to return a text value and not carry out any addition operations? Thanks
@BeardedDevData
@BeardedDevData 2 года назад
Sure, for example CASE WHEN Quantity < 10 THEN 'Small' WHEN QUANTITY < 30 THEN 'Medium' ELSE 'Large' END AS OrderSize
Далее
SQL Tutorial - CASE Statements
10:04
Просмотров 47 тыс.
МОЙ НОВЫЙ ДОМ
1:01:04
Просмотров 1,5 млн
IF/THEN logic with CASE expressions in SQL
1:02:31
Просмотров 1,5 тыс.
SQL Tutorial - How to update data using joins in SQL
22:41
SQL Views Tutorial | VIEWS in SQL Complete Tutorial
43:20
Writing CASE WHEN Statements in SQL (IF/THEN)
5:50
Просмотров 85 тыс.
Window Functions - Calculate Running Differences
11:44
iPhone 16 - НЕ СТОИТ ПРОПУСКАТЬ
4:50