Subject | Re: [firebird-support] Doubt (Problem) In Writing SQL |
---|---|
Author | Vishal Tiwari |
Post date | 2017-07-05T18:15:29Z |
Hi SET,
Just for you my Friend... Please check...
CREATE TABLE BOOK_SELLING_DETAIL
(
BOOK_NO Integer,
BOOK_ID Integer,
PUBLISHER Varchar(50),
MAIN_BRACH Varchar(10),
PUBLISH_DATE Date,
ORIGINAL_PRICE Float,
SELLING_PRICE Float
);
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '222', 'Willy', 'Branch1', '25.01.2010', '550.000000', '500.000000', '00000086:00000001');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '333', 'Willy', 'Branch1', '25.01.2010', '450.000000', '400.000000', '00000086:00000002');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '777', 'Willy', 'Branch1', '25.01.2010', '300.000000', '250.000000', '00000086:00000003');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '555', 'Willy', 'Branch1', '25.01.2010', '900.000000', '800.000000', '00000086:00000004');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '111', 'Tata Mcgraw Hill', 'Branch1', '25.01.2010', '750.000000', '700.000000', '00000086:00000005');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '666', 'Tata Mcgraw Hill', 'Branch1', '25.01.2010', '250.000000', '200.000000', '00000086:00000006');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '888', 'Tata Mcgraw Hill', 'Branch1', '25.01.2010', '600.000000', '500.000000', '00000086:00000007');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '999', 'Tata Mcgraw Hill', 'Branch1', '25.01.2010', '400.000000', '350.000000', '00000086:00000008');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '222', 'Willy', 'Branch2', '25.01.2010', '550.000000', '500.000000', '00000086:00000009');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '333', 'Willy', 'Branch2', '25.01.2010', '450.000000', '400.000000', '00000086:0000000a');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '777', 'Willy', 'Branch2', '25.01.2010', '300.000000', '250.000000', '00000086:0000000b');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '555', 'Willy', 'Branch2', '25.01.2010', '900.000000', '800.000000', '00000086:0000000c');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('9', '111', 'Tata Mcgraw Hill', 'Branch2', '25.01.2010', '750.000000', '700.000000', '00000086:0000000d');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('9', '666', 'Tata Mcgraw Hill', 'Branch2', '25.01.2010', '250.000000', '200.000000', '00000086:0000000e');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '888', 'Tata Mcgraw Hill', 'Branch2', '25.01.2010', '600.000000', '500.000000', '00000086:0000000f');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('5', '999', 'Tata Mcgraw Hill', 'Branch2', '25.01.2010', '400.000000', '350.000000', '00000086:00000010');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '222', 'Willy', 'Branch1', '30.01.2010', '650.000000', '600.000000', '00000086:00000011');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '333', 'Willy', 'Branch1', '30.01.2010', '750.000000', '700.000000', '00000086:00000012');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '777', 'Willy', 'Branch1', '30.01.2010', '400.000000', '350.000000', '00000086:00000013');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '555', 'Willy', 'Branch1', '30.01.2010', '800.000000', '700.000000', '00000086:00000014');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '111', 'Tata Mcgraw Hill', 'Branch1', '30.01.2010', '750.000000', '600.000000', '00000086:00000015');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '666', 'Tata Mcgraw Hill', 'Branch1', '30.01.2010', '250.000000', '100.000000', '00000086:00000016');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '888', 'Tata Mcgraw Hill', 'Branch1', '30.01.2010', '600.000000', '400.000000', '00000086:00000017');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('9', '999', 'Tata Mcgraw Hill', 'Branch1', '30.01.2010', '400.000000', '250.000000', '00000086:00000018');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '222', 'Willy', 'Branch2', '30.01.2010', '650.000000', '600.000000', '00000086:00000019');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('6', '333', 'Willy', 'Branch2', '30.01.2010', '750.000000', '700.000000', '00000086:0000001a');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '777', 'Willy', 'Branch2', '30.01.2010', '400.000000', '350.000000', '00000086:0000001b');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('7', '555', 'Willy', 'Branch2', '30.01.2010', '800.000000', '700.000000', '00000086:0000001c');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '111', 'Tata Mcgraw Hill', 'Branch2', '30.01.2010', '750.000000', '600.000000', '00000086:0000001d');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '666', 'Tata Mcgraw Hill', 'Branch2', '30.01.2010', '250.000000', '100.000000', '00000086:0000001e');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('8', '888', 'Tata Mcgraw Hill', 'Branch2', '30.01.2010', '600.000000', '400.000000', '00000086:0000001f');
INSERT INTO BOOK_SELLING_DETAIL (BOOK_NO, BOOK_ID, PUBLISHER, MAIN_BRACH, PUBLISH_DATE, ORIGINAL_PRICE, SELLING_PRICE, "DB_KEY") VALUES ('9', '999', 'Tata Mcgraw Hill', 'Branch2', '30.01.2010', '400.000000', '250.000000', '00000086:00000020');
For every Publisher, for its same Branch and for the same date find the Maximum Book_No value,
if there are multiple records of the same maximum Book_No found then
find the Maximum Book_ID value for that maximum Book_No and get all the details of that Book_ID row.
SQL:
SELECT BSD.BOOK_NO, BSD.BOOK_ID, BSD.PUBLISHER, BSD.MAIN_BRACH, BSD.PUBLISH_DATE, BSD.ORIGINAL_PRICE, BSD.SELLING_PRICE
FROM BOOK_SELLING_DETAIL BSD
Inner Join
(
Select BSD1.BOOK_NO, Max(BSD1.BOOK_ID) As BOOK_ID, BSD1.PUBLISHER, BSD1.MAIN_BRACH, BSD1.PUBLISH_DATE from BOOK_SELLING_DETAIL BSD1
Inner join
(
Select Max(BOOK_NO) As BOOK_NO, PUBLISHER, MAIN_BRACH, PUBLISH_DATE from BOOK_SELLING_DETAIL
Group By PUBLISHER, MAIN_BRACH, PUBLISH_DATE
) BSD2
ON
BSD1.PUBLISHER = BSD2.PUBLISHER And BSD1.MAIN_BRACH = BSD2.MAIN_BRACH And BSD1.PUBLISH_DATE = BSD2.PUBLISH_DATE And BSD1.BOOK_NO = BSD2.BOOK_NO
Group By BSD1.PUBLISHER, BSD1.MAIN_BRACH, BSD1.PUBLISH_DATE, BSD1.BOOK_NO
) BSD3
ON
BSD.BOOK_NO = BSD3.BOOK_NO And BSD.BOOK_ID = BSD3.BOOK_ID And BSD.PUBLISHER = BSD3.PUBLISHER And BSD.MAIN_BRACH = BSD3.MAIN_BRACH And BSD.PUBLISH_DATE = BSD3.PUBLISH_DATE
Order By BSD.PUBLISHER, BSD.MAIN_BRACH, BSD.PUBLISH_DATE, BSD.BOOK_NO, BSD.BOOK_ID
With Best Regards.
Vishal
On Sunday, 29 May 2011 8:49 AM, Vishal Tiwari <vishualsoft@...> wrote:
Hi SET,
Extreamely sorry for too late reply, coz was busy in project work.
But I am delighted as usual by reading mail....
I have not gone through you mail, soafter this i would go through yur mail and surly get back to you with some more doubts on this example.
Thanking You And With Best Regards.
To My Master-Trouble Shooter.
Vishal
--- On Wed, 18/5/11, Svein Erling Tysvær <svein.erling.tysvaer@...> wrote:
|