Select count with in query through Crystal Reports - sql

I'm trying to create a report where I have to get the top 10 facilities that have a specific action code. I'm using 3 different tables (customer.id) customers pertaining to those facilities, the facility information ( facility.id), and action code( action.id) I'm fairly new to writing queries so here is what
SELECT
customer.id_NO,
customer.id_CLIENT,
facility.id_NAME_1 ,
action.id_ACTION_CODE
FROM
CDS.action.id, CDS.customer.id, CDS.facility.id
WHERE
facility.id _NO = customer.id _CLIENT
AND action.id_CUSTOMER_NO = customer.id _NO
I want to count every time the action code 'CDQ' occurs per facility
SELECT COUNT ( action.id_ACTION_CODE)
FROM CDS.action.id
WHERE action.id_ACTION_CODE = 'CDQ'
Is it possible to get all in a single query? I'm using Crystal Reports

You can create a formula and use sum on this formula:
If {action.id_ACTION_CODE} = "CDQ" Then 1 Else 0

Related

Crystal Reports Summary field with sum of amounts from SQL command

I have a sub-report that returns the following....
SELECT
SUM (SorDetail.MPrice) OVER (PARTITION BY SorDetail.SalesOrder, [CusSorDetailMerch+].InvoiceGrouping) AS GroupTotal,
SorDetail.SalesOrder,
SorDetail.SalesOrderLine,
SorDetail.MStockCode,
SorDetail.MStockDes,
[CusSorDetailMerch+].InvoiceGrouping,
SorDetail.MOrderQty,
SorDetail.MPrice,
[CusSorDetailMerch+].SerialNumber
FROM
SorDetail
INNER JOIN
[CusSorDetailMerch+] ON SorDetail.SalesOrder = [CusSorDetailMerch+].SalesOrder
AND SorDetail.SalesOrderLine = [CusSorDetailMerch+].SalesOrderInitLine
WHERE
(dbo.SorDetail.SalesOrder = '000000000' + '{?SalesOrder}')
AND (dbo.SorDetail.SalesOrderInitLine = '{?Line}')
ORDER BY
[CusSorDetailMerch+].InvoiceGrouping
It returns this which is what I want:
Data Returned
What I just cant seem to do id to add a column that sums the different groups and separates the groups like this:
enter image description here
I have tried everything but cannot get the sums individually

Multiple entries in crystal reportviewer after adding a SQL expression field

I am using Visual Studio 2017 and I installed the latest crystal reportviewer (22)
What I want is to click a button and create a report from the customer that is selected in the datagridview and the addresses that are shown in the second datagridview.
I managed to do all that but the problem is that a few fields contain numbers which need to be converted to text. An SQL query I would use to do this would be like:
SELECT c.customer_nr, c.status, s.rename FROM CUSTOMERS c INNER JOIN SETUP s on s.id = c.status WHERE s.afk = 'STA'
In my SETUP database I have the columns ID,AFK and RENAME so if the status would be 1 it would convert to text: "ACTIVE", if status = 2 it would convert to "INACTIVE" for example.
I could do something with a formula field like this:
IF ({c.status} = 1) THEN "ACTIVE" ELSE
IF ({c.status}) = 2 THEN "INACTIVE"
but that is not good because i could add another status or change the name in the database etc.
So then I tried with an SQL expression field and I put something like this:
(
SELECT "SETUP"."RENAME" FROM SETUP
WHERE "SETUP"."AFK" = 'STA' AND "SETUP"."ID" = "CUSTOMERS"."STATUS"
)
There must be something wrong because I get the correct conversion but there is only one address in the database but I get 7 pages all with the same address. There should only be one address like I get when I remove the SQL expression field. Where does it go wrong?
* EDIT *
I found the problem. When I create a new database that contains only unique id's then it works. In my original database I have multiple times the id's 1,2,3,4,5 but with different abbreviations in column AFK. Somehow the query looks for the id value and every time it finds this id no matter the AFK value it generates an entry for the address value.
Maybe in the future I will find out how this exactly works for now I have a workaround.
Create a new table for example CrRepSta and add the following entries:
ID,AFK,RENAME
1,STA,Active
2,STA,Inactive
etc
The new query:
(
SELECT "CrRepSta"."RENAME" FROM CrRepSta
WHERE "CrRepSta"."AFK" = 'STA' AND "CrRepSta"."ID" = "CUSTOMERS"."STATUS"
)
And by the way the statement "CrRepSta"."AFK" = 'STA' is not really needed.

