Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help to modify this query

Author  Topic 

nicky_river
Yak Posting Veteran

55 Posts

Posted - 2010-10-08 : 02:50:20
Hi Everyone,

I need to display the report datewise i.e by "Date_of_Matching" and also I need to give the user the option of "from date" and "TO date", with "from date" and "to date" can be the same date. the below query is used to generate a report in SSRS. how do i modify the code below to achieve thse 2 tasks ?

------------------------------------------------------------------
select to_char(dt.date_application,'dd-mm-yyyy') Date_of_Matching,
ins.underlying_display_name Commodity_Name,
ins.symbol Contract_Name,
dtd.clearing_member_code CM_ID_Seller,
dtd.member_code TM_ID_Seller,
(select name from members m where m.member_code = dtd.clearing_member_code) CM_Name_Seller,
(select name from members m where m.member_code = dtd.member_code) TM_Name_Seller,
dtd.buy_sell Category_Seller,
la.balance Open_Position_in_Lots_Seller,
dt.quantity MatchedQty_in_Lots_Seller,
ih.delivery_settlement_price Rate_S,
dt.value Delivery_Amount_S,
to_char(dt.date_cash_pay_out,'dd-mm-yyyy') Funds_Settlement_Date_S,
to_char(dt.date_cash_pay_in,'dd-mm-yyyy') Delivery_Settlement_Date_S,
BUYER.clearing_member_code CM_ID_Buyer,
BUYER.member_code TM_ID_Buyer,
(select name from members m where m.member_code = BUYER.clearing_member_code) CM_Name_Buyer,
(select name from members m where m.member_code = BUYER.member_code) TM_Name_Buyer,
BUYER.buy_sell Category_Buyer,
BUYER.balance Open_Position_in_Lots_Buyer,
BUYER.quantity MatchedQty_in_Lots_Buyer,
BUYER.delivery_settlement_price Rate,
BUYER.value Delivery_Amount,
to_char(BUYER.date_cash_pay_out,'dd-mm-yyyy') Funds_Settlement_Date,
to_char(BUYER.date_cash_pay_in,'dd-mm-yyyy') Delivery_Settlement_Date
from DELIVERY_TRANSACTIONS dt,
INSTRUMENTS ins,
DELIVERY_TRANSACTION_DETAILS dtd,
LEDGER_ACCOUNTS la,
INSTRUMENT_HISTORIES IH,
(select dt1.date_application,
dt1.delivery_reference,
ins1.instrument_id, ins1.underlying_display_name,
ins1.symbol,
dtd1.clearing_member_code,
dtd1.member_code,
dtd1.buy_sell,
la1.balance,
dt1.quantity,
ih1.delivery_settlement_price,
dt1.value,
dt1.date_cash_pay_out,
dt1.date_cash_pay_in
from DELIVERY_TRANSACTIONS dt1,
INSTRUMENTS ins1,
DELIVERY_TRANSACTION_DETAILS dtd1,
LEDGER_ACCOUNTS la1,
INSTRUMENT_HISTORIES IH1
where dt1.instrument_id = ins1.instrument_id
and dt1.delivery_reference = dtd1.delivery_reference
and dt1.date_application = dtd1.date_application
and la1.member_code = dtd1.clearing_member_code
and la1.member_client_reference = dtd1.member_client_reference
and la1.instrument_id = ins1.instrument_id
and IH1.DATE_MARKET = dt1.date_application
and IH1.INSTRUMENT_ID = ins1.instrument_id
and dtd1.buy_sell = 'B') BUYER
where dt.instrument_id = ins.instrument_id
and dt.delivery_reference = dtd.delivery_reference
and dt.date_application = dtd.date_application
and la.member_code = dtd.clearing_member_code
and la.member_client_reference = dtd.member_client_reference
and la.instrument_id = ins.instrument_id
and IH.DATE_MARKET = dt.date_application
and IH.INSTRUMENT_ID = ins.instrument_id
and dtd.buy_sell = 'S'
and BUYER.date_application = dt.date_application
and BUYER.delivery_reference = dt.delivery_reference
and BUYER.instrument_id = ins.instrument_id
-------------------------------------------------------------------

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-10-08 : 03:19:07
quote:
Originally posted by nicky_river

Hi Everyone,

I need to display the report datewise i.e by "Date_of_Matching"



USE :

ORDER BY Date_of_Matching at the end of your query to get the result datewise.
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-10-08 : 03:22:36
quote:
Originally posted by nicky_river

Hi Everyone,

also I need to give the user the option of "from date" and "TO date", with "from date" and "to date" can be the same date. the below query is used to generate a report in SSRS.



Can you please be specific about the column on which you want to apply the from date and to date option? I can see lot of Datetime columns in your query eg:- date_application, DATE_MARKET, date_cash_pay_out, date_cash_pay_in
Go to Top of Page

nicky_river
Yak Posting Veteran

55 Posts

Posted - 2010-10-08 : 03:35:08
Hi,

I would like to specify "from date" and "to date" for the column "date_application".

Thanks in advance
Go to Top of Page

rohitvishwakarma
Posting Yak Master

232 Posts

Posted - 2010-10-08 : 03:47:13
try this:


