SBO Layout bring random record incase of draft or unadded documents - crystal-reports

am having a layout which is working fine , except for one part ,
If we use this layout to view or print any added document (AR Invoice) it will ring the write data and records.
but if the user click view or print before adding the document to the system , or if the document was saved as draft then : a random records will come into the layout , in a very randomly way , meaning if close the view and open it again another record different than the precious shown one will come .
What am looking for is simple as i thought , if a user tried to view or print a document thats not added into system then show blank page ,
any help ?
here is the query am using in CR
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate, OINV.DocEntry
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}

the solution is to use ObjectId to determine if the source is system document or draft . this is it
if {?ObjectId#} =13
SELECT ohem.[U_Employee_Code] as'Cashier',OUSR.USER_CODE, OUSR.U_NAME,OUDG.[Code],OUDG.Phone1,OUDG.Phone2, OINV.DocEntry,oinv.docnum, OINV.DocType, OINV.DocStatus, OINV.InvntSttus, OINV.DocDate, OINV.DocDueDate, OINV.CardCode, nnm1.seriesname,
OINV.Address2, OINV.Printed, OINV.CardName, OINV.U_Address, OINV.NumAtCard, OINV.VatSum, OINV.DiscPrcnt, OINV.DiscSum, OINV.DocTotal, OINV.DocRate, OINV.u_deldate,
OINV.ReceiptNum, OINV.GroupNum, OINV.OwnerCode,OINV.JrnlMemo, OINV.VatPaid, OINV.Address2 AS Expr2, OINV.U_CUSTNAME1 as 'Customer Name' , OINV.U_PHONE1, OINV.ExepAmnt, OINV.ExepAmntSC, OINV.ExepAmntFC, OINV.VatDate,OINV.TotalExpns,
OINV.PaidSum, OINV.OwnerCode, OINV.BillToOW, OINV.ShipToOW, OINV.RetInvoice, OINV.ReqName, OINV.Requester, INV1.ItemCode, INV1.Dscription, INV1.Quantity, inv1.PriceBefDi,
INV1.SubCatNum, INV1.ShipDate, INV1.Price, INV1.Currency, INV1.Rate, INV1.DiscPrcnt AS Expr1, INV1.LineTotal, INV1.OpenSum, INV1.VatPrcnt, INV1.PriceAfVAT,
INV1.unitMsr, OINV.TaxDate, INV1.GTotal, OINV.Comments, OCTG.PymntGroup, OINV.LicTradNum, INV1.BaseDocNum, INV1.LineVat, OINV.WTSum, oinv.u_cancelled,
OCRN.CurrName, oslp.SlpName,OUDG.Phone1,OUDG.phone2,
oinv.u_empid as'Sales man',OUDG.Phone1 as 'Branch Phone',OUDG.phone2 as 'Customer Support Phone',OINV.U_PHONE1 as 'Customer Phone',inv1.shipdate as 'Item Delivery Date', OINV.TotalExpns AS 'FREIGHT CHARGES',OINV.U_Address as 'Customer Address',oinv.UpdateDate
FROM OINV INNER JOIN
INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN
OUSR ON OINV.UserSign = OUSR.USERID INNER JOIN
OCTG ON OINV.GroupNum = OCTG.GroupNum INNER JOIN
OCRN ON OINV.DocCur = OCRN.CurrCode inner join
oslp on oinv.SlpCode = oslp.SlpCode INNER JOIN
OUDG ON OUSR.[DfltsGroup] = OUDG.[Code] inner join
nnm1 on OINV.series = nnm1.series LEFT JOIN
OHEM on OUSR.[userId] = OHEM.[USERID]
where OINV.DocEntry = {?DocKey#}
else select ' '

Related

Highlighting duplicate values

I have the following script:
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber)
Reference.LastName
Reference.FirstName
Address.ReferenceNumber AS [Address.ReferenceNumber]
Address.Address1
Address.Address2
Address.Address3
Address.Address4
Address.ZipCode
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber]
Telephone.TelephoneNumber
Email.ReferenceNumber AS [Email.ReferenceNumber]
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
This pulls through all customers, plus their addresses, email and phone numbers.
However some have duplicate surnames and addresses.
Please can you advise an amendment to the script that would show which records were duplicates? Ideally I'd like the output to show where the surname, address 1 and the zipcode were identical and restrict the output to the duplicate rows only.
Many thanks.
something like
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber),
case when vrln.c is null then Reference.LastName else 'dup: ' + Reference.LastName end as LastName,
Reference.FirstName,
Address.ReferenceNumber AS [Address.ReferenceNumber],
Address.Address1,
Address.Address2,
Address.Address3,
Address.Address4,
Address.ZipCode,
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber],
Telephone.TelephoneNumber,
Email.ReferenceNumber AS [Email.ReferenceNumber],
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
left join (
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
) vrln on Reference.LastName = vrln.LastName and Address.Address1 = vrln.Address1 and Address.ZipCode = vrln.ZipCode
where vrln.c is not null
but, to find duplicates :
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
is enough

