Вы находитесь на странице: 1из 3

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'
)

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. Same 2, but display the records where TransDate is EndDate of the Month
2. 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
3. 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

Вам также может понравиться