数据库第二次作业
姓名:~~~,学号:~~~,专业:~~~
SELECT ChangeClose
FROM NDX
WHERE TDayOfWeek = 'Friday';
SELECT MIN(ChangeClose) as MinChangeClose,
MAX(ChangeClose) as MaxChangeClose,
AVG(ChangeClose) as AvgChangeClose,
FROM NDX
WHERE TDayOfWeek = 'Friday';
SELECT TYear,AVG(ChangeClose) as AvgChangeClose
FROM NDX
GROUP BY TYear;
SELECT TYear, TMonth, AVG(ChangeClose) as AvgChangeClose
FROM NDX
GROUP BY TYear, TMonth;
SELECT TYear, TQuarter, TMonth, AVG(ChangeClose) as AvgChangeClose
FROM (
SELECT TYear, TQuarter, TMonth,
DATEPART(MM, CONVERT(DATETIME, '01 ' + TMonth + ' 2000')) as MonthNumber,
ChangeClose
FROM NDX
) as SubQuery
GROUP BY TYear, TQuarter, MonthNumber
ORDER BY TYear, TQuarter, MonthNumber, AvgChangeClose DESC;
在这个查询中,我们首先创建了一个子查询,其中使用 DATEPART
和 CONVERT
函数将 TMonth
字段的字符串值转换为对应的数字。然后在主查询中,我们使用这个子查询的结果进行分组和排序。
CONVERT
函数将 TMonth
字段的值转换为日期类型,然后 DATEPART
函数提取出月份部分。这样,我们就得到了对应的月份数字,可以用来按照日历序排列月份。
SELECT TYear, TQuarter, TMonth, MAX(ChangeClose) - MIN(ChangeClose) AS Difference
FROM NDX
GROUP BY TYear, TQuarter, TMonth
ORDER BY Difference DESC;
SELECT TYear, AVG(ChangeClose) AS AverageChangeClose
FROM NDX
GROUP BY TYear
HAVING AVG(ChangeClose) > 0
ORDER BY AverageChangeClose DESC;
SELECT CONCAT(TYear, '/',
CASE
WHEN TMonth = 'January' THEN '01'
WHEN TMonth = 'February' THEN '02'
WHEN TMonth = 'March' THEN '03'
WHEN TMonth = 'April' THEN '04'
WHEN TMonth = 'May' THEN '05'
WHEN TMonth = 'June' THEN '06'
WHEN TMonth = 'July' THEN '07'
WHEN TMonth = 'August' THEN '08'
WHEN TMonth = 'September' THEN '09'
WHEN TMonth = 'October' THEN '10'
WHEN TMonth = 'November' THEN '11'
WHEN TMonth = 'December' THEN '12'
END,
'/', TDayOfMonth) AS FormattedDate
FROM NDX;
SELECT TYear, TMonth, TDayOfMonth, SUM(Volume) AS TotalVolume, AVG(ChangeClose) AS AverageChangeClose
FROM NDX
GROUP BY TYear, TMonth, TDayOfMonth;
SELECT TYear, TMonth, SUM(Volume) AS TotalVolume, AVG(ChangeClose) AS AverageChangeClose
FROM NDX
GROUP BY TYear, TMonth;
SELECT TYear, TMonth, TDayOfMonth, SUM(Volume) AS TotalVolume
FROM NDX
GROUP BY TYear, TMonth, TDayOfMonth
ORDER BY TotalVolume DESC
LIMIT 1;
SELECT TYear, TQuarter, AVG(Volume) AS AverageVolume
FROM NDX
GROUP BY TYear, TQuarter;
SELECT TYear, TMonth, TDayOfMonth, SUM(Volume) AS TotalVolume
FROM NDX
GROUP BY TYear, TMonth, TDayOfMonth
HAVING TotalVolume > 10000;