0% found this document useful (0 votes)
35 views2 pages

Question 1) : Update

The document contains SQL code to: 1) Create databases and tables to store customer, usage, subscription, consumption, and rate data 2) Write a query to sum total consumption quantity by customer for the year 2014 3) Define a function to return the period with the highest consumption quantity for a given customer ID 4) Define a function to return the rate tier with the highest maximum value for a given rate tier ID 5) Define a stored procedure to select periods and quantities from the consumption table where the consumption ID matches a rate tier number

Uploaded by

kjiuuohuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Question 1) : Update

The document contains SQL code to: 1) Create databases and tables to store customer, usage, subscription, consumption, and rate data 2) Write a query to sum total consumption quantity by customer for the year 2014 3) Define a function to return the period with the highest consumption quantity for a given customer ID 4) Define a function to return the rate tier with the highest maximum value for a given rate tier ID 5) Define a stored procedure to select periods and quantities from the consumption table where the consumption ID matches a rate tier number

Uploaded by

kjiuuohuo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

--Question 1}

create database TP2015


use TP2015

create table Abonne (idAbonne int primary key,


nomAb varchar(50),
TelAab varchar ,
pass varchar)

create table Usage(idUsage int primary key,


NomUsaoe varchar(50)
,montantUsage float)

create table Abonnement(idAbonnement int primary key


,dateAb varchar(50)
,idUsage int foreign key references Usage(idUsage)
, idAbonne int foreign key references Abonne (idAbonne))

create table Tranches(NumTranche int identity primary key


,ValMin int
,ValMax int
,PU float)

create table Consommation(idconsommation int primary key


,periode date
,Qte int
,idAbonnement int foreign key references
Abonnement(idAbonnement))

if Not Exists(select * from Tranches where ValMax>500)


insert into Tranches(ValMin,ValMax,PU )values(0,100,0.9010),(101,150,0.9589 ),
(151,200,0.9689),
(201,300,1.0541),(301,500,1.2474),
(501,' ',1.4407)
Else
Update Tranches set Pu=1.4407 where ValMax>500

--Question 2}

select abm.idAbonnement,sum(Qte) Total, ab.nomAb


from Consommation c join Abonnement abm on c.idAbonnement=abm.idAbonnement join
Abonne ab on abm.idAbonne=ab.idAbonne
Where DATEPART(YEAR,periode)='2014' group by abm.idAbonnement,ab.nomAb

--Question 3}

create function Question3(@code int)


returns date
as
begin
return (select periode from Consommation C
Join Consomaton C1 on C.idconsommation=C1.idConsommation
where C.Max(Qte)>C1.Max(Qte) and idAbonnement=@code)

end
--Question 4}

create function Question4(@code int)


returns date
as
begin
return (select NumTranche from Tranches T
Join Tranches T1 on T.NumTranche=T1.NumTranche
where T.Max(ValMax)>T1.Max(ValMax) and NumTranche=@code)

end

--Question 5}
create procedure t5(@Code int)
AS
begin
select periode,Qte from Consommation c where Exists (select NumTranche from
Tranches t where t.NumTranche=c.idconsommation)
Group by t.NumTranche
end

You might also like