Você está na página 1de 5

if exists (select * from dbo.

sysobjects where id = object_id(N'[Customer]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Customer]
GO

CREATE TABLE Customer


(
CustomerID INT Primary Key,
CustomerName VARCHAR(100),
CustomerType CHAR(1), --S-Saving/C-Current
OpeningBalance MONEY,
DOJ DATETIME
)
GO

INSERT INTO Customer


(
CustomerID, CustomerName, CustomerType, OpeningBalance, DOJ
)
VALUES
(
1, 'Shekar', 'C', 500.00, '01/01/2005'
)

INSERT INTO Customer


(
CustomerID, CustomerName, CustomerType, OpeningBalance, DOJ
)
VALUES
(
2, 'Suman', 'C', 5100.00, '02/01/2005'
)

INSERT INTO Customer


(
CustomerID, CustomerName, CustomerType, OpeningBalance
)
VALUES
(
3, 'Ali', 'S', 100.00
)

INSERT INTO Customer


(
CustomerID, CustomerName, CustomerType, DOJ
)
VALUES
(
4, 'Mohan', 'C', '02/01/2005'
)

INSERT INTO Customer


(
CustomerID, CustomerName, OpeningBalance, DOJ
)
VALUES
(
5, 'Latha', 300.00, '02/01/2005'
)

Write the following queries using query analyzer

1. Display all Customer Records order by CustomerType, CustomerName and don’t


display records which does not possess CustomerType
2. Display # of Customers for each CustomerType
3. Display the records Min OpeningBalance in each CustomerType
4. Display the records Max OpeningBalance in each CustomerType
5. Display the records Sum OpeningBalance in each CustomerType
6. Display the records Avg OpeningBalance in each CustomerType
7. Display the records sum OpeningBalance in each CustomerType Having Sum
OpeningBalance greater than 100
8. Display the records # of customers join based on each Month and Year
i.e. Month, Year, # of Customers
and also Order by Month & Year
9. Display the all columns in which CustomerType should display “Saving” for “S”
and Current for “C”
10. Display all records where opening balance is more than 100 and order by opening
balance

Answers:-
--1)
select * from Customer
where CustomerType=CustomerType
order by CustomerType,customername
--2)
select CustomerType,count(*)
from Customer
where CustomerType=CustomerType
group by CustomerType
--3)
select CustomerType,min(OpeningBalance)
from Customer
where CustomerType=CustomerType
group by CustomerType
--4)
select CustomerType,max(OpeningBalance)
from Customer
where CustomerType=CustomerType
group by CustomerType
--5)
select CustomerType,sum(OpeningBalance)
from Customer
where CustomerType=CustomerType
group by CustomerType
--6)
select CustomerType,avg(OpeningBalance)
from Customer
where CustomerType=CustomerType
group by CustomerType
--7)
select CustomerType,sum(OpeningBalance)
from Customer
group by CustomerType
Having sum(OpeningBalance)>100 and (CustomerType=CustomerType)
--8)
select count(*) as 'number of customers',datename(mm,doj) as
'Month',datename(yy,doj)as 'Year'from customer
where doj=doj
group by datename(mm,doj) ,datename(yy,doj)

--9)
We can get this by table updation only.
--10)
select * from customer
where openingBalance>100 and (openingBalance=openingBalance)
order by openingBalance

Answers :
===============

1. Display all Customer Records order by CustomerType,


CustomerName and don’t display records which does not possess
CustomerType

select * from Customer where CustomerType is not null order by


CustomerType
2. Display # of Customers for each CustomerType

select CustomerType,count(*) as NoOfCustomers from Customer


where CustomerType is not null group by CustomerType

3. Display the records Min OpeningBalance in each CustomerType

select * from Customer


select CustomerType,min(OpeningBalance)as MinOFOpeningBalance from
Customer
group by CustomerType

4. Display the records Max OpeningBalance in each CustomerType

select CustomerType,max(OpeningBalance)as MaxOFOpeningBalance from


Customer
group by CustomerType

5. Display the records Sum OpeningBalance in each CustomerType

select CustomerType,sum(OpeningBalance)as SumOFOpeningBalance from


Customer
group by CustomerType

6. Display the records Avg OpeningBalance in each CustomerType

--select CustomerType,sum(OpeningBalance),count(CustomerType)as
SumOFOpeningBalance from Customer
--group by CustomerType ---reference

select CustomerType,avg(OpeningBalance)as AvgOFOpeningBalance from


Customer
group by CustomerType

7. Display the records sum OpeningBalance in each CustomerType


Having Sum OpeningBalance greater than 100

select CustomerType,sum(OpeningBalance)as SumOFOpeningBalance from


Customer
group by CustomerType
having sum(OpeningBalance)>100

8. Display the records # of customers join based on each Month and


Year
i.e. Month, Year, # of Customers
and also Order by Month & Year

--select * from Customer

select month(DOJ),year(DOJ),count(CustomerType) from Customer


where DOJ is not null group by month(DOJ),year(DOJ),CustomerType

9. Display the all columns in which


CustomerType should display “Saving” for “S” and Current for “C”
select CustomerID ,
CustomerName ,
case when CustomerType='C' then 'Current'
when CustomerType='S' then 'Saving'
end as CustomerType ,
OpeningBalance ,
DOJ

from Customer

10. Display all records where opening balance


is more than 100 and order by opening balance

select * from Customer


where OpeningBalance>100
order by OpeningBalance

Você também pode gostar