SQL Query to Crystal Report - sql

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.

Related

SQL query generated by Crystal Reports works in SSMS but pulls no records in Crystal

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.

IBM Informix-SQL syntax error, basic query from Microsoft BIDS to Cisco UCCX database

I'm running the below query against an IBM Informix database and getting an ERROR 42000: A syntax error has occurred. The FROM and WHERE clauses run fine in other queries, so I'm looking at the SELECT and GROUP BY portions. Any ideas what's wrong with the syntax?
SELECT COUNT(DISTINCT "informix".agentconnectiondetail.sessionid) AS calls_abandoned,
DAY("informix".agentconnectiondetail.startdatetime) AS Expr2
FROM "informix".agentconnectiondetail, "informix".contactqueuedetail, "informix".contactservicequeue
WHERE "informix".agentconnectiondetail.sessionid = "informix".contactqueuedetail.sessionid AND
"informix".contactqueuedetail.targetid = "informix".contactservicequeue.recordid AND "informix".contactqueuedetail.disposition = 1 AND
"informix".agentconnectiondetail.startdatetime BETWEEN '2016-10-1 00:00:00' AND CURRENT
GROUP BY DAY("informix".agentconnectiondetail.startdatetime)
The goal btw is to find the total number of unique calls (calls_abandoned) that occur on each day of the month (1-31).
Replace the
GROUP BY DAY("informix".agentconnectiondetail.startdatetime)
by
GROUP BY 2

How to Achieve this SQL statement with Crystal Reports?

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

Implement a SQL query in Crystal reports

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.

Genrate a report in crystal report where count of a field put into a column

I have to generate a report in Crystal report and VB report where database is Oracle, like the attached screen shot:
I have used the following query and got the output as below screen shot:
SELECT ins.ins, ins.ins_name, crdi.ct, crdi.bin,
(select count(*) from crdh where crd_st='CN') as CNcount, (select count(*)
from crdh where crd_st='PO') as POcount
FROM crdh, crdi, ins
where crdh.bn=crdi.bn and crdi.ins=ins.ins and crdh.crd_st IN ('PO','CN')
GROUP BY ins.ins, crdi.bn, ins.ins_name,crdi
ORDER BY ins.ins, crdi.bn;
I have the following issues:
How can I correct the above query so that it can return the count of crd_st for a perticular bn where crd_st is 'CN' or 'PO' separately. For example this query should tell me what is the count of records where crd_st is PO for bn 123456.
I am very new in the Crystal report. Please help to make this report file and
Also tell me how can I implement Sr No for numbering of records in this report file(.rpt).
I ran the following query and got the output as below screen shot
SELECT ins.ins_name,ins.ins,
crdi.crd_st, crdi.bin, crdh.crd_st,
COUNT(crdh.crd_st) as count
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.crd_st,crdh.crd_st
ORDER BY ins.ins, crdi.bn;
I want to show the count column as a row in the report for respective bn.
I have Oracle 10g database and followed the following link
link for pivote
And make the following query and got the 'ORA-00933: SQL command not properly ended' error.
SELECT bn, CNCount, POCount
FROM (
SELECT bn,
crd_st,
ROW_NUMBER() OVER (PARTITION BY bn ORDER BY crd_st) AS cardRank
FROM cardholder
)
pivot( count(crd_st) FOR cardRank IN ('CN' as CNCount, 'PO' as POCount));
Thanks for your help in advance.
Since you got some output, now to manuplate that output as required use cross tab in crystal report.
Use bn in column.
Create a formula #Count and write just "Count" in that formula and use the formula in rows.
Use database field Count in summarized fields.
Let me know how it goes
Edit--------------------------------------------------------------------------------------
1. Drag the crosstab on to report.
2. `Right click` on crosstab and go to `Crosstab expert`
3. There you will find 3 options, `Rows`, `Columns` and `Summarized fileds`
4. place the field what you want in row, place the filed what you want in column and same way place the column for summarized fields
I got the required output by running the following query:
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;