I'm working on a query in Access, but I'm really struggling to return a count of unique rows, and similarly a sum of unique values.
My query is as follows:
SELECT SiteOrders.CustomerID, Count(SiteOrders.ID) AS CountOfID, Sum(SiteOrders.Total) AS SumOfTotal, Sum(SiteOrderItems.Total) AS BrandTotal, Sum(SiteOrderItems.Quantity) AS SumOfQuantity
FROM SiteProducts INNER JOIN (SiteOrderItems INNER JOIN SiteOrders ON SiteOrderItems.OrderID = SiteOrders.ID) ON SiteProducts.ID = SiteOrderItems.ProductID
WHERE (((SiteProducts.Brand)="1") AND ((SiteOrders.OrderDate)>#4/1/2017# And (SiteOrders.OrderDate)<#4/1/2020#) AND ((SiteOrders.Paid)=True))
GROUP BY SiteOrders.CustomerID
ORDER BY SiteOrders.CustomerID;
The two fields where I want distinct values are a count on SiteOrders.ID and a sum on SiteOrders.Total.
Many thanks
Need nested query for aggregation of SiteOrders. Consider:
SELECT SiteOrders.CustomerID, Sum(SiteOrderItems.Quantity) AS SumQuantity,
Sum(SiteOrderItems.Total) AS SumTotal, Query1.CountOrderID, Query1.SumOrderTotal
FROM (SELECT CustomerID, Count(ID) AS CountOrderID, Sum(Total) AS SumOrderTotal
FROM SiteOrders
WHERE (((OrderDate) Between #4/1/2017# And #3/31/2020#) AND ((Paid)=True))
GROUP BY CustomerID) AS Query1
INNER JOIN (SiteOrders INNER JOIN (SiteProducts INNER JOIN SiteOrderItems
ON SiteProducts.ID = SiteOrderItems.ProductID)
ON SiteOrders.ID = SiteOrderItems.OrderID)
ON Query1.CustomerID = SiteOrders.CustomerID
GROUP BY SiteOrders.CustomerID, Query1.CountOrderID, Query1.SumOrderTotal;
Related
I have a query like below
select
COALESCE(Clinic,'total') as Clinic,
sum(Non_Billable) as Non_Billable,
sum(initial_Non_Billable) as initial_Non_Billable,
sum(Non_Billable)/NULLIF(sum(initial_Non_Billable),0) as Non_Billable_initial_revenue
FROM (
select
businesses.label as Clinic,
sum(CASE
WHEN appointment_types.category IN (
'Others',
'Non-Billable')
and appointment_types.name like '%initial%'
then invoices.net_amount ELSE 0 END)
as Non_Billable,
count(CASE
WHEN appointment_types.category IN (
'Others',
'Non-Billable')
and appointment_types.name like '%initial%'
then appointment_types.name ELSE null END)
as initial_Non_Billable
FROM
individual_appointments
INNER join appointment_types on
appointment_types.id = individual_appointments.appointment_type_id
inner join invoices on
invoices.appointment_id = individual_appointments.id
inner join businesses on
businesses.id = individual_appointments.business_id
group by
businesses.label,
appointment_types.name,
appointment_types.category,
invoices.net_amount
)x
group by
ROLLUP(Clinic);
is it possible for each condition in "case when then" to have a different table?
example: for "Non_Billable" only want from individual_appointment, appointment_types, invoices and businesses table only.
while "initial_Non_Billable" only wants from individual_appointment, appointment_types, and businesses table.
can it be like that?
If possible, how? can anyone give an example?
From what I see, I suppose the problem here is that there can be many invoices per appointment, so when you join invoices you get appointments multifold which also multiplies your counts.
You don't really want to join invoices hence, but invoice sums. I.e. aggregate before joining.
Then, you are only interested in non-billable appointments, so you can put that criteria into your ON clauses and outer join in order to get clinics without such appointments.
select
coalesce(b.label, 'total') as clinic,
coalesce(sum(i.total_net_amount), 0) as initial_non_billable_sum,
count(at.id) as initial_non_billable_count,
avg(i.total_net_amount) as initial_non_billable_average
from businesses b
left join individual_appointments ia on ia.business_id = b.id
left join appointment_types at on at.id = ia.appointment_type_id
and at.category in ('others', 'non-billable')
and at.name like '%initial%'
left join
(
select appointment_id, sum(net_amount) as total_net_amount
from invoices
group by appointment_id
) i on i.appointment_id = ia.id
group by rollup(b.label)
order by b.label nulls last;
I have a table that looks like this:
For each COMPANY there are multiple NATURAL_PERSON_ID, every NATURAL_PERSON have a date in which an audit was performed FECHA_DE_REPORTE and as a company there is a date in which the first loan was give to that company.
What I want is to select for each NATURAL_PERSON all the FOLIO_CONSULTA whose FECHA_DE_REPORTE is less or equal to FIRST_LOAN (the date in which the first loan was given for that company) Then I need to find the MAX date among each group and keep al the information (the whole row) for the value that fulfills all these conditions, and all this for each NATURAL_PERSON
So for this example the result I expected is all the information of the second row since this is the MAX() of FECHA_DE_REPORTE by COMPANY AND NATURAL_PERSON.
I have tried:
SELECT NPC.COMPANY_ID
,NPC.NATURAL_PERSON_ID
,NPS.DIGITAL_SIGNATURE_ID
,CDC.FOLIO_CONSULTA
,CDC.FECHA_DE_REPORTE
,FIRST_LOAN.FIRST_LOAN
,MAX(CDC.FECHA_DE_REPORTE) MAX_FOLIO_CONSUTA
FROM KONFIO.NATURAL_PERSON_COMPANY NPC
LEFT JOIN KONFIO.NATURAL_PERSON_SIGNATURE NPS ON NPS.NATURAL_PERSON_ID = NPC.NATURAL_PERSON_ID
JOIN KONFIO.CDC_RESPONSE CDC ON CDC.DIGITAL_SIGNATURE_ID= NPS.DIGITAL_SIGNATURE_ID
JOIN
(
SELECT CAPP.COMPANY_ID
,MIN(LOAN.DOCUMENTATION_DATE) FIRST_LOAN
FROM KONFIO.COMPANY_APPLICATION CAPP
JOIN KONFIO.LOAN ON LOAN.APPLICATION_ID = CAPP.APPLICATION_ID
GROUP BY CAPP.COMPANY_ID) FIRST_LOAN ON FIRST_LOAN.COMPANY_ID = NPC.COMPANY_ID
WHERE CDC.FECHA_DE_REPORTE <= FIRST_LOAN.FIRST_LOAN
AND NPC.COMPANY_ID IN (1033)
GROUP BY NPC.COMPANY_ID, NPC.NATURAL_PERSON_ID
but it retrieves the first value that finds so the FOLIO_CONSULTA does not correspond to the FOLIO_CONSULTA of the MAX() FECHA_DE_REPORTE
Any help would be appreciated
You should join the subquery for MAX(FECHA_DE_REPORTE) on table CDC_RESPONSE
SELECT NPC.COMPANY_ID
,NPC.NATURAL_PERSON_ID
,NPS.DIGITAL_SIGNATURE_ID
,CDC.FOLIO_CONSULTA
,CDC.FECHA_DE_REPORTE
,FIRST_LOAN.FIRST_LOAN
,T.MAX_FOLIO_CONSUTA
FROM KONFIO.NATURAL_PERSON_COMPANY NPC
INNER JOIN (
SELECT DIGITAL_SIGNATURE_ID
, MAX(FECHA_DE_REPORTE) MAX_FOLIO_CONSUTA
FROM KONFIO.CDC_RESPONSE
GROUP BY DIGITAL_SIGNATURE_ID
) T ON T.DIGITAL_SIGNATURE_ID = NPS.DIGITAL_SIGNATURE_ID
AND T.MAX_FOLIO_CONSUTA = CDC.FECHA_DE_REPORTE
LEFT JOIN KONFIO.NATURAL_PERSON_SIGNATURE NPS ON NPS.NATURAL_PERSON_ID = NPC.NATURAL_PERSON_ID
JOIN KONFIO.CDC_RESPONSE CDC ON CDC.DIGITAL_SIGNATURE_ID= NPS.DIGITAL_SIGNATURE_ID
JOIN
(
SELECT CAPP.COMPANY_ID
,MIN(LOAN.DOCUMENTATION_DATE) FIRST_LOAN
FROM KONFIO.COMPANY_APPLICATION CAPP
JOIN KONFIO.LOAN ON LOAN.APPLICATION_ID = CAPP.APPLICATION_ID
GROUP BY CAPP.COMPANY_ID) FIRST_LOAN ON FIRST_LOAN.COMPANY_ID = NPC.COMPANY_ID
WHERE CDC.FECHA_DE_REPORTE <= FIRST_LOAN.FIRST_LOAN
AND NPC.COMPANY_ID IN (1033)
GROUP BY NPC.COMPANY_ID, NPC.NATURAL_PERSON_ID
...... missing part
This is my query.
SELECT dr_trans_dtl.dr_ID, jo_trans_dtl.qty
FROM dr_trans_dtl
LEFT JOIN jo_trans_dtl ON dr_trans_dtl.jo_no = jo_trans_dtl.jo_no
WHERE dr_trans_dtl.dr_no = '3329' GROUP BY dr_trans_dtl.dr_ID
Here is the actual result:
What I wanted is the qty should be like this (500,40,1). Because that is the data in jo_trans_dtl.
It seems you need aggregate function as you used group by
SELECT dr_trans_dtl.dr_ID,sum(jo_trans_dtl.qty) as qty
FROM dr_trans_dtl
JOIN jo_trans_dtl ON dr_trans_dtl.jo_no = jo_trans_dtl.jo_no
WHERE dr_trans_dtl.dr_no = '3329'
GROUP BY dr_trans_dtl.dr_ID
I have a table:
and this table:
I would like to create report like this
I've tried this SQL:
select
master_problem.problem,
master_problem.sop_reference,
master_problem.adidas_spec,
count(log_roving_qc.id_problem) as jumlahfrom
master_problem
inner join log_roving_qc
on master_problem.id_problem = log_roving_qc.id_problem
group by master_problem.id_problem
but the empty data does not show. I want to display blank data with a description of 0
Do a left join of the master_problem table to a subquery which does the count aggregation:
SELECT
mp.problem,
mp.sop_reference,
mp.adidas_spec,
COALESCE(t.cnt, 0) AS jumlahfrom
FROM master_problem mp
LEFT JOIN
(
SELECT id_problem, COUNT(*) as cnt
FROM log_roving_qc
GROUP BY id_problem
) t
ON mp.id_problem = t.id_problem;
I'm trying obtain one tabla of cars which have three rows in relations with other three columns (make, model and group) and I only want obtain one car by model.
Here a image of MySQL table:
You will see three rows with same model_id (model_id is foreign key the other table, the other table is called models)
My SQL query for obtain those cars are:
SELECT *
FROM gm_cars AS cars
INNER JOIN gm_cars_makes AS makes
ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models
ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups
ON (cars.group_id = groups.group_id) AND
makes.make_visible = 1
ORDER BY cars.model_id;
but I wish obtain one row for one model, here one example (I have used Photoshop):
Some like: SELECT *, DISTINCT(model_id) FROM cars
If you still want to return all columns, you can create sub-query to return only one Car per Model and then write you query as before:
SELECT * FROM gm_cars AS cars
INNER JOIN (SELECT model_id, MAX(car_Id) AS car_Id FROM gm_cars GROUP BY model_id) AS grp_cars ON grp_cars.car_Id = cars.car_Id
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id) AND makes.make_visible = 1 ORDER BY cars.model_id;
You can also add GROUP BY in you main query, and add aggregation function to all other columns. But it can return you columns from different cars with the same model:
SELECT cars.model_id,
MAX(car_passengers),
MAX(car_suitcases),
....
FROM gm_cars AS cars
INNER JOIN (SELECT model_id, MAX(car_Id) AS car_Id FROM gm_cars GROUP BY model_id) AS grp_cars ON grp_cars.car_Id = cars.car_Id
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id) AND makes.make_visible = 1
GROUP BY cars.model_id
ORDER BY cars.model_id;
SELECT *
FROM gm_cars AS cars
INNER JOIN gm_cars_makes AS makes ON (cars.make_id = makes.make_id)
INNER JOIN gm_cars_models AS models ON (cars.model_id = models.model_id)
INNER JOIN gm_cars_groups AS groups ON (cars.group_id = groups.group_id)
AND makes.make_visible = 1
group by (model_id)
ORDER BY cars.model_id
is this helpful?
;with cte
AS
(
Select *,row_number() OVER(partition by model_id order by car id) rn from gm_cars
)
select * from cte where rn=1