DECLARE @sql VARCHAR(MAX)
DECLARE @from_date DATETIME
DECLARE @to_date DATETIME
SET @from_date = '2010-08-21'
SET @to_date = '2010-10-23'
SET @sql = '

SELECT to_char(dt.date_application,''dd-mm-yyyy'') Date_of_Matching,
ins.underlying_display_name Commodity_Name,
ins.symbol Contract_Name,
dtd.clearing_member_code CM_ID_Seller,
dtd.member_code TM_ID_Seller,
( SELECT name FROM members m WHERE m.member_code = dtd.clearing_member_code ) CM_Name_Seller,
( SELECT name FROM members m WHERE m.member_code = dtd.member_code ) TM_Name_Seller,
dtd.buy_sell Category_Seller,
la.balance Open_Position_in_Lots_Seller,
dt.quantity MatchedQty_in_Lots_Seller,
ih.delivery_settlement_price Rate_S,
dt.value Delivery_Amount_S,
to_char(dt.date_cash_pay_out,''dd-mm-yyyy'') Funds_Settlement_Date_S,
to_char(dt.date_cash_pay_in,''dd-mm-yyyy'') Delivery_Settlement_Date_S,
BUYER.clearing_member_code CM_ID_Buyer,
BUYER.member_code TM_ID_Buyer,
( SELECT name FROM members m WHERE m.member_code = BUYER.clearing_member_code ) CM_Name_Buyer,
( SELECT name FROM members m WHERE m.member_code = BUYER.member_code ) TM_Name_Buyer,
BUYER.buy_sell Category_Buyer,
BUYER.balance Open_Position_in_Lots_Buyer,
BUYER.quantity MatchedQty_in_Lots_Buyer,
BUYER.delivery_settlement_price Rate,
BUYER.value Delivery_Amount,
to_char(BUYER.date_cash_pay_out,''dd-mm-yyyy'') Funds_Settlement_Date,
to_char(BUYER.date_cash_pay_in,''dd-mm-yyyy'') Delivery_Settlement_Date
FROM DELIVERY_TRANSACTIONS dt,
INSTRUMENTS ins,
DELIVERY_TRANSACTION_DETAILS dtd,
LEDGER_ACCOUNTS la,
INSTRUMENT_HISTORIES IH,
(
SELECT dt1.date_application,
dt1.delivery_reference,
ins1.instrument_id, ins1.underlying_display_name,
ins1.symbol,
dtd1.clearing_member_code,
dtd1.member_code,
dtd1.buy_sell,
la1.balance,
dt1.quantity,
ih1.delivery_settlement_price,
dt1.value,
dt1.date_cash_pay_out,
dt1.date_cash_pay_in
FROM DELIVERY_TRANSACTIONS dt1,
INSTRUMENTS ins1,
DELIVERY_TRANSACTION_DETAILS dtd1,
LEDGER_ACCOUNTS la1,
INSTRUMENT_HISTORIES IH1
WHERE dt1.instrument_id = ins1.instrument_id
and dt1.delivery_reference = dtd1.delivery_reference
and dt1.date_application = dtd1.date_application
and dt1.date_application BETWEEN '''+CAST(@from_date AS VARCHAR(15))+''' AND '''+CAST(@to_date AS VARCHAR(15))+'''
and la1.member_code = dtd1.clearing_member_code
and la1.member_client_reference = dtd1.member_client_reference
and la1.instrument_id = ins1.instrument_id
and IH1.DATE_MARKET = dt1.date_application
and IH1.INSTRUMENT_ID = ins1.instrument_id
and dtd1.buy_sell = ''B''
) BUYER
WHERE dt.instrument_id = ins.instrument_id
and dt.delivery_reference = dtd.delivery_reference
and dt.date_application = dtd.date_application
and dt.date_application BETWEEN '''+CAST(@from_date AS VARCHAR(15))+ ''' AND ''' +CAST(@to_date AS VARCHAR(15)) + '''
and la.member_code = dtd.clearing_member_code
and la.member_client_reference = dtd.member_client_reference
and la.instrument_id = ins.instrument_id
and IH.DATE_MARKET = dt.date_application
and IH.INSTRUMENT_ID = ins.instrument_id
and dtd.buy_sell = ''S''
and BUYER.date_application = dt.date_application
and BUYER.delivery_reference = dt.delivery_reference
and BUYER.instrument_id = ins.instrument_id
ORDER BY dt.date_application
'


-- PRINT @sql -- uncomment this to check whether the query is formed correctly
EXEC(@sql)
Go to Top of Page

nicky_river
Yak Posting Veteran

55 Posts

Posted - 2010-10-08 : 08:12:34
Hi,

When I am entering values for "from date" and "to date" as '30-09-2010' and '01-10-2010' in SSRS, it is giving an error "ORA-01722:invalid number" how do i resolve this error ?


and dt.date_application BETWEEN to_char(:From_Date,'dd-mm-yyyy') AND to_char(:To_Date,'dd-mm-yyyy')

Thanks
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2010-10-08 : 08:51:38
Any solution given here would be for MICROSOFT SQL SERVER. You are on an ORACLE server. Try asking here: http://www.dbforums.com/oracle/

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -