Querying the number of specific items in a database - sql

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;

Related

Select count with in query through Crystal Reports

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

SQL subqueries/ subqueries within subqueries

I'm trying to learn SQL (through a company guide).
I have a list of invoices in a table and a tax code associated with one.
All of the invoices have one tax code, except number 1000001 which has two (and therefore two records in dbo.InvoiceTaxBreakDown.
The exercise wants me to create the variables TaxCode1 and TaxCode2. All except one record should have 'NULL' in TaxCode2.
This is my code:
SELECT
InvoiceNo,
TaxCode1 =
(
SELECT
TOP 1 T.TaxCode
FROM
dbo.InvoiceTaxBreakDown as T
WHERE
I.InvoiceGUID = T.InvoiceGUID
ORDER BY
T.TaxCode DESC
)
,
TaxCode2 =
(
SELECT
TOP 1 T.TaxCode
FROM
dbo.InvoiceTaxBreakDown as T
WHERE
I.InvoiceGUID = T.InvoiceGUID
ORDER BY
T.TaxCode ASC
)
FROM
dbo.InvoiceTaxBreakDown as I
I'm not sure if I even need to reference two data sources..
Only the first record is correct! Please help.
[Output][1]

How to to get two columns of data unrelated to each other in one sql query statement?

I need to get a state level count on number of services. For the purposes of this I only have two services. The first column is the states, the second column is the first services and the third column is the second service. What I am struggling with is to have the second and third column show up on the results in one query. Here is my code:
SELECT Distinct allstates.Name, count (data.StateName) as CareCase_Management_Services, count(data.StateName) Caregiver_Support_Services
From
(select distinct Name from USstate) allstates
Left Join
Client2017 data
on
allstates.Name = data.StateName and
data.FiscalYear = 2017 and
data.SrvstartCareCaseMgmtCode NOT IN('999','', '998') and
data.SrvstartCaregiverSuppCode NOT IN('999','', '998')
GROUP BY allstates.Name
ORDER BY allstates.Name ASC
I understand that you are looking to compute, for each state, the count of services that match certain criteria. There are two types of services, stored in two different columns.
If so, your query could be simplified using conditional aggregation :
SELECT
allstates.Name,
SUM(CASE WHEN c.SrvstartCareCaseMgmtCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) CareCase_Management_Services,
SUM(CASE WHEN c.SrvstartCaregiverSuppCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) Caregiver_Support_Services
FROM
(SELECT DISTINCT Name FROM USstate) s
LEFT JOIN Client2017 c ON s.Name = c.StateName AND c.FiscalYear = 2017
GROUP BY allstates.Name
With this technique, each service is counted according to its own logic ; when conditions are met, the record is counted in (1 is added to the SUM()), else it is ignored (+ 0).
NB : do you really have duplicated state names in USstate ? if no, you can replace subquery (SELECT DISTINCT Name FROM USstate) s with just USstate

Identify Duplicate Invoice number with prefix or suffixe

I'm using access 2013 and trying to identify duplicate payments made to vendors. I use the SQL query below to identify different type of duplicates but it is not giving desired results as sometimes two criteria are different like invoice number and invoice date.
SELECT
Base.ID AS SerialNumber,
Base.CoCd AS CoCode,
Base.DocumentNo AS DocID,
Base.ClrngdocNo AS ClearingDoc,
Base.DocumentType AS DocType,
Base.Account AS VendorName,
Base.Reference AS InvoiceNumber,
Base.DocumentDate AS InvoiceDate,
Base.GrossInvoiceAmount AS InvAmount
FROM RawData2017TillDate AS Base
INNER JOIN RawData2017TillDate AS duplicate
ON (Base.ID <> duplicate.ID)
AND (Base.Account = duplicate.Account)
AND (Base.Reference <> duplicate.Reference)
AND (Base.DocumentDate = duplicate.DocumentDate)
AND (Base.GrossInvoiceAmount = duplicate.GrossInvoiceAmount)
ORDER BY Base.GrossInvoiceAmount DESC , Base.reference DESC;
I just want single query to identify duplicate with one or more characters added at the begining or at the end of invoice number like examples below
2713565
2713565R,
01456
1456,
I-0001118588
1118588
Also, if I could get a better query to identify duplicates based on other criteria will be appreciated. I am looking for a single query for all criteria.
Thanks in advance!
Please try like below:
I grouped invoice numbers based on their first 2 letters.
SELECT
Mid(Base.reference, 1, 2) , count(1)
FROM RawData2017TillDate AS Base
INNER JOIN RawData2017TillDate AS duplicate
ON (Base.ID <> duplicate.ID)
AND (Base.Account = duplicate.Account)
AND (Base.Reference <> duplicate.Reference)
AND (Base.DocumentDate = duplicate.DocumentDate)
AND (Base.GrossInvoiceAmount = duplicate.GrossInvoiceAmount)
group by Mid(Base.reference, 1, 2) ;

Access SQL query - Percentage of Total calculation

In Access SQL, I am attempting what should seem like a simple task in attaining a percentage of total. There are 3 item stores (Sears, kmart & Mktpl) of which in any given week, I wish to calculate their respective percent of total based on balance of sales (all can be obtained using one table - tbl_BUChannelReporting).
For example week 5 dummy numbers - Sears 7000, kmart 2500, mktpl 2000
the following ratios would be returned: sears 61%, kmart 22%, mktpl 17%
I was originally trying to create a sub query and wasn't getting anywhere so I am essentially trying to sum sales on one of the item stores in week 5 divided by the sum of all 3 item store sales in week 5. The following is my query, which is giving me "cannot have aggregate function in expression" error:
SELECT FY, FW, Rept_Chnl, BU_NM, Order_Store, Item_Store, CDBL(
SUM(IIF([item_store]="sears", revenue, IIF([item_store]="kmart", revenue, IIF([item_store]="mktpl", revenue,0)))) /
(SUM(IIF([item_store]="sears",revenue,0)+SUM(IIF([item_store]="kmart",revenue,0)+SUM(IIF([item_store]="mktpl",revenue,0))))))
AS Ratios
FROM tbl_BUChannelReporting
WHERE FY = "2017"
AND FW = 5
GROUP BY FY, FW, Rept_Chnl, BU_NM, Order_Store, item_store
Thanks all in advance for taking the time. This is my 1st post here and I don't consider myself anything but a newbie anxious to learn from the best and see how this turns out.
Take care!
-D
Consider using two derived tables or saved aggregate queries: one that groups on Item_Store and the other that does not include Item_Store in order to sum the total stores' revenue. All other groupings (FY, FW, Rept_Chnl, BU_NM, Order_Store) remain in both and used to join the two. Then in outer query, calculate percentage ratio.
SELECT i.*, CDbl(i.Store_Revenue / a.Store_Revenue) As Ratios
FROM
(SELECT t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store, t.Item_Store,
SUM(t.Revenue) As Store_Revenue
FROM tbl_BUChannelReporting t
WHERE t.FY = '2017' AND t.FW = 5
GROUP BY t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store, t.Item_Store) As i
INNER JOIN
(SELECT t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store
SUM(t.Revenue) As Store_Revenue
FROM tbl_BUChannelReporting t
WHERE t.FY = '2017' AND t.FW = 5
GROUP BY t.FY, t.FW, t.Rept_Chnl, t.BU_NM, t.Order_Store) As a
ON i.FY = a.FY AND i.FW = a.FW AND i.Rept_Chnl = a.Rept_Chnl
AND i.BU_NM = a.BU_NM AND i.Order_Store = a.Order_Store
Or save each above SELECT statement as its own query and reference both below:
SELECT i.*, (i.Store_Revenue / a.Store_Revenue) As Ratios
FROM
Indiv_Item_StoreAggQ As i
INNER JOIN
All_Item_StoreAggQ As a
ON i.FY = a.FY AND i.FW = a.FW AND i.Rept_Chnl = a.Rept_Chnl
AND i.BU_NM = a.BU_NM AND i.Order_Store = a.Order_Store