I'm having an issue with the following query
select
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end as DateEnd, coup.coupon_id
from `order` ord
inner join order_product ordProduct on ord.order_id = ordProduct.order_id
inner join coupon_product coupProd on ordProduct.product_id = coupProd.product_id
inner join coupon coup on coupProd.coupon_id = coup.coupon_id
where (coup.date_end > curdate());
If I remvove the where clause, the query executes fine, otherwise it just hangs. Any ideas?
It's not a solution per se, but as a workaround, you could maybe get it done as a nested query. i.e. ,
SELECT * FROM (
SELECT
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end AS DateEnd, coup.coupon_id
FROM `order` ord
INNER JOIN order_product ordProduct ON ord.order_id = ordProduct.order_id
INNER JOIN coupon_product coupProd ON ordProduct.product_id = coupProd.product_id
INNER JOIN coupon coup ON coupProd.coupon_id = coup.coupon_id)
WHERE (DateEnd > CURDATE());
Related
i tried use "Total" in outer where, and i got error "Unknown "AL.Total", how to fix it ?
here my query :
SELECT
AL.CODE,
AL.NAME,
SUM(AL.BEGINING+AL.MUTATION) AS TOTAL
from
(SELECT
ACC.CODE,
ACC.NAME,
ACC.BEGINING,
SUM(JOURNAL.DEBIT-JOURNAL.KREDIT) AS MUTATION
FROM
G_JOURNAL INNER JOIN P ON Category.Category= G_JOURNAL.Category
INNER JOIN JOURNAL ON G_JOURNAL.NOINDEX = JOURNAL.NOINDEX_JOURNAL
INNER JOIN ACCOUNT ON JOURNAL.CODE = ACC.CODE
INNER JOIN SUBCLASS ON SUBCLASS.NOSUBCLASS= ACC.NOSUBCLASS
INNER JOIN CLASS ON SUBCLASS.NOSUBCLASS = CLASS.NOCLASS
WHERE
(MONTH(G_JOURNAL.DATE) <= '12' AND YEAR (G_JOURNAL.DATE<= '2020')
AND
SUBCLASS.NOSUBCLASS=120
GROUP BY ACC.NAME) AL
WHERE AL.TOTAL >0
GROUP BY AL.NAME
ORDER BY AL.CODE ASC
Try HAVING TOTAL >0 instead of WHERE AL.TOTAL >0,because TOTAL
is not in AL temp table
SELECT
AL.CODE,
AL.NAME,
SUM(AL.BEGINING+AL.MUTATION) AS TOTAL
from
(SELECT
ACC.CODE,
ACC.NAME,
ACC.BEGINING,
SUM(JOURNAL.DEBIT-JOURNAL.KREDIT) AS MUTATION
FROM
G_JOURNAL INNER JOIN P ON Category.Category= G_JOURNAL.Category
INNER JOIN JOURNAL ON G_JOURNAL.NOINDEX = JOURNAL.NOINDEX_JOURNAL
INNER JOIN ACCOUNT ON JOURNAL.CODE = ACC.CODE
INNER JOIN SUBCLASS ON SUBCLASS.NOSUBCLASS= ACC.NOSUBCLASS
INNER JOIN CLASS ON SUBCLASS.NOSUBCLASS = CLASS.NOCLASS
WHERE
(MONTH(G_JOURNAL.DATE) <= '12' AND YEAR (G_JOURNAL.DATE<= '2020')
AND
SUBCLASS.NOSUBCLASS=120
GROUP BY ACC.NAME) AL
GROUP BY AL.NAME
HAVING TOTAL >0
ORDER BY AL.CODE ASC
Please check the below code.
SELECT
`order`.idorder
, order_status_code.idorder_status_code
, order_status_code.order_status_code
, user.iduser
, `order`.required_delivery_date
, `order`.cancel
, `order`.date_created
, `order`.last_updated
, COUNT(order_item.idorder_item)
from
`order`
INNER JOIN order_status_code
ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user
ON `order`.iduser = user.iduser
INNER JOIN order_item
ON order_item.idorder = `order`.`idorder`
WHERE
`order`.iduser = 1
In here, I want the COUNT(order_item.idorder_item) to return the number of items under the idorder. In other words, if I run that SQL Part along, that would be like below
SELECT
COUNT(`idorder_item`)
from
order_item
where
idorder = 1
How can I get this done in my main query?
SELECT `order`.idorder,
order_status_code.idorder_status_code,
order_status_code.order_status_code,
user.iduser,
`order`.required_delivery_date,
`order`.cancel,
`order`.date_created,
`order`.last_updated,
COUNT(order_item.idorder_item),
(SELECT COUNT(`idorder_item`)
from order_item
where idorder=1) as count_idorder_item
from `order`
INNER JOIN order_status_code ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user ON `order`.iduser = user.iduser
INNER JOIN order_item ON order_item.idorder = `order`.`idorder`
WHERE `order`.iduser= 1
I have a query:
SELECT * FROM `InvoiceLines`
INNER JOIN Invoices ON Invoices.`invoiceID` = InvoiceLines.`invoiceID`
INNER JOIN `DistributorOrdersTbl` ON DistributorOrdersTbl.`distOrderRecID` = Invoices.`orderID`
WHERE DistributorOrdersTbl.`distOrderRecID` = 3829
AND InvoiceLines.`qty` > 0
Which returns a number of Invoices:
I wanted to select only the invoices with the newest ID 2800. The way I ended up doing it was a bit of copy pasting:
SELECT * FROM `InvoiceLines`
INNER JOIN Invoices ON Invoices.`invoiceID` = InvoiceLines.`invoiceID`
INNER JOIN `DistributorOrdersTbl` ON DistributorOrdersTbl.`distOrderRecID` = Invoices.`orderID`
WHERE DistributorOrdersTbl.`distOrderRecID` = 3829
AND InvoiceLines.`qty` > 0
AND Invoices.invoiceID = (
SELECT MAX(Invoices.invoiceID) FROM `InvoiceLines`
INNER JOIN Invoices ON Invoices.`invoiceID` = InvoiceLines.`invoiceID`
INNER JOIN `DistributorOrdersTbl` ON DistributorOrdersTbl.`distOrderRecID` = Invoices.`orderID`
WHERE DistributorOrdersTbl.`distOrderRecID` = 3829
AND InvoiceLines.`qty` > 0
)
And sure enough, the correct invoices are returned. However it feels as though this is inefficient as I am essentially performing the query twice. What would be the best way of doing this? I tried the following but it seems this is incorrect SQL:
SELECT * FROM `InvoiceLines`
INNER JOIN Invoices ON Invoices.`invoiceID` = InvoiceLines.`invoiceID`
INNER JOIN `DistributorOrdersTbl` ON DistributorOrdersTbl.`distOrderRecID` = Invoices.`orderID`
WHERE DistributorOrdersTbl.`distOrderRecID` = 3829
AND InvoiceLines.`qty` > 0
AND Invoices.invoiceID = MAX(Invoices.invoiceID)
Which returns "Invalid use of group function".
Thanks in advance!
Assuming all invoices have invoice lines, you can use a subquery just on Invoices:
SELECT *
FROM (SELECT i.*
FROM Invoices i
WHERE i.orderID = 3829
ORDER BY i.invoiceID DESC
LIMIT 1
) i JOIN
InvoiceLines il
ON i.invoiceID = il.invoiceID JOIN
DistributorOrdersTbl d
ON d.distOrderRecID = i.orderID
WHERE il.qty > 0;
I am having an issue understanding the inner and left join
I am having the below query in the outsystems
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
The issue is the Select is pulling all the CLD elements without respect to the Project, I am trying to select CLD's whose Project id = Project.Id. I tried both the joins but it keep pulling all the values
Below how the structure looks like
Please try the following: First get the CLDProjects matching with Project then get the CLD from matched records.
FROM {CLD} INNER JOIN (
{CLDProjects} INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
) ON {CLD}.[Id] = {CLDProjects}.[CLDId]
You're probably missing the inner join to CLDProjects on the ProjectParticipant subquery. Add
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId])
on the second EXISTS join conditions, otherwise it will the second results will match the exists for every project the user is in, ignoring the other conditions over CLDProjects. Try the following:
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
Also.. make sure you're not passing #IsJAXPM as 1... otherwise it will definitely return all records.
Let us know if that works. Otherwise, please extend the diagram to show the ProjectParticipant and ProjectParticipantWidget tables as well.
I think you just need to reorder your joins, I tried to reproduce your case (with incorrect joins order), but it gives me error when test the query,
but if you reorder the the joins, it works. note that I am using OutSystems 10
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
order by
Ad_Date_Created
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
I keep getting an error that multi part data can not be bound. Please help to correct query
Try this:
select Distinct
_Ad.ad_id, _Ad.Ad_Name,
ID.Image_Path, VM.year,
VD.Vehicle_Transformation, VD.Vehicle_Fuel_Type, VD.Vehicle_Mileage
from
_Ad
inner join
_Image_Details ID on ID.ad_id = _Ad.ad_id
inner join
_Vehicle_Model VM on VM.vehicle_model_id = _AD.vehicle_model_id
inner join
_Vehicle_Details VD on _ad.ad_id = VD.ad_id;
order by
Ad_Date_Created
The syntax of your SQL statement is wrong. An ORDER BY clause should come after the JOIN's