Query in a SQL SERVER table

Asked

Viewed 113 times

0

Hello, people. I need to make a query in SQL SERVER but the same is returning incorrect values. I have the following tables.
inserir a descrição da imagem aqui

In the proposed exercise I must return the following information:

  • Extract the smallest, largest, average and sum of sales of each month of the year 2000, in ascending order.
  • Return the same previous query, but only the records that have an average sales of more than 500.

How do I extract sales for each month of the year? Below I will post the SQL code that was used to create the database:

CREATE DATABASE CONCESSIONARIA
GO
USE CONCESSIONARIA
GO

CREATE TABLE TB_ANO
(
        ID_ANO              SMALLINT    IDENTITY        -- 32.767
    ,   ANO                 SMALLINT

        CONSTRAINT  PK_TB_ANO_ID_ANO            PRIMARY KEY (ID_ANO)
    ,   CONSTRAINT  CK_TB_ANO_ID_ANO            CHECK (ID_ANO <= 200)
)

INSERT TB_ANO
( ANO )
VALUES
( 2000 ),
( 2001 ),
( 2002 ),
( 2003 ),
( 2004 ),
( 2005 )

CREATE TABLE TB_MES
(
        ID_MES              SMALLINT    IDENTITY ( 15 , 3 )  -- E O FATO DE NÃO PODER ULTRAPASSAR 200 REGISTROS
    ,   MES                 SMALLINT

        CONSTRAINT  PK_TB_MES_ID_MES            PRIMARY KEY (ID_MES)
    ,   CONSTRAINT  CK_TB_MES_ID_MES            CHECK (ID_MES <= 612) 
)

INSERT TB_MES
( MES )
VALUES
( 01 ),
( 02 ),
( 03 ),
( 04 ),
( 05 ),
( 06 )

CREATE TABLE TB_MODELO
(
        ID_MODELO       SMALLINT    IDENTITY
    ,   DESCRICAO       VARCHAR(50) NOT NULL

        CONSTRAINT  PK_TB_MODELO_ID_MODELO      PRIMARY KEY (ID_MODELO)
    ,   CONSTRAINT  CK_TB_MODELO_ID_MODELO      CHECK (ID_MODELO <= 200)
)

INSERT TB_MODELO
( DESCRICAO )
VALUES
( 'GOL 1.6 MSI' ),
( 'PALIO FIRE 1.0 ' ),
( 'KA 1.6 MPI' ),
( 'RENEGADE STD 1.6' ),
( 'COROLLA ALTIS' ),
( 'CG 125 STD' )

CREATE TABLE TB_FABRICANTE
(
        ID_FABRICANTE   SMALLINT        IDENTITY
    ,   NOME            VARCHAR(50)     NOT NULL
    ,   CIDADE          VARCHAR(50)     NOT NULL
    ,   ENDERECO        VARCHAR(100)    
    ,   UF              CHAR(02)        
    ,   TELEFONE        VARCHAR(20)
    ,   CONTATO         VARCHAR(50)     NOT NULL

        CONSTRAINT  PK_TB_FABRICANTE_ID_FABRICANTE      PRIMARY KEY (ID_FABRICANTE)
    ,   CONSTRAINT  CK_TB_FABRICANTE_ID_FABRICANTE      CHECK (ID_FABRICANTE <= 200)
)

INSERT TB_FABRICANTE
( NOME , CIDADE , ENDERECO , UF , TELEFONE , CONTATO )
VALUES
( 'VOLKSWAGEN' , 'SÃO BERNARDO' , 'RODOVIA ANCHIETA S/N' , 'SP' , '2000-9999' , '[email protected]' ),
( 'FIAT' , 'BETIM' , 'AV. ENG. LUIS CARLOS' , 'MG' , '04571-010' , '[email protected]' ),
( 'FORD' , 'SÃO BERNARDO' , 'AV. TABOÃO' , 'SP' , '2000-9999' , '[email protected]' ),
( 'JEEP' , 'SÃO BERNARDO' , 'AV. SABURÔ' , 'SP' , '09751-510' , '[email protected]' ),
( 'TOYOTA' , 'SOROCABA' , 'ROD. CASTELO BRANCO' , 'SP' , '7770-652' , '[email protected]' ),
( 'HONDA MOTORS' , 'MANAUS' , 'AV. ANHAGUERA' , 'MA' , '2000-555' , '[email protected]' )

