2021-03-09 SQL-Intermediate

You can use CHARINDEX to find a specific character or pattern within a column.
CHARINDEX returns the position of the keyword, for example, the "weather" appears at 29th (caculation includes space (空格键)), so it finally returns 29

Use substring:
SUBSTRING(Expression, Starting Position, Total Length)
We used SUBSTRING, passed in the description column, told the function to begin counting from the 15th character, for the remaining length of the string, which ensured we returned each remaining cause of outage.

About how to solve this problem:
start_number is a number
now I want to extract additional information of the description.
now I want the string except the "weather"
substring(description, ?, len(description)) ? should be the starting position

Sometimes you might want to 'save' the results of a query so you can do some more work with the data. You can do that by creating a temporary table that remains in the database until SQL Server is restarted.

SELECT  album.title AS album_title,
  artist.name as artist,
  MAX(track.milliseconds / (1000 * 60) % 60 ) AS max_track_length_mins
-- Name the temp table #maxtracks
INTO #maxtracks
FROM album
-- Join album to artist using artist_id
INNER JOIN artist ON album.artist_id = artist.artist_id
-- Join track to album using album_id
INNER JOIN track ON track.album_id=album.album_id
GROUP BY artist.artist_id, album.title, artist.name,album.album_id
-- Run the final SELECT query to retrieve the results from the temporary table
SELECT album_title, artist, max_track_length_mins
FROM  #maxtracks
ORDER BY max_track_length_mins DESC, artist;

Replace the null value:

-- Check the IncidentState column for missing values and replace them with the City column
SELECT IncidentState, ISNULL(IncidentState,"City") AS Location
FROM Incidents
-- Filter to only return missing values from IncidentState
WHERE IncidentState IS NULL

What if you want to replace missing values in one column with another and want to check the replacement column to make sure it doesn't have any missing values? To do that you need to use the COALESCE statement.

-- Replace missing values 
SELECT Country, coalesce(IncidentState, City, 'Unknown') AS Location
FROM Incidents
WHERE Country IS NULL

Create a new column, SourceCountry, defined from these cases:
When Country is 'us' then it takes the value 'USA'.
Otherwise it takes the value 'International'.

SELECT Country, 
       CASE WHEN Country = 'us'  THEN 'USA'
       ELSE 'International'
       END AS SourceCountry
FROM Incidents

Use case when:

- Complete the syntax for cutting the duration into different cases
SELECT DurationSeconds, 
-- Start with the 2 TSQL keywords, and after the condition a TSQL word and a value
      case when (DurationSeconds <= 120) then 1
-- The pattern repeats with the same keyword and after the condition the same word and next value          
       when (DurationSeconds > 120 AND DurationSeconds <= 600)then 2
-- Use the same syntax here             
        when (DurationSeconds > 601 AND DurationSeconds <= 1200) then  3
-- Use the same syntax here               
        when (DurationSeconds > 1201 AND DurationSeconds <= 5000) then 4
-- Specify a value      
       ELSE 5 
       END AS SecondGroup   
FROM Incidents

Use count just after "select" and then use group by

-- Write a query that returns an aggregation 
SELECT MixDesc, SUM(Quantity) AS Total
FROM Shipments
-- Group by the relevant column
GROUP BY MixDesc

Use DATEDIFF and DATEADD
SELECT DATEADD(DD, -30, '2020-06-21')
DATEDIFF (datepart, startdate, enddate)

- Return the DeliveryDate as 5 days after the ShipDate
SELECT OrderDate, 
       DATEADD(DD,5,ShipDate) AS DeliveryDate
FROM Shipments

Use ABS() to return non-negative values
Use SQUARE() and SQRT to calculate square and square root

use derived tabel:
calculate the maximum value of the blood glucose level for each record by age.

SELECT a.RecordId, a.Age, a.BloodGlucoseRandom, 
-- Select maximum glucose value (use colname from derived table)
       b.MaxGlucose
FROM Kidney a
-- Join to derived table
JOIN (SELECT Age, MAX(BloodGlucoseRandom) AS MaxGlucose FROM Kidney GROUP BY Age) b
-- Join on Age
ON a.Age = b.Age

Another sample to select the highest bloodpressure grouped by their age:

