Presentación
https://drive.google.com/open?id=0B7Ct7K_LTJvSbG5xNWJRZDF1MGM
Datos Empleados
https://drive.google.com/open?id=0B7Ct7K_LTJvSeldxQmdjNTQtd1UHoras Extras
https://drive.google.com/open?id=0B7Ct7K_LTJvSdnExQjhCaU1yRlU
Script Completo SistemaNotas2
https://drive.google.com/open?id=0B7Ct7K_LTJvSOHJTTWdZb3dNclkEjemplos
USE [master]
GO
/****** Object:
Database [SistemaNotas] Script
Date: 19/10/2016 2:21:05 p. m. ******/
IF NOT EXISTS
(SELECT
name FROM sys.databases WHERE name = N'SistemaNotas')
BEGIN
CREATE DATABASE [SistemaNotas]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'SistemaNotas',
FILENAME
= N'C:\DatosDS\SistemaNotas.mdf'
,
SIZE =
16384KB , MAXSIZE =
1048576KB ,
FILEGROWTH = 8192KB )
LOG
ON
( NAME = N'SistemaNotas_log',
FILENAME = N'C:\DatosDS\SistemaNotas_log.ldf'
,
SIZE = 8192KB , MAXSIZE = 65536KB ,
FILEGROWTH = 8192KB )
END
GO
Ejemplo CREATE:
USE [SistemaNotas]
GO
/****** Object:
Table [dbo].[Departamentos]
Script Date: 25/10/2016 9:51:10 a. m. ******/
CREATE TABLE [dbo].[Departamentos](
[Codigo] [int] NOT NULL,
[Nombre] [varchar](100) NOT NULL,
CONSTRAINT
[PK_Departamentos] PRIMARY KEY CLUSTERED
(
[Codigo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE =
OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:
Table [dbo].[Empleados] Script
Date: 25/10/2016 9:51:10 a. m. ******/
CREATE TABLE [dbo].[Empleados](
[Codigo] [int] IDENTITY(1,1) NOT NULL,
[CUI] [varchar](13) NOT NULL,
[NombrePrimero]
[varchar](50) NOT NULL,
[NombreSegundo]
[varchar](50) NOT NULL CONSTRAINT [DF_Empleados_NombreSegundo] DEFAULT (''),
[ApellidoPrimero]
[varchar](50) NOT NULL,
[ApellidoSegundo]
[varchar](50) NOT NULL CONSTRAINT [DF_Empleados_ApellidoSegundo] DEFAULT (''),
[FechaNacimiento] [date] NOT
NULL CONSTRAINT
[DF_Empleados_FechaNacimiento] DEFAULT (getdate()),
[CodigoDepartamento] [int] NOT NULL,
[NIT] [varchar](20) NOT NULL CONSTRAINT [DF_Empleados_NIT] DEFAULT (''),
CONSTRAINT
[PK_Empleados] PRIMARY KEY
CLUSTERED
(
[Codigo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE =
OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:
Table [dbo].[HorasExtras]
Script Date: 25/10/2016 9:51:10 a. m. ******/
CREATE TABLE [dbo].[HorasExtras](
[CodigoEmpleado] [int] NOT NULL,
[Fecha] [date] NOT NULL CONSTRAINT [DF_HorasExtras_Fecha] DEFAULT (getdate()),
[Cantidad] [int] NOT NULL,
CONSTRAINT
[PK_HorasExtras] PRIMARY KEY CLUSTERED
(
[CodigoEmpleado] ASC,
[Fecha] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE =
OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Empleados] WITH CHECK ADD CONSTRAINT
[FK_Empleados_Departamentos] FOREIGN KEY([CodigoDepartamento])
REFERENCES [dbo].[Departamentos] ([Codigo])
GO
ALTER TABLE [dbo].[Empleados]
CHECK CONSTRAINT
[FK_Empleados_Departamentos]
GO
ALTER TABLE [dbo].[HorasExtras] WITH CHECK ADD CONSTRAINT
[FK_HorasExtras_Empleados] FOREIGN KEY([CodigoEmpleado])
REFERENCES [dbo].[Empleados] ([Codigo])
GO
ALTER TABLE [dbo].[HorasExtras]
CHECK CONSTRAINT
[FK_HorasExtras_Empleados]
GO
Ejemplo SELECT
USE SistemaNotas2
GO
SELECT NombrePrimero, ApellidoPrimero
FROM Empleados;
SELECT *
FROM Departamentos;
SELECT CodigoDepartamento, NombrePrimero,
ApellidoPrimero
FROM Empleados
WHERE (CodigoDepartamento
= 2
OR CodigoDepartamento =
3)
AND ApellidoPrimero LIKE
'P%';
SELECT *
FROM Empleados
WHERE ApellidoPrimero =
'Palacios';
SELECT CodigoDepartamento, ApellidoPrimero,
NombrePrimero, FechaNacimiento
FROM Empleados
ORDER BY
CodigoDepartamento, ApellidoPrimero;
SELECT FechaNacimiento, ApellidoPrimero,
NombrePrimero
FROM Empleados
WHERE CodigoDepartamento =
2
ORDER BY YEAR(FechaNacimiento) DESC,
ApellidoPrimero;
SELECT Departamentos.Nombre, Empleados.ApellidoPrimero,
Empleados.NombrePrimero
FROM Departamentos
INNER JOIN Empleados
ON Departamentos.Codigo
= Empleados.CodigoDepartamento;
-- Comentario
/*
Comentario
Multilínea
*/
SELECT D.Nombre AS
Departamento, E.ApellidoPrimero,
E.NombrePrimero
FROM Departamentos D
INNER JOIN Empleados
E
ON D.Codigo = E.CodigoDepartamento
ORDER BY D.Nombre;
SELECT Departamentos.Nombre, Empleados.NombrePrimero,
Empleados.ApellidoPrimero,
HorasExtras.Fecha,
HorasExtras.Cantidad
FROM Departamentos
INNER JOIN Empleados
ON Departamentos.Codigo
= Empleados.CodigoDepartamento
INNER JOIN
HorasExtras
ON Empleados.Codigo = HorasExtras.CodigoEmpleado
WHERE YEAR(HorasExtras.Fecha) = 2014
ORDER
BY Departamentos.Nombre;
SELECT COUNT(*) AS Conteo,
SUM(Cantidad) AS Total,
AVG(Cantidad) AS Promedio,
MIN(Cantidad) AS Minimo,
MAX(Cantidad) AS Maximo
FROM
HorasExtras
WHERE
YEAR(Fecha)=2015;
SELECT YEAR(Fecha) AS [Año],
COUNT(*) AS Conteo,
SUM(Cantidad) AS Total
FROM
HorasExtras
GROUP
BY YEAR(Fecha);
SELECT YEAR(Fecha) AS [Año],
MONTH(Fecha) AS Mes,
COUNT(*) AS Conteo,
SUM(Cantidad) AS Total
FROM
HorasExtras
WHERE
CodigoEmpleado BETWEEN 50 AND 450
GROUP
BY YEAR(Fecha), MONTH(Fecha)
HAVING
SUM(Cantidad) > 700
ORDER
BY YEAR(Fecha), MONTH(Fecha);
SELECT Emp.Codigo, Emp.ApellidoPrimero,
Emp.NombrePrimero,
(SELECT AVG(Cantidad) AS Promedio
FROM HorasExtras HE
WHERE HE.CodigoEmpleado
= Emp.Codigo)
AS Promedio
FROM Empleados Emp
WHERE Emp.Codigo BETWEEN
150 AND 200
ORDER BY Emp.ApellidoPrimero, Emp.NombrePrimero;
SELECT AVG(Cantidad) AS Promedio
FROM HorasExtras HE
WHERE HE.CodigoEmpleado = 169;
SELECT CodigoEmpleado,
YEAR(Fecha) AS [Año],
MONTH(Fecha) AS Mes,
SUM(Cantidad) AS Total
FROM
HorasExtras
GROUP
BY CodigoEmpleado,
YEAR(Fecha),
MONTH(Fecha);
No hay comentarios.:
Publicar un comentario