Possibly this Ibm i VIEW is creating duplicated records?

I am seeing what seems to be duplicate records from this view. Do you think this View is causing duplicate records?
create view astccdta.acaudia AS
( select d.IARCC9,b.ItQty#,a.IhDocd,b.ItPRT#
from astdta.OEINHDIH a
join astdta.ICDETLIT b on
b.Itcom# = a.IhCom#
and
b.ItTrn# = a.IhIdc#
left join astdta.ICECTLCT c on
c.CtCom#= b.ItCom# and
c.CtCtr# = ' ' and
c.CtPrt# =b.ItPrt#
left join astdta.Audia d
on d.IAprt# = b.ItPrt#
where b.ItTscc = 'I' and a.IHDOCD > 20120105
and d.IARCC9 > ''
)

Cannot get view to work on SQL Server

I am trying to create a view that includes columns froms several tables.
This is what it looks like:
And this is my query:
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN
Billing.WebPortalBilling ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
WHERE
(Billing.WebPortalBilling.IsActive = 1)
It's only returning 1 record, which is not correct. I'm trying to tie the Customer's name back to the WebPortalBilling table along with the account number and branth in the other two tables.
I'm new to sql, so be kind.
Thanks!
As commented the where is killing the outer
Try
SELECT
Billing.WebPortalBilling.WebPortalBillingId,
Billing.WebPortalBilling.CorporationId,
Billing.WebPortalBilling.TokenId,
Billing.WebPortalBilling.GatewaySupportFee,
Billing.WebPortalBilling.GatewayPerTransactionFee,
Billing.WebPortalBilling.PortalPerCustomerFee,
Billing.WebPortalBilling.PortalSupportFee,
Customer.Account.AccountNumber,
Billing.WebPortalBilling.IsActive,
Customer.Customer.Name,
Customer.Customer.TaxCode,
Company.CorporationStructure.Branch
FROM
Company.CorporationStructure
RIGHT OUTER JOIN
Customer.Account ON Company.CorporationStructure.CorporationStructureId = Customer.Account.CorporationStructureId
RIGHT OUTER JOIN
Customer.Customer ON Company.CorporationStructure.Branch = Customer.Customer.Branch
RIGHT OUTER JOIN Billing.WebPortalBilling
ON Customer.Account.CorporationId = Billing.WebPortalBilling.CorporationId
AND Billing.WebPortalBilling.IsActive = 1
Try this, I think left joins are clearer.
SELECT
B.WebPortalBillingId,
B.CorporationId,
B.TokenId,
B.GatewaySupportFee,
B.GatewayPerTransactionFee,
B.PortalPerCustomerFee,
B.PortalSupportFee,
C.AccountNumber,
B.IsActive,
C.Name,
C.TaxCode,
CS.Branch
FROM Customer.Customer C
LEFT JOIN Company.CorporationStructure CS ON CS.Branch = C.Branch
LEFT JOIN Customer.Account A ON CS.CorporationStructureId = A.CorporationStructureId
LEFT JOIN Billing.WebPortalBilling B ON A.CorporationId = B.CorporationId
WHERE B.IsActive = 1

Postgres Complex Select in a View