How to make an SSRS parameter = a field from another dataset

I am writing an SSRS report to create an invoice.
In my report, I have a dataset whcih runs the following query:
select Customer, Name, BillAddress, BillCity, BillState, BillZip from ARCM where ARCM.Customer = #BillCustomer and ARCM.CustGroup = 1
As you can see, I have a parameter called '#BillCustomer'.
I have another dataset which runs this query:
select Co, Customer, Invoice, TransDate, DueDate, PayTerms, CustRef from ARBH
Where Invoice = #Invoice
How can I change my report so that #BillCustomer does not need to be manually entered when I run the report however, it gets its value from the Customer field in the second dataset?
Could you change your Customer dataset to use the parameter #Invoice directly and change the query to
select Customer, Name, BillAddress, BillCity, BillState, BillZip
from ARCM where ARCM.Customer IN (
select Customer
from ARBH
Where Invoice = #Invoice) and ARCM.CustGroup = 1
Or using JOIN in above query.

Access Update not working

I have an Access Database which links into Excel for a college database project. The goal is to fix incorrect/incomplete data before importation through the use of Update/SQL queries. There is one item in particular which is a field containing the order status. If an order is complete = Complete, if an order is missing a few parts = Backorder and nothing at all = Open. The issue I have is there is multiple parts on one PO ID# which makes it difficult to determine if an order is on backorder as 2/5 parts may be complete while 3/5 may be on backorder. Any ideas on how I can force access to automatically set a default order status or is this a long sql query?
Thanks
With SQL you can return a query to find the order status, using a CASE statement. I'm not sure of the table/column names in the parent table you have but you can probably tweak it from here (I'm just assuming identity columns):
SELECT P.Id, P.Name, "Status" =
CASE
WHEN R.QuantityReceived < I.QuantityOrdered THEN 'Backorder'
WHEN R.QuantityReceived = I.QuantityOrdered THEN 'Complete'
WHEN R.QuantityReceived > I.QuantityOrdered THEN 'Open'
END
FROM PurchaseOrders P INNER JOIN POItem I on P.PurchaseOrderId = I.PurchaseOrderId
INNER JOIN POReceipt R on I.ItemId = R.ItemId
So first you need to join your tables to get the related records (this is also assuming that there is only one of each), then use a CASE statement to return the value you are looking for based on a comparison between fields.

Querying the number of specific items in a database

I was just wondering how could I count the number of a specific equipment..
SELECT
EQUIPMENTS.DESCRIPTION AS [EQUIPMENT TYPE],
Count(EQUIPMENTS.EQNAME) AS QUANTITY,
(SELECT Count(EQUIPMENTS.CONDITION) FROM EQUIPMENTS WHERE EQUIPMENTS.CONDITION = 'Functional') AS WORKING,
(SELECT Count(EQUIPMENTS.CONDITION) FROM EQUIPMENTS WHERE EQUIPMENTS.CONDITION = 'Non-Functional') AS [NON-WORKING]
FROM EQUIPMENTS
GROUP BY EQUIPMENTS.DESCRIPTION;
this query returns the following :
EQUIPMENT NAME : PROJECTOR
QUANTITY : 3
WORKING : 2
NON-WORKING :1
Now if I add another equipment which has a different type, for example CALCULATOR, it would have the same count of WORKING AND NON-WORKING which only is for the PROJECTOR. How do I Make it such that it also counts the quantity of the Calculator and the number of working and non-working itself? I mean whenever I add another equipment which has a specific description, the query would also count it independently?
I'm using VB.NET and this query is made in MS ACCESS 2007.
Use IIf() expressions to return 1 when the condition is satisfied, and 0 when not. Then Sum those values.
SELECT
e.DESCRIPTION AS [EQUIPMENT TYPE],
Count(e.EQNAME) AS QUANTITY,
Sum(IIf(e.CONDITION = 'Functional', 1, 0)) AS WORKING,
Sum(IIf(e.CONDITION = 'Non-Functional', 1, 0)) AS [NON-WORKING]
FROM EQUIPMENTS AS e
GROUP BY e.DESCRIPTION;