SQL Serverで四半期ごとにグループ化された3列のデータを含むテーブル変数を作成しようとしています。 3つの列のそれぞれに適切なデータを選択することに成功しましたが、追加のクエリごとにテーブル変数の内容を更新する方法はわかりません。SQL Server:テーブル変数の更新
したがって、4つの行(四半期ごとに1つ)と3つのデータ列を持つ表ではなく、データ列に12行(4/4が3回重複しています)と多数のNULLがあります。
NULLSなしでテーブルの4行のみのバージョンを作成するにはどうすればよいですか?
クエリ:クエリから設定
DECLARE @temp TABLE (oYear INT, oQuarter INT, chin FLOAT, press FLOAT, pull FLOAT)
DECLARE @sDate AS DATE, @eDate AS DATE, @athName AS VARCGAR(50),
@chin1RM AS FLOAT, @press1RM AS FLOAT, @pull1RM AS FLOAT;
SET @sDate = '2016-10-01';
SET @eDate = '2017-10-01';
SET @athName = 'Steve';
INSERT INTO @temp (oYear, oQuarter, chin)
SELECT
DATEPART(YEAR, Day.Date),
DATEPART(QUARTER, Day.Date),
MAX(Pred1RMBMF)
FROM
GymLoad
JOIN
Session ON Session.id = GymLoad.SessionID
JOIN
Day ON Day.id = Session.DayID
JOIN
Athlete ON Athlete.id = Day.AthleteID
WHERE
(ExerciseID = 200 OR ExerciseID = 198)
AND FullName = @athName
AND Day.Date >= @sDate
AND Day.Date <= @eDate
GROUP BY
DATEPART(YEAR,Day.Date), DATEPART(QUARTER,Day.Date);
INSERT INTO @temp (oYear, oQuarter, press)
SELECT
DATEPART(YEAR, Day.Date),
DATEPART(QUARTER, Day.Date),
MAX(Pred1RMBMF)
FROM
GymLoad
JOIN
Session ON Session.id = GymLoad.SessionID
JOIN
Day ON Day.id = Session.DayID
JOIN
Athlete ON Athlete.id = Day.AthleteID
WHERE
(ExerciseID = 164 OR ExerciseID = 174)
AND FullName = @athName
AND Day.Date >= @sDate
AND Day.Date <= @eDate
GROUP BY
DATEPART(YEAR, Day.Date), DATEPART(QUARTER, Day.Date);
INSERT INTO @temp (oYear, oQuarter, pull)
SELECT
DATEPART(YEAR, Day.Date),
DATEPART(QUARTER, Day.Date),
MAX(Pred1RMBMF)
FROM
GymLoad
JOIN
Session ON Session.id = GymLoad.SessionID
JOIN
Day ON Day.id = Session.DayID
JOIN
Athlete ON Athlete.id = Day.AthleteID
WHERE
(ExerciseID = 183 OR ExerciseID = 183)
AND FullName = @athName
AND Day.Date >= @sDate
AND Day.Date <= @eDate
GROUP BY
DATEPART(YEAR, Day.Date), DATEPART(QUARTER, Day.Date);
SELECT * FROM @Temp
現在返される結果:
oYear oQuarter chin press pull
------------------------------------------
2017 1 104.02 NULL NULL
2017 2 102.22 NULL NULL
2017 3 100.54 NULL NULL
2016 4 104.3 NULL NULL
2017 1 NULL 81.61 NULL
2017 2 NULL 71.26 NULL
2017 3 NULL 75.86 NULL
2016 4 NULL 80 NULL
2017 1 NULL NULL 91.95
2017 2 NULL NULL 68.97
2017 3 NULL NULL 73.75
2016 4 NULL NULL 85.33
すべてのヘルプは、最も高く評価されるだろう。
どのdbmsを使用していますか? (コードは製品固有のものです) – jarlh