Can someone please give me a lesson in how to achieve the following SQL statement in Crystal Reports?
select Debtor_Assessment_Facts.Assessment
from Debtor_Assessment_Facts
where Debtor_Assessment_Facts.Assessment_Facts_Key not in
(
select Debtor_Transaction_Facts.Assessment_Facts_Key
from Debtor_Transaction_Facts
inner join Debtor_Transaction_Types
on Debtor_Transaction_Facts.Transaction_type_Key = Debtor_Transaction_Types.Transaction_Type_Key
where Debtor_Transaction_Types.Transaction_Type_Code = 'RCPT' and Debtor_Transaction_Facts.Transaction_Date >= DATEADD(year,-6,GETDATE())
group by Debtor_Transaction_Facts.Assessment_Facts_Key
)
and Debtor_Assessment_Facts.Balance <> 0
order by Debtor_Assessment_Facts.Assessment
Open the crystal reports... make a database connection... do add command and paste your sql statement there and proceed with report design
Related
I copied the query directly from Crystal Reports and removed the quotations that crystal includes in their query. No data is returned in SQL Studio. I am not sure what is causing this.
SELECT APTH.Mth, APTH.APTrans, APTH.Vendor, APVM.Name, APTH.APRef,
APTH.InvDate, APTH.DueDate, APWH.UnpaidAmt, APWH.APCo
FROM (APTH
INNER JOIN APVM ON (APTH.Vendor=APVM.Vendor) AND
(APTH.VendorGroup=APVM.VendorGroup))
INNER JOIN APWH ON ((APTH.APCo=APWH.APCo) AND
(APTH.VendorGroup=APWH.VendorGroup)) AND
(APTH.APTrans=APWH.APTrans)
WHERE APWH.UnpaidAmt>0 AND APWH.APCo=100
As the title says, I've built a Crystal Report to pull data from a SQL server. The SQL query that Crystal Reports generates pulls the data as expected when copied into SSMS but pulls no records when the report is run from Crystal Reports.
My record selection:
({PRECEIPTD.CREDAT_0} >= {?startDate} AND {PRECEIPTD.CREDAT_0} <= {?endDate})
AND ISNULL({PINVOICED.NUM_0})
AND IF ISNULL({?bpr}) OR {?bpr} = "" THEN {PORDERQ.BPSNUM_0} = {PORDERQ.BPSNUM_0} ELSE {PORDERQ.BPSNUM_0} = {?bpr}
I've also tried writing the selection like this, which pulls all of the same records:
({PRECEIPTD.CREDAT_0} >= {?startDate} AND {PRECEIPTD.CREDAT_0} <= {?endDate})
AND ISNULL({PINVOICED.NUM_0})
AND IF ISNULL({?bpr}) OR {?bpr} = "" THEN {PORDERQ.BPSNUM_0} LIKE "*" ELSE {PORDERQ.BPSNUM_0} = {?bpr}
I'm using Crystal Reports 2013 SP11, accessing the database through a ODBC connection.
EDIT:
The SQL query Crystal generates:
SELECT
PRECEIPTD.CREDAT_0
, PORDERQ.BPSNUM_0
, PORDERQ.POHNUM_0
, PORDERQ.ITMREF_0
, PORDERQ.QTYPUU_0
, PORDERP.NETPRI_0
, PRECEIPTD.PTHNUM_0
, PINVOICED.NUM_0
FROM ((x3v6.PROD.PORDERQ PORDERQ
INNER JOIN x3v6.PROD.PORDERP PORDERP ON
(PORDERQ.POHNUM_0=PORDERP.POHNUM_0)
AND (PORDERQ.POPLIN_0=PORDERP.POPLIN_0))
LEFT OUTER JOIN x3v6.PROD.PRECEIPTD PRECEIPTD ON
((PORDERQ.POPLIN_0=PRECEIPTD.POPLIN_0)
AND (PORDERQ.POQSEQ_0=PRECEIPTD.POQSEQ_0))
AND (PORDERQ.POHNUM_0=PRECEIPTD.POHNUM_0))
LEFT OUTER JOIN x3v6.PROD.PINVOICED PINVOICED ON
(PRECEIPTD.PTHNUM_0=PINVOICED.PTHNUM_0)
AND (PRECEIPTD.PTDLIN_0=PINVOICED.PTDLIN_0)
WHERE
(PRECEIPTD.CREDAT_0>={ts '2018-01-01 00:00:00'} AND PRECEIPTD.CREDAT_0<{ts '2019-01-01 00:00:00'})
AND PINVOICED.NUM_0 IS NULL
AND PORDERQ.BPSNUM_0=PORDERQ.BPSNUM_0
ORDER BY
PORDERQ.BPSNUM_0
, PRECEIPTD.CREDAT_0
Did some more searching around and found a similar post at https://stackoverflow.com/a/43203088/8672275. The AND ISNULL({PINVOICED.NUM_0}) section of my record selection was throwing Crystal Reports off. I changed it to AND ToText({PINVOICED.NUM_0}) = "" and the report is returning exactly what I expect to see.
I'm writing a query with a report file, an employee file and a report distribution file. I would like a list of employee names, each with every report name and a 0 if they don't get the report and a 1 if they do get the report.
select distinct ut.user_name
, ut.emailaddress
, r.name
, iif(ISNULL(rd.employeeid,0)=0, 0,1) AS currentreport
from US_usertable ut
cross join DL_reports r
left outer join DL_Reptdistrib rd
on ut.employeeID = rd.employeeid
For each user, I get a complete set of reports - so the cross join works - but either they get a 1 for all the reports or a 0 for all their reports. I don't understand why this query isn't working. Kindly help if you can. Thanks in advance!!!
I think you are missing a join condition on the report:
select ut.user_name, ut.emailaddress, r.name,
(case when rd.employeeid is null then 0 else 1 end) as currentreport
from US_usertable ut cross join
DL_reports r left outer join
DL_Reptdistrib rd
on ut.employeeID = rd.employeeid and r.?? = rd.??;
The ?? is for the field used to identify the report. I might guess that it is reportID.
Note: I switched the syntax to standard SQL. IIF() is in SQL Server for compatibility with MS Access (why Microsoft didn't put the ANSI standard case in MS Access is beyond me). I also replaced ISNULL() with the ANSI standard IS NULL.
Need the results from this query:
SELECT CAST(a.InvoiceDate AS DATE) InvoiceDate,
COUNT(*) RecordCount
FROM SalesOrder a
JOIN IMSSalesExtractHistory b
ON a.SlsOrdNbr = b.SlsOrdNbr
AND a.OrdLnNbr = b.OrdLnNbr
AND a.OrdLnSeqNbr = b.OrdLnSeqNbr
WHERE b.SAFInsertDate > GETDATE()-2
GROUP BY CAST(a.InvoiceDate AS DATE)
ORDER BY CAST(a.InvoiceDate AS DATE)
to be replicated into my Crystal Report.
I have started this formula in the Formula Workshop window within Crystal Reports 2013 but it keeps giving me this error:
(
SELECT DISTINCT CAST("SalesOrder"."InvoiceDate" AS DATE) InvoiceDate
FROM "SalesOrder"
JOIN "IMSSalesExtractHistory"
ON "SalesOrder"."SlsOrdNbr" = "IMSSalesExtractHistory"."SlsOrdNbr"
AND "SalesOrder"."OrdLnNbr" = "IMSSalesExtractHistory"."OrdLnNbr"
AND "SalesOrder"."OrdLnSeqNbr" = "IMSSalesExtractHistory"."OrdLnSeqNbr"
WHERE "IMSSalesExtractHistory"."FileDate" > {fn CURDATE()}
)
Error:
SAP Crystal Reports
Error in compiling SQL Expression :
Failed to retrieve data from the database.
Details: ADO Error Code: 0x80040e07
Source: Microsoft OLE DB Provider for SQL Server
Description: Conversion failed when converting the varchar value '2016-05-25' to data type int.
SQL State: 22018
Native Error: 245 [Database Vendor Code: 245 ].
OK
I removed the COUNT(*) from the CS query because I was going to use the same code when I got it working and find the count for a new column within my report.
Looking for someone to help convert the initial SQL query so that it has no errors in Crystal Reports.
EDIT:
For reference, this is the result set I need in Crystal Report that I generated from the SQL script above.
InvoiceDate RecordCount
2016-05-13 16074
2016-05-14 2
2016-05-15 4
2016-05-16 27495
2016-05-17 20023
2016-05-18 18923
2016-05-19 18944
Start crystal reports and make database connection.
instead of selecting tables select add command and paste this query
SELECT CAST(a.InvoiceDate AS DATE) InvoiceDate,
COUNT(*) RecordCount
FROM SalesOrder a
JOIN IMSSalesExtractHistory b
ON a.SlsOrdNbr = b.SlsOrdNbr
AND a.OrdLnNbr = b.OrdLnNbr
AND a.OrdLnSeqNbr = b.OrdLnSeqNbr
WHERE b.SAFInsertDate > GETDATE()-2
GROUP BY CAST(a.InvoiceDate AS DATE)
ORDER BY CAST(a.InvoiceDate AS DATE)
Now got to design and place the required columns in detail sectio.
I want to generate a report in Crystal Reports and I am very new to it. I am unaware of how to manipulate the Crystal Report. I made a SQL query which gives me all the required output from the Oracle(10g) database. But I need to convert it to be used inside Crystal Reports.
This is the SQL query which gives me required output:
SELECT
ins.ins_name,ins.ins,crdi.ct, crdi.bn,
sum(DECODE(cardh.crd_st, 'PO', 1, 0)) POCount,
sum(DECODE(cardh.crd_st, 'CN', 1, 0)) CNCount
FROM
crdh, crdi, ins
WHERE
crdh.crd_st IN ('PO','CN')
and crdi.bn in (select unique bn from crdh)
and crdh.bn = crdi.bn
and crdi.ins = ins.ins
GROUP BY
ins.ins, crdi.bn, ins.ins_name, crdi.ct
ORDER BY
ins.ins, crdi.bn;
When I implemented the above query I got the following error:
a) When I put the above query in Database|Show SQL Query..., the following part of the query is removed:
sum(DECODE(cardh.crd_st, 'PO', 1, 0)) POCount,
sum(DECODE(cardh.crd_st, 'CN', 1, 0)) CNCount
GROUP BY ins.ins, crdi.bn, ins.ins_name,crdi.ct
b) When I added a group for ins.ins, Crystal reports adds a lots of spaces in the report.
c) How can I print the value of POCount and CNCount in the crystal report?
I am also adding the screenshot of the output for better understanding.
There can be multiple BN for one INS and for one BN there are multiple CNs and POs. Like INS 3 has two BN ('123456','789012') and there are 3 POs and 0 CN in BN '123456', but there is only one CN in BN '789012'. I hope this is helpful to replier.
Please help me to get a report same as the output of the above mentioned query. Thanks in advance.
I got the following result :
Dont implement sum in query instead implement it in crystal. so change the query like this.
SELECT ins.ins_name,ins.ins,crdi.ct, crdi.bn,
cardh.crd_st,cardh.crd_st
FROM crdh, crdi, ins
Add above query in crystal report command when you make a connection to the report.
Now for your report to display in the required format.
Create a formula #PO
if cardh.crd_st= 'PO'
then 1
else 0
Create formula for CN
if cardh.crd_st='CN'
then 1
else 0
Place above formula in detail
Create a group by ins.ins_name
Place all your columns in group footer and at the same time take sum for the formulas #po and CN
As you are using where condition in query to get that condition in CR. Implement your where clause in Select Expert ---> Record Selection Formula, If you are not comfortable then try implement the where clause in query itself.
Let me know how it goes.