SELECT *
FROM Kidney a
-- Create derived table: select age, max blood pressure from kidney grouped by age
JOIN (select age,max(BloodPressure) AS MaxBloodPressure from kidney group by age) b
-- JOIN on BloodPressure equal to MaxBloodPressure
ON a.BloodPressure=b.MaxBloodPressure
-- Join on Age
AND a.Age=b.Age

UseCTE() to simplify the sql sentences:

-- Specify the keyowrds to create the CTE
with BloodGlucoseRandom (MaxGlucose) 
as (SELECT MAX(BloodGlucoseRandom) AS MaxGlucose FROM Kidney)

SELECT a.Age, b.MaxGlucose
FROM Kidney a
-- Join the CTE on blood glucose equal to max blood glucose
JOIN BloodGlucoseRandom b
on a.BloodGlucoseRandom=b.MaxGlucose

Find the patient with the highest bloodpressure:

- Create the CTE
WITH BloodPressure(MaxBloodPressure)
AS (SELECT MAX(BloodPressure) FROM kidney AS MaxBloodPressure)

SELECT *
FROM Kidney a
-- Join the CTE  
Join BloodPressure b
on a.BloodPressure=b.MaxBloodPressure

you can create a window for the entire table. To create partitions using a specific column, you need to use OVER() along with PARTITION BY.

SELECT OrderID, TerritoryName, 
       -- Total price for each partition
       sum(OrderPrice)
       -- Create the window and partitions
       over(partition by TerritoryName) AS TotalPrice
FROM Orders

you want to figure out the first OrderDate in each territory or the last one. How would you do that? You can use the window functions FIRST_VALUE() and LAST_VALUE(), respectively! Here are the steps:

First, create partitions for each territory
Then, order by OrderDate
Finally, use the FIRST_VALUE() and/or LAST_VALUE() functions as per your requirement
SELECT TerritoryName, OrderDate, 
       -- Select the first value in each partition
       first_value(OrderDate) 
       -- Create the partitions and arrange the rows
       OVER(PARTITION BY TerritoryName order BY OrderDate) AS FirstOrder
FROM Orders

Use lead() next, lag() previous

SELECT TerritoryName, OrderDate, 
       -- Specify the previous OrderDate in the window
       lag(OrderDate) 
       -- Over the window, partition by territory & order by order date
       Over(partition BY TerritoryName order BY OrderDate) AS PreviousOrder,
       -- Specify the next OrderDate in the window
       lead(OrderDate) 
       -- Create the partitions and arrange the rows
       Over(partition BY TerritoryName order BY OrderDate)  AS NextOrder
FROM Orders

create a running total:

SELECT TerritoryName, OrderDate, 
       -- Create a running total
       sum(orderprice) 
       -- Create the partitions and arrange the rows
       over(partition by TerritoryName order by OrderDate) AS TerritoryTotal      
FROM Orders

Use row_number() to count the rows in different groups

SELECT TerritoryName, OrderDate, 
       -- Assign a row number
       row_number()
       -- Create the partitions and arrange the rows
       OVER(PARTITION BY TerritoryName ORDER BY OrderDate) AS OrderCount
FROM Orders

Use standered deiviation : stdev()

There is no functions to calculate the mode, so we have to create CTE() to them

-- Create a CTE Called ModePrice which contains two columns
with ModePrice (OrderPrice, UnitPriceFrequency)
AS
(
    SELECT OrderPrice, 
    ROW_NUMBER() 
    OVER(PARTITION BY OrderPrice ORDER BY OrderPrice) AS UnitPriceFrequency
    FROM Orders 
)

-- Select everything from the CTE
SELECT * FROM ModePrice

the whole version of how to use CTE to calculate mode:

WITH ModePrice (OrderPrice, UnitPriceFrequency)
AS
(
    SELECT OrderPrice,
    ROW_NUMBER() 
    OVER (PARTITION BY OrderPrice ORDER BY OrderPrice) AS UnitPriceFrequency
    FROM Orders
)

-- Select the order price from the CTE
SELECT OrderPrice AS ModeOrderPrice
FROM ModePrice
-- Select the maximum UnitPriceFrequency from the CTE
WHERE UnitPriceFrequency IN (select max(UnitPriceFrequency) FROM ModePrice)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,764评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,235评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,965评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,984评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,984评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,471评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,844评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,818评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,359评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,385评论 3 346
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,515评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,114评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,836评论 3 338
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,291评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,422评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,064评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,581评论 2 365

推荐阅读更多精彩内容