100% found this document useful (1 vote)
1K views7 pages

SQL Server Question Paper - 4

Dbo.sysobjects has a table named "trans" if table exists, drop table "trans" and use query analyzer to query it. Query analyzer returns a list of all the records in the table.

Uploaded by

api-3766129
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views7 pages

SQL Server Question Paper - 4

Dbo.sysobjects has a table named "trans" if table exists, drop table "trans" and use query analyzer to query it. Query analyzer returns a list of all the records in the table.

Uploaded by

api-3766129
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

if exists (select * from dbo.

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


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

CREATE TABLE Trans


(
CustomerID INT FOREIGN KEY REFERENCES Customer(CustomerID),
TransDate DATETIME,
TransType CHAR(1),
Amount MONEY
)
GO

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(1, '01/01/2005', 'C', 2000)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(1, '02/01/2005', 'C', 2200)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(1, '03/02/2005', 'D', 2000)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(2, '01/01/2005', 'C', 2000)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(2, '02/01/2005', 'D', 234.223)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(2, '04/01/2005', 'C', 2000)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(3, '02/20/2005', 'D', 200.2)

INSERT INTO Trans(CustomerID, TransDate, TransType)


VALUES(3, '02/21/2005', 'D')

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(3, '03/31/2005', 'C', 334.5452)

INSERT INTO Trans(CustomerID, TransDate, TransType, Amount)


VALUES(4, '04/30/2005', 'C', 332.452)
GO
select * from trans

Write the following queries using query analyzer

1. Display Trans Table records with following columns


CustomerID, TransDate, WeekDay, TransType, Amount
Where TransType should display “Credit” for “C” and “Debit for “D”
and WeekDay is TransDate of WeekDay,
e.g. Today’s Date is “Apr 17 2006” and Today WeekDay is “Monday”
2. Display Trans Table records with following columns
CustomerID, TransDate, Credit Amount, Debit Amount,
Where Credit Amount is Amount When TransType = ‘C’
Where Debit Amount is Amount When TransType = ‘D’
Display Null Value as “0.00”
Display Amount with Two Decimal.
Display TransDate with “MM/dd/yyyy” Format
3. Same 2, but display the records where TransDate Month belongs to “4” month
and Year = 2005
4. Same 2, but display the records where TransDate is StartDate of the Month
5. Same 2, but display the records where TransDate is EndDate of the Month
6. Display Trans Table records with following columns
CustomerID, Year, Credit Total Amount, Debit Total Amount
I need Total for each CustomerID, Year
Where Credit Total Amount and Debit Total Amount is Sum of Amount
Null Amount should display as 0.00
7. Display Trans Table records with following columns
CustomerID, Year, Month, Credit Total Amount, Debit Total Amount, Balance
I need Total for each CustomerID, Year, Month
Where Credit Total Amount and Debit Total Amount is Sum of Amount
Where Balance is Credit Total Amount - Debit Total Amount
Null Amount should display as 0.00
Balance should be greater than 0

Answers:
--1
SELECT
CustomerID, TransDate, DATEName(DW, TransDate) [WeekDay],
CASE WHEN TransType = 'C' THEN 'Credit' WHEN TransType = 'D' THEN
'Debit' END TransType,
Amount
FROM Trans

--2
SELECT
CustomerID, CONVERT(VARCHAR, TransDate, 101) TransDate,
CAST
(
ISNULL
(
CASE
WHEN TransType = 'C' THEN Amount
END,
0.00)
AS VARCHAR)[Credit Amount],
CAST
(
ISNULL
(

CASE
WHEN TransType = 'D' THEN Amount
END,
0.00)
AS VARCHAR) [Debit Amount]
FROM Trans

--3
SELECT
CustomerID, CONVERT(VARCHAR, TransDate, 101) TransDate,
CAST
(
ISNULL
(
CASE
WHEN TransType = 'C' THEN Amount
END,
0.00)
AS VARCHAR)[Credit Amount],
CAST
(
ISNULL
(

CASE
WHEN TransType = 'D' THEN Amount
END,
0.00)
AS VARCHAR) [Debit Amount]
FROM Trans
WHERE MONTH(TransDate) = 4 AND YEAR(TransDate) = 2005
--4
SELECT
CustomerID, CONVERT(VARCHAR, TransDate, 101) TransDate,
CAST
(
ISNULL
(
CASE
WHEN TransType = 'C' THEN Amount
END,
0.00)
AS VARCHAR)[Credit Amount],
CAST
(
ISNULL
(

CASE
WHEN TransType = 'D' THEN Amount
END,
0.00)
AS VARCHAR) [Debit Amount]
FROM Trans
WHERE TransDate = CAST((CAST(MONTH(TransDate) AS VARCHAR) + '/01/' +
CAST(YEAR(TransDate) AS VARCHAR)) AS DATETIME)

--5
SELECT
CustomerID, CONVERT(VARCHAR, TransDate, 101) TransDate,
CAST
(
ISNULL
(
CASE
WHEN TransType = 'C' THEN Amount
END,
0.00)
AS VARCHAR)[Credit Amount],
CAST
(
ISNULL
(

CASE
WHEN TransType = 'D' THEN Amount
END,
0.00)
AS VARCHAR) [Debit Amount]
FROM Trans
WHERE TransDate =
DATEADD(MM, 1, CAST((CAST(MONTH(TransDate) AS VARCHAR) + '/01/'
+ CAST(YEAR(TransDate) AS VARCHAR)) AS DATETIME)) - 1

--6
SELECT
CustomerID, YEAR(TransDate) [Year],
ISNULL
(
SUM
(
CASE
WHEN TransType = 'C' THEN Amount
END
),
0.00) [Credit Total Amount],
ISNULL
(
SUM
(
CASE
WHEN TransType = 'D' THEN Amount
END
),
0.00) [Debit Total Amount]
FROM Trans
GROUP BY CustomerID, YEAR(TransDate)

--7
SELECT
CustomerID, YEAR(TransDate) [Year], MONTH(TransDate) [Date],
ISNULL
(
SUM
(
CASE
WHEN TransType = 'C' THEN Amount
END
),
0.00) [Credit Total Amount],
ISNULL
(
SUM
(
CASE
WHEN TransType = 'D' THEN Amount
END
),
0.00) [Debit Total Amount],

ISNULL
(
SUM
(
CASE
WHEN TransType = 'C' THEN Amount
END
),
0.00) -
ISNULL
(
SUM
(
CASE
WHEN TransType = 'D' THEN Amount
END
),
0.00) [Balance]
FROM Trans
GROUP BY CustomerID, YEAR(TransDate), MONTH(TransDate)
HAVING
(
ISNULL
(
SUM
(
CASE
WHEN TransType = 'C' THEN Amount
END
),
0.00) -
ISNULL
(
SUM
(
CASE
WHEN TransType = 'D' THEN Amount
END
),
0.00)
)>0

You might also like