sql [sql] CTAS - 当您要创建临时表以执行进一步查询时使用。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql [sql] CTAS - 当您要创建临时表以执行进一步查询时使用。相关的知识,希望对你有一定的参考价值。

------------------------------------------------------------------
-- CREATE A TEMPORARY REGISTERED PLAYERS TABLE TO FURTHER QUERY --
------------------------------------------------------------------

SELECT * 
INTO 
  [RegisteredPlayers] 
FROM 
  [func_GetRegisteredPlayers](@PollID)

-------------------------------------------
-- TEMPORARY TABLE CREATED, NOW QUERY IT --
-------------------------------------------

-- TOTAL REGISTERED PLAYERS (PROFILES) --
SELECT @RegisteredPlayersCount = COUNT([ID]) FROM [RegisteredPlayers]

-- PLAYERS RATIO BY GENDER : MALE --
SELECT @MalePlayersCount = COUNT([ID]) FROM [RegisteredPlayers] WHERE [Gender] = 'male'

-- PLAYERS RATIO BY GENDER : FEMALE --
SELECT @FemalePlayersCount = COUNT([ID]) FROM [RegisteredPlayers] WHERE [Gender] = 'female'

-- PLAYERS RATIO BY AGE GROUP : 18-29 --
SELECT @PlayersAge18Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '18-29'

-- PLAYERS RATIO BY AGE GROUP : 30-49 --
SELECT @PlayersAge30Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '30-49'

-- PLAYERS RATIO BY AGE GROUP : 50-64 --
SELECT @PlayersAge50Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '50-64'

-- PLAYERS RATIO BY AGE GROUP : 65+ --
SELECT @PlayersAge65Count = COUNT([ID]) FROM [RegisteredPlayers] WHERE [AgeGap] = '65'

-------------------------------------------------------------------
-- CLEAN UP DATABASE FROM THE TEMPORARY TABLE (NO LONGER NEEDED) --
-------------------------------------------------------------------

DROP TABLE [RegisteredPlayers]
CREATE FUNCTION [dbo].[func_GetRegisteredPlayers]
(
  @PollID INT
)
RETURNS TABLE
AS
RETURN
(
  SELECT
    [ID],
    [GUID],
    [AgeGap],
    [Gender]
  FROM
    [Players] AS [PL]
  WHERE 
  (
    [GUID] IN
    (
      SELECT
        DISTINCT [MR].[PlayerGUID]
      FROM
        [MatchupResults] AS [MR]
        INNER JOIN [MatchupItems] AS [MI] ON [MI].[ID] = [MR].[SelectedID]
      WHERE 
        [MI].[PollID] = @PollID
    )
  )
)

以上是关于sql [sql] CTAS - 当您要创建临时表以执行进一步查询时使用。的主要内容,如果未能解决你的问题,请参考以下文章

AWS Redshift CTAS 查询在集群查询选项卡中完成,但仍从客户端 sql 工作台/j 运行。该表也未创建

sql 蜂巢CTAS

HiveQL/SQL:CREATE TABLE AS SELECT (CTAS) 和 CREATE EXTERNAL TABLE + INSERT INTO 有啥区别?

Azure Data PlatformDedicated SQL Pool——导入性能测试——CTAS

使用 CTAS 命令后 Netezza 表大小增加

CTAS COPY INTO 的输出