テンポラリワーカー、パーマネントワーカー、および過去20年間のDATE形式の日付の3つの異なる結果セットを取得するために、 。COUNT機能を含む3つのテンポラリテーブルの情報を1つに照合する方法
> -- Temp Table for Contract Entries
SELECT *
INTO #temp
FROM
(Select U.Id,
UWH.Role,
USI.Title,
Cast(USI.StartDate As Date) AS StartDate,
Cast(USI.EndDate As Date) AS EndDate
From UserWorkHistory UWH
Join Users U On UWH.UserId = U.Id
Join UserStoryItems USI On U.Id = USI.UserId
Join UserWorkHistoryTypes UWHT On UWH.UserWorkHistoryTypeId = UWHT.Id
Where
(
U.Location Like '%Great Britain%'
Or U.Location Like '%United Kingdom%'
Or U.Location Like '%England%'
Or U.Location Like '%UK'
Or U.Location Like '%U.K.%'
)
and UWHT.Id = 1
And USI.UserStoryItemTypeId = 1
And Cast(USI.StartDate As Date) > DateAdd(Year, -20, GetDate())
And
(
(
UWH.Role LIKE '%Contract%'
Or UWH.Role LIKE '%Contractor%'
Or UWH.Role LIKE '%Freelance%'
Or UWH.Role LIKE '%Non-perm%'
Or UWH.Role LIKE '%non-permanent%'
)
Or
(
USI.Title LIKE '%Contract%'
Or USI.Title LIKE '%Contractor%'
Or USI.Title LIKE '%Freelance%'
Or USI.Title LIKE '%Non-perm%'
Or USI.Title LIKE '%non-permanent%'
Or USI.Title LIKE '%self-made%'
)
)
) AS EntriesContract
>-- Temp Table for Perm Entries
SELECT *
INTO #temp2
FROM
(Select U.Id,
UWH.Role,
USI.Title,
Cast(USI.StartDate As Date) AS Startdate,
Cast(USI.EndDate As Date) AS EndDate
From UserWorkHistory UWH
Join Users U On UWH.UserId = U.Id
Join UserStoryItems USI On U.Id = USI.UserId
Join UserWorkHistoryTypes UWHT On UWH.UserWorkHistoryTypeId = UWHT.Id
Where
(
U.Location Like '%Great Britain%'
Or U.Location Like '%United Kingdom%'
Or U.Location Like '%England%'
Or U.Location Like '%UK%'
Or U.Location Like '%U.K.%'
)
and UWHT.Id = 1
And USI.UserStoryItemTypeId = 1
And Cast(USI.StartDate As Date) > DateAdd(Year, -20, GetDate())
And
(
(
UWH.Role NOT LIKE '%Contract%'
Or UWH.Role NOT LIKE '%Contractor%'
Or UWH.Role NOT LIKE '%Freelance%'
Or UWH.Role NOT LIKE '%Non-perm%'
Or UWH.Role NOT LIKE '%non-permanent%'
)
Or
(
USI.Title NOT LIKE '%Contract%'
Or USI.Title NOT LIKE '%Contractor%'
Or USI.Title NOT LIKE '%Freelance%'
Or USI.Title NOT LIKE '%Non-perm%'
Or USI.Title NOT LIKE '%non-permanent%'
Or USI.Title NOT LIKE '%self-made%'
)
)
)AS EntriesPerm
>-- Create Temp Table of Dates
SELECT *
INTO #temp3
FROM
(
SELECT DISTINCT CAST(USI.StartDate AS Date) AS Dates
FROM UserStoryItems USI
WHERE CAST(USI.StartDate AS Date) BETWEEN '1997-01-01 00:00:00' AND '2017-01-01 00:00:00'
)
AS Dates
<
私は(どちらか一時テーブルまたはSELECTクエリを使用するなど)私の3つの列与える結果の第4テーブルを作成したいと思います: 1)第三一時テーブルに返された日付を(# TEMP3)
2)一時的な労働者テーブル(#1 TEMP1からCOUNT(U.Id))であれば、最初の列に返される日付は、開始日と終了日
3)との間にあるように2)COUNT最初の列に返された日付がStartDateとEndDateの間である限り、Permanent Workersテーブル(#temp2)からのデータ(U.Id)を返します
目的は、たとえば2009-09-01 00:00:00に何人の常勤労働者があったのかを教えてくれるテーブルを得ることです。最終的に私は結果をグラフに入れたいと思います。
次の手順に問題があります。私の最初の試みでした:
INSERT INTO #temp4 (Dates, CountTemp, CountPerm)
SELECT #temp3.Dates, COUNT(#temp.Id), COUNT(#temp2.Id)
FROM #temp
JOIN #temp2 ON #temp.StartDate=#temp2.StartDate
JOIN #temp3 ON #temp.StartDate=#temp3.StartDate
WHERE #temp3.Dates BETWEEN #temp2.StartDate AND #temp3.StartDate
GROUP BY #temp3.Dates
ORDER BY #temp3.Dates DESC
しかし、エラー「無効な列名 『開始日』無効な列名 『開始日』無効なオブジェクト名 『#のTEMP4』。。。」返されました。
提案がありますか?私の無知を許して、私はスタックとSQLに質問を投稿するのが初めてです。
よくある質問3「StartDate」という名前のフィールドはありません。どうやってジョーニング?それは日付と同じですか? – Zeina
downvoterに - あなたのフィードバックに感謝します。将来の貢献をより良くするために、なぜこれが悪い質問であるかを詳しく説明できますか?私はコミュニティの強力な部分になりたい。ありがとう。 – BadAtCoding
こんにちはZeina - そうです、多分これは問題です。 StartDates(列の名前)の代わりに#temp.Cast(USI.StartDate As Date)を入れる必要がありますか? – BadAtCoding