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.
Related
New to SQL and I am trying to run a query that pulls all our item codes, lot number, and qty on hand.
Each lot number has multiple entries due to adjustments. I need a way of running my query and having it add or subtract to get the actual qty on hand for each lot and only show me lots that are in the negatives. I have tried playing with SSRS but I cant get it right. I'm using SQL 2008R2.
SELECT
IMLAYER.ITEM_CODE
,IMMSTR.ITEM_DESC
,IMLAYER.LOT_NO
,IMLAYER.QTY_ON_HAND
FROM
IMLAYER
INNER JOIN
IMMSTR
ON
IMLAYER.ITEM_CODE = IMMSTR.ITEM_CODE
WHERE
(IMLAYER.QTY_ON_HAND < 0);
I believe I understand the requirements correctly, but if not please comment and I can update the query:
SELECT
M.ITEM_CODE
,M.ITEM_DESC
,L.LOT_NO
,'SUM_OF_QTY_ON_HAND' = SUM(L.QTY_ON_HAND)
FROM
IMLAYER L
INNER JOIN
IMMSTR M
ON L.ITEM_CODE = M.ITEM_CODE
GROUP BY
M.ITEM_CODE
,M.ITEM_DESC
,L.LOT_NO
HAVING
SUM(L.QTY_ON_HAND) < 0
HAVING is the trick you are looking for to be able to use an aggregate function for filtering.
I often use the function (now legacy) in Excel to get data from SQL Server where I paste an actual SQL statement into the Excel sheet and it works fine. I have been researching how to do this with queries I have that have date parameters that need to be changed each time a report is ran and at first it seemed like using Microsoft Query in Excel would be the best option. This would use the '?' instead of the dates themselves and allow for adding parameters. Whenever I try to do this with the below query I get the error "Parameters are not allowed in queries that can't be displayed graphically." I honestly have no idea what that means but would value any input. My Query is below. Thanks
SELECT E.TEAM_MEMBER_NAME AS 'PURCHASER',
M.DEPARTMENT,
M.BUSINESS_SEGMENT_CODE,
KB.BUSINESS_SEGMENT_DESC,
KG.GENDER_DESC,
MR.PLANT_CODE [PLANT],
MR.STOCK_CATEGORY,
M.MATERIAL,
M.[DESCRIPTION],
M.COLOR_1,
M.COLOR_2,
MR.SIZE_LITERAL,
MR.QUANTITY,
M.STANDARD_COST,
M.DEALER_PRICE,
M.CURRENT_SEASON,
MR.STOCK_NUMBER AS 'AFS PO #',
H.PO_CREATED_BY,
H.PO_TYPE,
MR.MRP_INDICATOR,
MR.STOCK_TYPE,
H.PO_ISSUE_DATE
FROM PDX_SAP_USER..VW_MRP_ALLOCATION MR
JOIN PDX_SAP_USER..VW_MM_MATERIAL M ON MR.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..VW_KD_BUSINESS_SEGMENT KB ON M.BUSINESS_SEGMENT_CODE = KB.BUSINESS_SEGMENT_CODE
JOIN PDX_SAP_USER..VW_KD_GENDER KG ON M.GENDER_CODE = KG.GENDER_CODE
JOIN PDX_SAP_USER..VW_PO_HEADER H ON MR.STOCK_NUMBER = H.PO_NUMBER
JOIN ADI_USER_MAINTAINED..SCM_PO_EMPLOYEE_NAME E ON MR.STOCK_NUMBER = E.PO_NUMBER
WHERE M.BUSINESS_SEGMENT_CODE NOT IN ('420','421','422','424')
AND MR.STOCK_CATEGORY NOT LIKE 'A60383%'
AND MR.STOCK_CATEGORY NOT IN ('A60382001','A60380070')
AND M.MATERIAL NOT IN ('AY1480','CD4683')
AND H.PO_TYPE NOT IN ('02','06','10','UB','DB')
AND MR.MRP_INDICATOR IN ('A','N')
AND MR.STOCK_TYPE = 'B'
AND MR.QUANTITY >= 50
AND H.PO_ISSUE_DATE BETWEEN '09/26/2018' AND '10/10/2018'
ORDER BY MR.QUANTITY DESC
I got it to work...a bit of a work around. I had my DBA create a view on our server and selected all from that view. I then used MS Query to bring in the data and replaced the dates with ?. From there in the data source within Excel you can assign those question marks to cells in which you enter your dates. Works like a charm. And not taking credit - all credit goes to:
https://www.youtube.com/watch?v=xPalEw4xw1w
Short answer is that I've researched it at the time as well, trying my best to pass parameters (let's say, 'Sheet1!A1' cell) and couldn't. There is no real way to do it UNLESS you're using the Power Query as well as using an SQL stored procedure.
Do you think you can ask your DBA (or whomever is responsible for the database) to create a stored procedure for you in which you'd pass the date parameters? That's basically your only way to create a parameterised query.
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
I'm trying to avoid using straight up SQL in my Rails app, but need to do a quite large version of this:
SELECT ds.product_id,
( SELECT SUM(units) FROM daily_sales WHERE (date BETWEEN '2015-01-01' AND '2015-01-08') AND service_type = 1 ) as wk1,
( SELECT SUM(units) FROM daily_sales WHERE (date BETWEEN '2015-01-09' AND '2015-01-16') AND service_type = 1 ) as wk2
FROM daily_sales as ds group by ds.product_id
I'm sure it can be done, but i'm struggling to write this as an active record statement. Can anyone help?
If you must do this in a single query, you'll need to write some SQL for the CASE statements. The following is what you need:
ranges = [ # ordered array of all your date-ranges
Date.new(2015, 1, 1)..Date.new(2015, 1, 8),
Date.new(2015, 1, 9)..Date.new(2015, 1, 16)
]
overall_range = (ranges.first.min)..(ranges.last.max)
grouping_sub_str = \
ranges.map.with_index do |range, i|
"WHEN (date BETWEEN '#{range.min}' AND '#{range.max}') THEN 'week#{i}'"
end.join(' ')
grouping_condition = "CASE #{grouping_sub_str} END"
grouping_columns = ['product_id', grouping_condition]
DailySale.where(date: overall_range).group(grouping_columns).sum(:units)
That will produce a hash with array keys and numeric values. A key will be of the form [product_id, 'week1'] and the value will be the corresponding sum of units for that week.
Simplify your SQL to the following and try converting it..
SELECT ds.product_id,
, SUM(CASE WHEN date BETWEEN '2015-01-01' AND '2015-01-08' AND service_type = 1
THEN units
END) WK1
, SUM(CASE WHEN date BETWEEN '2015-01-09' AND '2015-01-16' AND service_type = 1
THEN units
END) WK2
FROM daily_sales as ds
group by ds.product_id
Every rail developer sooner or later hits his/her head against the walls of Active Record query interface just to find the solution in Arel.
Arel gives you the flexibility that you need in creating your query without using loops, etc. I am not going to give runnable code rather some hints how to do it yourself:
We are going to use arel_tables to create our query. For a model called for example Product, getting the Arel table is as easy as products = Product.arel_table
Getting sum of a column is like daily_sales.project(daily_sales[:units].count).where(daily_sales[:date].gt(BEGIN_DATE).where(daily_sales[:date].lt(END_DATE). You can chain as many wheres as you want and it will be translated into SQL ANDs.
Since we need to have multiple sums in our end result you need to make use of Common Table Expressions(CTE). Take a look at docs and this answer for more info on this.
You can use those CTEs from step 3 in combination with group and you are done!
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;