SUM is not giving correct amount when WHERE condition is NULL
SELECT
DE."Donor First Name",
DE."Donor Last Name",
EX."SUM(Amount)",
No."MAX(Note Written)",
SUM(DE."Amount")
FROM "Latest Thank You Note" AS No
JOIN "Over 250 - Donors List" AS EX ON No."Full Name" = EX."Donor Name"
JOIN "Donor Table" AS DE ON No."Full name" = DE."Full Name"
WHERE DE."Date" >= No."MAX(Note Written)"
OR No."Max(Note Written)" Is NULL
GROUP BY 1,
2,
3,
4
Here is the code I am writing. It pulls donors names, their total amount donated, then the last time they were written a thank you note, and what they've donated since. However, sometimes when I pull this table, if there was never a thank you note written "MAX(Note Written") is Null, then it will give me a random sum. Is there any way to fix it?
I Tried removing the null but it doesn't pull the correct results.
I am new to SQL and programming in general and am writing to write a data pull which would pull the monetary value for the category as well as department for each observation that we have. Yet I encounter this error. Any help is really appreciated.
SELECT CAST(ROUND(sales.store_nbr,0) AS INT) AS "Store NUMBER", dept.acct_dept_nbr AS "Department NUMBER", dept.dept_desc AS "Department Description", SUM(sales.wkly_sales) AS "Weekly sales",SUM(sales.yrly_sales) AS "Yearly Sales"
--pull sales and quantity by store
FROM CA_WM_VM.SKU_DLY_POS sales, CA_WM_VM.DEPT_DESC dept, ca_wm_vm.item item, CA_WM_VM_SKU_YRLY_POS sales
--for a store, for a period of time
WHERE sales.item_nbr = item.item_nbr
AND item.dept_nbr = dept.acct_dept_nbr
AND sales.wm_yr_wk BETWEEN 11613 AND 11713
--group by store number, & dept number
GROUP BY sales.store_nbr, dept.acct_dept_nbr, dept.dept_desc
The first and last table in the from clause have the same alias name "sales".
On our postgreSql Database we have two table one is appointment and the other one location. We are currently counting the number of Appointment per location.
We use the following query. Unfortunately it is giving one row per every appointment whereas we would like to count the total. Is there a way to do this?
Thank you!
select "domain".locations."name" AS "Location Name",
COUNT("domain".appointments."id") AS "Number Of Appointment",
"domain".locations.mongo AS "Location ID Mongo",
"domain".locations."id" AS "Location ID",
"domain".countries."name" AS "Country Name",
to_char("domain".appointments.created_at,'FMDD.MM.YYYY') AS "Appointment Created Date"
from "domain".appointments
INNER JOIN "domain".locations
ON "domain".locations."id" = "domain".appointments.location_id
INNER JOIN "domain".countries
ON "domain".locations.country_id = "domain".countries."id"
WHERE "domain".appointments."source" = 'extranet'
AND date_trunc('day', "domain".appointments.created_at) = date_trunc('day', current_date - 1)
AND "domain".locations."id" <> '2016'
GROUP BY "domain".locations."id",
"domain".locations."name",
"domain".countries."name",
"domain".appointments.created_at,
"domain".appointments."id"
I've been working on some sql code in FileMaker and I have a basically a table called "Incidents" and a field called "Incident Type" under the said table. I use the code below to count the number of times the option "Injury" is selected from the field "Incident Type". This works 100%. However, I have been trying to sort the records in this database by the year it happened and to no surprise the code is counting all times the option "Injury" is selected in its respective field. I want to only count the times when the field "Incident Type" is equal to "Injury" and the other field "Incident Year" is equal to '2014'. Can someone revise the code below to show that?
ExecuteSQL(
"SELECT Count(\"Incident Type\")
FROM \"Incidents\"
WHERE \"Incident Type\" = ?"
;"";"";"Injury")
I'm not 100% sure this will work for you, but it should be close enough for you to tweak to your needs.
ExecuteSQL(
"
SELECT Count(\"Incident Type\")
FROM \"Incidents\"
WHERE (\"Incident Type\" = ?)
AND (\"Incident Year\" = ?)
"; ""; ""; "Injury"; "2014"
)
In case you are trying to display total injuries for a number of years, sorted by year
ExecuteSQL(
"
SELECT \"Incident Year\", Count(\"Incident Type\")
FROM \"Incidents\"
WHERE (\"Incident Type\" = ?)
GROUP BY \"Incident Year\"
ORDER BY 1
"; ""; ""; "Injury"
)
I am trying to store the result of a select statement (in temp memory maybe?) and then call the saved result and be able to use it again in other selects so it doesn't have to execute again and again which would increase the execution time. I was thinking maybe using a procedure or a cursor would be the solution, but I'm know enough SQL to be able to use it, any help would be helpful!
Here is the select statement that I would like to store:
select "PO Number2", "Total"
from (select distinct "PO Number2", sum("Amount") as "Total"
from (select distinct account as Account,
company as Company,
year_period_key as "Year Period Key",
voucher_no as "Voucher No",
voucher_type as "Vou Type",
voucher_date as "GRN/Invoice Date",
reference_number as "PO Number2",
code_c as "Project ID",
ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(reference_number) as "Supplier ID",
ifsapp.SUPPLIER_INFO_API.Get_Name(ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(reference_number)) as "Supplier Name",
sum(quantity) as "Quantity",
sum(amount) as "Amount",
sum(debet_amount) as "Debit Amount",
sum(credit_amount) as "Credit Amount"
from ifsapp.GEN_LED_VOUCHER_ROW_UNION_QRY
where account = '222010'
and Voucher_Type = '0'
group by voucher_type,
account,
year_period_key,
voucher_date,
reference_number,
code_c,
company,
voucher_no,
party_type_id
union all
select distinct account as Account,
company as Company,
year_period_key as "Year Period Key",
voucher_no as "Voucher No",
voucher_type as "Vou Type",
voucher_date as "GRN/Invoice Date",
ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number) as "PO Number2",
code_c as "Project ID",
ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number)) as "Supplier ID",
ifsapp.SUPPLIER_INFO_API.Get_Name(ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number))) as "Supplier Name",
sum(quantity) as "Quantity",
sum(amount) as "Amount",
sum(debet_amount) as "Debit Amount",
sum(credit_amount) as "Credit Amount"
from ifsapp.GEN_LED_VOUCHER_ROW_UNION_QRY
where account = '222010'
and Voucher_Type = 'J'
group by voucher_type,
account,
year_period_key,
voucher_date,
reference_number,
code_c,
company,
voucher_no,
party_type_id)
where "Year Period Key" <= '&YearPeriodKey'
group by "PO Number2")
where "Total" = 0
Thanks In advance! :)
Oracle has it's own optimizers that help you not perform the same query multiple times. It'll store the results on the server memory (limited) if the query is exactly the same. So if you perform this query 5 times in one minute, it'll will not consume many resources. In advance, you should see if a materialized view does not solve you problem.
Are you wanting to "store" the result and re-use within a single session or to store the result for other sessions to re-use. To materialize teh query data such that it becomes visible to other sessions, and remains so until such time as a refresh is invoke (manually or scehudled), then really you're looking at materialised views. If this is just for the purpose of a single session/job and prevent having to re-query the data repeatedly, remember that with ref-cursors you can only go forwards. You could write the data to a PLSQL table (array/collection), but if the dataset is large, watch out for memory. For larger datasets, probably the best bet are temporary tables.