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
Related
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;
The original query:
SELECT o.offering_number,
o.english_description,
o.french_description,
fop.price_amount,
fop.price_type_code,
fop.price_status_code,
fop.offering_id,
(SELECT fop1.price_amount from facility_offering_price fop1
WHERE fop.offering_id = fop1.Offering_Id
AND fop1.price_type_code = 5
AND fop1.price_status_code = 3
) as 'priceAmount'
from facility_offering_price fop
join offering o on fop.offering_id = o.offering_id
WHERE fop.price_start_date = '15-10-28'
AND fop.price_status_code IN (1,2)
/*AND (price_status_code IS NULL)*/
AND fop.price_type_code = 5
/*AND (o.offering_number IS NULL)*/
ORDER BY o.offering_number ASC, fop.price_sequence_number ASC;
It produces a result of one entry.
The result query:
SELECT o.offering_number,
o.english_description,
o.french_description,
fop.price_amount,
fop2.price_amount,
fop.price_type_code,
fop.offering_id,
fop2.offering_id
from facility_offering_price fop
join offering o on fop.offering_id = o.offering_id
inner join
(select
fop1.offering_id,
fop1.price_amount
from facility_offering_price fop1
WHERE fop1.price_type_code = 5
AND fop1.price_status_code = 3
) fop2 on fop.offering_id = fop2.offering_id
WHERE fop.price_start_date = '15-10-28'
AND fop.price_status_code IN (1,2)
/*AND (price_status_code IS NULL)*/
AND fop.price_type_code = 5
/*AND (o.offering_number IS NULL)*/
ORDER BY o.offering_number ASC, fop.price_sequence_number ASC;
It's result set is empty. However, an entry is found if I ask for fop1.price_status_code = 1.
Unable to wrap my head around this one I would appreciate your help.
Try using LEFT JOIN instead. The conversion from SELECT a, subquery AS val FROM ... to a join is more accurately reflected that way. The original query would return rows with NULL val when the subquery has no results; your version ends up omitting such rows completely.
im trying to use max() here in my sql but im confused because i am using join table and i dont have idea where i should insert the max()
i need to add the column name date_issue from the table crew_documents_table
here is my sql:
select *
from
info join
crew_documents_table on info.id = crew_documents_table.document_crew_id join
crew_rank on info.crew_rank = crew_rank.crew_rank_id
where
crew_rank in ('1','2','3','4','5') and
crew_status = '$crew_status' and
vessel = '$vessel_name'
group by full_name
You can customize your SQL query as below :
select *, MAX(crew_documents_table.date_issue) as max_date
from
info join
crew_documents_table on info.id = crew_documents_table.document_crew_id join
crew_rank on info.crew_rank = crew_rank.crew_rank_id
where
crew_rank in ('1','2','3','4','5') and
crew_status = '$crew_status' and
vessel = '$vessel_name'
group by full_name
MAX allows you to select the maximum date
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
I have the following SQL query
SELECT
DISTINCT
count("SiteTree_Live"."ID")
FROM
"SiteTree_Live"
LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2012-11%')
but it producing a wrong count as the query result. The total intented result this query is suppose to return should not be more than 245 but currently, its returning more than about "4569" results.
Thats is because of the JOIN on the "DepartureDate" table as the query returns the expected result when i remove the join from the "DepartureDate" table.
What modification do i need to make to my query to count the Macthes between "SiteTree_Live"."ID" and "DepartureDate"."TourID" whiles counting only the "SiteTree_Live"."ID" count excluding the Departure dates?
Any suggestions welcomed :)
THE ANSWER
SELECT
COUNT(DISTINCT SiteTree_Live.ID)
FROM
"SiteTree_Live" LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2013-03%')
Seems to give me the right result. Thanks for the tip #Michael Berkowski
Minor correction: if DepartureDate is a date-type, then the LIKE '2013-03% will force it to be coerced into a character type (this is a mysql feature) As a result, any indexes on DepartureDate will not be used, IIRC. Better use a plain range-query:
SELECT
COUNT(DISTINCT stl.ID)
FROM
SiteTree_Live stl
LEFT JOIN
DepartureDate dd ON dd.TourID = stl.ID
WHERE
stl.Locale = 'en_AU'
AND stl.ClassName = 'TourPage'
AND dd.DepartureDate >= '2013-03-01'
AND dd.DepartureDate < '2013-04-01'
;
Do this (You have a bunch of unneeded joins)
SELECT
COUNT(DISTINCT SiteTree_Live.ID)
FROM
`SiteTree_Live`
LEFT JOIN
`DepartureDate` ON `DepartureDate`.`TourID` = `SiteTree_Live`.`ID`
WHERE
`SiteTree_Live`.`Locale` = 'en_AU'
AND `SiteTree_Live`.`ClassName` = 'TourPage'
AND `DepartureDate`.`DepartureDate` LIKE '2013-03%'
You could also do a GROUP BY:
SELECT
COUNT(SiteTree_Live.ID)
FROM
`SiteTree_Live`
LEFT JOIN
`DepartureDate` ON `DepartureDate`.`TourID` = `SiteTree_Live`.`ID`
WHERE
`SiteTree_Live`.`Locale` = 'en_AU'
AND `SiteTree_Live`.`ClassName` = 'TourPage'
AND `DepartureDate`.`DepartureDate` LIKE '2013-03%'
GROUP BY
SiteTree_Live.ID