CREATE TABLE TB_VEICULO
(
        ID_VEICULO              SMALLINT                IDENTITY
    ,   DESCRICAO               VARCHAR(50)             NOT NULL
    ,   VALOR                   DECIMAL ( 8 , 2 )       NOT NULL -- CHEGARÁ A CASA DOS R$ 200.000,00. COM ESSA DEFINIÇÃO DE CAMPO: R$ 999.999,99
    ,   ID_MODELO               SMALLINT            
    ,   ID_FABRICANTE           SMALLINT
    ,   ID_ANO_FABRICACAO       SMALLINT
    ,   DATA_COMPRA             DATE                    NOT NULL

        CONSTRAINT  PK_TB_VEICULO_ID_VEICULO            PRIMARY KEY (ID_VEICULO)
    ,   CONSTRAINT  CK_TB_VEICULO_ID_VEICULO            CHECK (ID_VEICULO <= 10000)
    ,   CONSTRAINT  FK_TB_VEICULO_ID_MODELO             FOREIGN KEY (ID_MODELO)             REFERENCES  TB_MODELO(ID_MODELO)
    ,   CONSTRAINT  FK_TB_VEICULO_ID_FABRICANTE         FOREIGN KEY (ID_FABRICANTE)         REFERENCES  TB_FABRICANTE(ID_FABRICANTE)
    ,   CONSTRAINT  FK_TB_VEICULO_ID_ANO_FABRICACAO     FOREIGN KEY (ID_ANO_FABRICACAO)     REFERENCES  TB_ANO(ID_ANO)
)

INSERT TB_VEICULO
( DESCRICAO , VALOR , ID_MODELO , ID_FABRICANTE , ID_ANO_FABRICACAO , DATA_COMPRA )
VALUES
( 'GOL'         ,       250000.99 ,     1 , 1 , 1 , '2020-09-15' ),
( 'PALIO'           ,   130000.99 ,     2 , 2 , 2 , '2020-09-05' ),
( 'KA'          ,       40000.50 ,      3 , 3 , 3 , '2005-01-01' ),
( 'RENEGADE'    ,       250000.99 ,     4 , 4 , 4 , '2020-09-15' ),
( 'COROLLA'     ,       102110.99 ,     5 , 5 , 5 , '2020-09-15' ),
( 'CG 125 STD'  ,       222222.99 ,     6,  6 , 6 , '2020-09-15' )

CREATE TABLE TB_VENDAS_ANUAIS
(
        ID_VENDAS           SMALLINT        IDENTITY
    ,   QTD                 SMALLINT        NOT NULL
    ,   ID_VEICULO          SMALLINT
    ,   ID_ANO_DA_VENDA     SMALLINT
    ,   ID_MES_DA_VENDA     SMALLINT

        CONSTRAINT  PK_TB_VENDAS_ANUAIS_ID_VENDAS           PRIMARY KEY(ID_VENDAS)
    ,   CONSTRAINT  CK_TB_VENDAS_ANUAIS_ID_VENDAS           CHECK(ID_VENDAS <= 10000)
    ,   CONSTRAINT  CK_TB_VENDAS_ANUAIS_QTD_MIN             CHECK(QTD >= 1)
    ,   CONSTRAINT  CK_TB_VENDAS_ANUAIS_QTD_MAX             CHECK(QTD <= 5000)

    ,   CONSTRAINT  FK_TB_VENDAS_ANUAIS_ID_VEICULO          FOREIGN KEY(ID_VEICULO)         REFERENCES  TB_VEICULO(ID_VEICULO)
    ,   CONSTRAINT  FK_TB_VENDAS_ANUAIS_ID_ANO_DA_VENDA     FOREIGN KEY(ID_ANO_DA_VENDA)    REFERENCES  TB_ANO(ID_ANO)
    ,   CONSTRAINT  FK_TB_VENDAS_ANUAIS_ID_MES_DA_VENDA     FOREIGN KEY(ID_MES_DA_VENDA)    REFERENCES  TB_MES(ID_MES)
)

INSERT TB_VENDAS_ANUAIS
( QTD , ID_VEICULO , ID_ANO_DA_VENDA , ID_MES_DA_VENDA )
VALUES
( 2222 , 6 , 2 , 21 ),
( 1111 , 6 , 2 , 21 ),
( 1000 , 1 , 1 , 15 ),
( 2000 , 2 , 3 , 18 )
  • Everything indicates the use of incorrect values of the artificial key in the inclusion of TB_VEICULO table records.

  • It was the use of incorrect values of the keys, I arranged them and managed to return the values correctly. Now how can I extract sales from each month of the year?

  • 1

    Study the GROUP BY clause, together with the appropriate aggregation function, as well as the HAVING clause.

  • I got it, thank you. In the first I managed to do it without using HAVING, but in the second I used it. Thanks for the tips.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.