i have question regarding SQL, i am using Crystal report and c#2008.
I am building a report which has two table one is order and other is payment. What i want is :--its a small software for a building contractor, he has couple of building site where few supplier give material, what i want is a Ledger report left side should be the supplier order for perticular supplier 'A' for that site with in the perticular date range and one right side i want is the payment made by my client with in that perticular date range for that site for that perticular supplier 'A' and for that perticular site.
the query i made is as follows :--
SELECT `order`.`orderBillNo`, `order`.`siteName`, `order`.`orderDt`, `order`.`vendorName`, `order`.`totalAmt`, `vendorpayments`.`InvoiceNo`, `vendorpayments`.`Date`, `vendorpayments`.`Paymode`, `vendorpayments`.`amt`, `vendorpayments`.`Chqddno`, `vendorpayments`.`bankname`
FROM `order` , `vendorpayments`
where `order`.`vendorName`='A'
AND `vendorpayments`.`vendor` ='A'
AND `order`.`sitename`='B'
AND `vendorpayments`.`sitename`='B'
AND (`order`.`orderDt`>="2012/02/01" and `order`.`orderDt`<="2012/02/25")
AND (`vendorpayments`.`date`>="2012/02/01" and `vendorpayments`.`date`<="2012/02/25")
i am using ADODB recordset, and using crviewer.setdatasource(adoRS) for showing report to CR. i m using CR8
If i run using this query then report is showing proper result but when i do the sum of the order.totalamout & vendorpayments.amt then it shows sum with suppressed rows as well this is perticular query the order.totalamount should be 7,500 but its showing 30,000. and for vendorpayment it should show two rows with pay of 1,500 and 500 and total should be 2000, but its showing 4 rows and total of 4000,it its taking order.totamt also 4 times so i m getting 30000. I have used the suppressed option but still its not showing correct amout. I want help about how to remove this extra sum. Does there is problem with my query , does my query is giving more more iterations so 4 rows are coming. If any can correct my query iwill be thankful.Its very urgent
thanks in advance.
Loading

Kedar PawgiPosted Mar 1, 2012, 1:23 PM
Thanks for your reply, yes i can try ur query in database. Another thing is i will surely add the ID field for both in the table. hope it will solve my problem. Else i will reply you back tomorrow...hope it will work.
Thanks for your reply again.
ThomasPosted Mar 1, 2012, 11:57 AM
To fix that rather than pull both tables and limit it in the where clause it would be better to write the query using inner joins. (Not sure what database program you are using so the SQL syntax below may vary slightly).
I don't know how much control you have over the tables and table structure but it sounds like you could have a table listing vendors, then an Orders table and finally a Payments table. In the Vendor table you could have a Vendor ID that is unique to each vendor. Then use that ID in the Orders table and in the Payments table.
Then your query could be written like this:
SELECT order.orderBillNo, order.siteName, order.orderDt, Vendor.vendorName, order.totalAmt, payments.InvoiceNo, payments.Date, payments.Paymode, payments.amt, payments.Chqddno, payments.bankname`
FROM vendor
inner join order
on vendor.vendorid = order.vendorid
inner join payments
on vendor.vendorid = payments.vendorid
and Order.siteName = payments.siteName
where vendor.Name='A'
AND order.sitename='B'
AND (order.orderDt between "2012/02/01" and "2012/02/25")
AND (payments.date`between "2012/02/01" and "2012/02/25")
If you can not adjust your tables at all then you can write your query like this
SELECT `order`.`orderBillNo`, `order`.`siteName`, `order`.`orderDt`, `order`.`vendorName`, `order`.`totalAmt`, `vendorpayments`.`InvoiceNo`, `vendorpayments`.`Date`, `vendorpayments`.`Paymode`, `vendorpayments`.`amt`, `vendorpayments`.`Chqddno`, `vendorpayments`.`bankname`
FROM `order`
inner join `vendorpayments`
on `order`.`vendorName`= `vendorpayments`.`vendor`
AND `order`.`sitename` = `vendorpayments`.`sitename`
where `order`.`vendorName`='A'
AND `order`.`sitename`='B'
AND (`order`.`orderDt`>="2012/02/01" and `order`.`orderDt`<="2012/02/25")
AND (`vendorpayments`.`date`>="2012/02/01" and `vendorpayments`.`date`<="2012/02/25")
that SHOULD give you a single row of data for each order and payment. Run the SQL in the database to make sure that is the case. If it is not see if there is another field you did not include here that is different between each of the duplicate records. then you can adjust your where clause or Join to include that field.