I have this select clause that is working perfect:
SELECT
"Aspectos"."ID" AS "Aspecto Normativo ID",
"Aspectos"."Aspecto" AS "Aspecto Normativo",
"Fatores"."ID", "Fatores"."Fator" AS "Fator Normativo",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator" AS "Fator Determinativo",
"Aspectos_1"."ID" AS "Aspecto Determinativo ID",
"Aspectos_1"."Aspecto" AS "Aspecto Determinativo",
Count("Itens"."ID") AS "No Itens",
Count("Itens"."ID") AS "Pri"
FROM "Diagnostico" INNER JOIN ("Aspectos" AS "Aspectos_1"
INNER JOIN (("Fontes" INNER JOIN "Itens" ON "Fontes"."ID" = "Itens"."Fonte")
INNER JOIN ("Fatores" AS "Fatores_1"
INNER JOIN ("Aspectos"
INNER JOIN ("Vinculos"
INNER JOIN "Fatores"
ON "Vinculos"."Fator_Normativo" = "Fatores"."ID")
ON ("Aspectos"."ID" = "Fatores"."Aspecto")
AND ("Aspectos"."ID" = "Fatores"."Aspecto"))
ON "Fatores_1"."ID" = "Vinculos"."Fator_Determinativo")
ON "Itens"."ID" = "Vinculos"."Item")
ON "Aspectos_1"."ID" = "Fatores_1"."Aspecto")
ON "Diagnostico"."ID" = "Vinculos"."Diagnostico_ID"
GROUP BY "Aspectos"."ID", "Aspectos"."Aspecto",
"Fatores"."ID", "Fatores"."Fator",
"Diagnostico"."Vinculo_Final",
"Fatores_1"."ID",
"Fatores_1"."Fator",
"Aspectos_1"."ID",
"Aspectos_1"."Aspecto"
ORDER BY "Aspectos"."ID", "Aspectos_1"."ID",
"Fatores"."Fator", "Fatores_1"."Fator";
But when I try to CREATE A VIEW with this same select I'm getting thuis error:
ERROR: column "ID" specified more than one time
Can anybody help me on this.
Thanks
You have "Fatores"."ID" (line 4) and "Fatores_1"."ID" (line 6). Give them different aliases.
For such complex queries it is recommended to have only 1 (one) column in per line in the statement for better visibility. Also it is recommended to always give aliases to the columns.

customize the prospects exports of a prospectlist in SugarCrm

I using SugarCRM and when i exports prospects I can get all the fields, including the custom filed I added. but when i try to download the prospects from prospect list then only a few fields are exporting from the database. I need to get all the fields like the prospect download
I understand that Prospect_list.php file contains the code for exporting this one and I changed this one but database failure happening please help me to do this one.
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS \"name\", p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = $record_id AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";
here the sugar team specified the fields to export but i need to get all the fields in the table[prospects] how to do this one.
Trying changing the query to this...
$prospects_query = "SELECT p.id AS id, 'Prospects' AS related_type, '' AS 'name', p.first_name AS first_name, p.last_name AS last_name,p.title AS title, p.salutation AS salutation,
p.primary_address_street AS primary_address_street,p.primary_address_city AS primary_address_city, p.primary_address_state AS primary_address_state, p.primary_address_postalcode AS primary_address_postalcode, p.primary_address_country AS primary_address_country,
p.account_name AS account_name,
ea.email_address AS email_address, ea.invalid_email AS invalid_email, ea.opt_out AS opt_out, ea.deleted AS ea_deleted, ear.deleted AS ear_deleted, ear.primary_address AS primary_address,
p.do_not_call AS do_not_call, p.phone_fax AS phone_fax, p.phone_other AS phone_other, p.phone_home AS phone_home, p.phone_mobile AS phone_mobile, p.phone_work AS phone_work , p.description As Description
FROM prospect_lists_prospects plp
INNER JOIN prospects p ON plp.related_id=p.id
LEFT JOIN email_addr_bean_rel ear ON ear.bean_id=p.id AND ear.deleted=0
LEFT JOIN email_addresses ea ON ear.email_address_id=ea.id
WHERE plp.prospect_list_id = '$record_id' AND plp.deleted=0
AND p.deleted=0
AND (ear.deleted=0 OR ear.deleted IS NULL)";