I'm trying to create a stored procedure with the in parameters first date, last date and country
This is the normal query witch works just fine.
SELECT CAR_TYPE.NAME, CAR_TYPE.TYPE, CAR.DAY_PRICE, CAR.REGISTRATION_NUMBER, AGENCY.NAME, BRANCH.NAME
FROM CAR
JOIN CAR_TYPE ON CAR_TYPE.ID = CAR.CAR_TYPE_ID
JOIN BRANCH ON BRANCH.ID = CAR.BRANCH_ID
JOIN BRANCH_ADDRESS ON BRANCH_ADDRESS.BRANCH_ID = BRANCH.ID
JOIN AGENCY ON AGENCY.ID = BRANCH.ID`
WHERE CAR.ID NOT IN
(SELECT BOOKING.CAR_ID FROM BOOKING WHERE BOOKING.PICKUP_DATE < '2013-01-01' AND BOOKING.RETURN_DATE > '2013-12-12')
AND BRANCH_ADDRESS.CITY_NAME = 'Stockholm'
end
I can't make out what you are doing from your question, as it is not formatted well.
I have reformatted it so that it looks like a proper query, and made some changes to improve readability.
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID
WHERE ba.CITY_NAME = 'Stockholm'
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < '2013-01-01'
AND RETURN_DATE > '2013-12-12')
if this is SQL server, then the statement to cerate a basic (without error handling) stored proc would be
Create Procedure dbo.FetchCarData -- or whatever you want to name it
#firstDate DateTime,
#lastDate DateTime,
#country varChar(50)
As
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID`
WHERE ba.CITY_NAME = #country
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < #firstDate
AND RETURN_DATE > #lastDate )
in MySQL I think it would look like this...
Create Procedure FetchCarData( -- or whatever you want to name it
#firstDate DateTime,
#lastDate DateTime,
#country varChar(50))
Begin
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID`
WHERE ba.CITY_NAME = #country
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < #firstDate
AND RETURN_DATE > #lastDate )
End
Related
I have bd hf3 and 5 tables there:
active_preset with columns (id , preset_id)
preset with columns (id , birja_id, trend_id, fractal, interval_up)
birja with columns (id , name)
trend with columns (id , name)
uq_active_preset with columns (id , birja, trend, fractal, interval_up)
In table preset I have a few records. Some of them are in table active_preset by foreign key preset_id. In table active_preset a few records exist once , a few more than once.
I need to update table uq_active_preset with records from table active_preset disregarding repetitions of records if they are present.
I did query from active_preset and it works good:
SELECT
b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
FROM hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
But I don't know how to update uq_active_preset
I tried this and it returns syntax error:1064 :
UPDATE hf3.uq_active_preset uap SET
uap.birja = st.birja ,
uap.fractal = st.fractal,
uap.trend = st.trend,
uap.interval_up = st.interval_up,
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
when you make an update using from is like you join the updated table with your query result. So, you need also a where statement in order to tell where those two are connected. Also, don't use alias of your updated table on set statement.
You need something like that:
UPDATE hf3.uq_active_preset uap SET birja=st.birja,fractal=st.fractal,trend=st.trend,interval_up=st.interval_up
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
where uap.fkey=st.fkey
Currently we have to run the following SQL Query to find all customers that have not been assigned access to a downloadable product.
SELECT c.entity_id as customer_id, dp.purchased_id as purchased_id
FROM magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp on so.increment_id = dp.order_increment_id
where
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null and
dp.customer_id != c.entity_id
;
Which produces this:
customer_id purchased_id
99999 55555
Then with the results, we manually have to go through, and for each customer_id, and purchase_id run the following query:
UPDATE `magento_dev`.`downloadable_link_purchased` SET `customer_id` = '99999' WHERE (`purchased_id` = '55555');
So for the SQL Ninja's, any suggestions on how to make this a single query that finds the mismatched assignments and then does the update/insert at once?
Just put all the JOINs in the UPDATE query.
UPDATE `magento_dev`.`downloadable_link_purchased` AS dp1
JOIN magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp
ON so.increment_id = dp.order_increment_id
AND dp.customer_id != c.entity_id
AND dp1.purchased_id = dp.purchased_id
SET dp1.customer_id = c.entity_id
where
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null
Can be done like sub query and join with the table which should be updated,
UPDATE
magento_dev.downloadable_link_purchased F
JOIN
(SELECT c.entity_id as customer_id, dp.purchased_id as purchased_id
FROM magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp on so.increment_id = dp.order_increment_id
WHERE
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null and
dp.customer_id != c.entity_id) S
ON F.purchased_id = S.purchased_id
SET F.customer_id = S.customer_id;
I want to add a condition in the procedure, but I can't use (IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
This is my procedure:
CREATE DEFINER=`akarremote`#`%` PROCEDURE `laporan_top_brand_penjualan`(IN Wh INT, IN `Datemin` VARCHAR(191), IN `Datemax` VARCHAR(191))
BEGIN
SELECT
A.item_id,
A.item_name AS ITEM,
(
SELECT
SUM( C.jumlah ) AS jumlah
FROM
mob_penjualan B
LEFT JOIN mob_penjualandetail C ON C.noTransaksi = B.noTransaksi
LEFT JOIN krd_wh D ON D.wh_id = B.idWH
WHERE
C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
(IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
GROUP BY
C.idItemProduk
ORDER BY
jumlah DESC
) AS JML,
(
SELECT
SUM( C.qty ) AS qty
FROM
mob_penjualan B
LEFT JOIN mob_penjualandetail C ON C.noTransaksi = B.noTransaksi
LEFT JOIN krd_wh D ON D.wh_id = B.idWH
WHERE
C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
(IF(Wh=0,'AND 1=1','AND D.wh_parn = 102'))
GROUP BY
C.idItemProduk
ORDER BY
qty DESC
) AS QTY
FROM
krd_item A
ORDER BY
JML DESC, QTY DESC;
END
What's the solution?
WHERE C.idItemProduk = A.item_id
AND B.TglTransaksi BETWEEN Datemin AND Datemax
AND IF(Wh=0, 1, D.wh_parn = 102)
Someone please explain the difference between parent query and sub query? How do I understand?
e.g:
SELECT
ra.ID id,
ra.ACCOUNT_ID accountId,
ef.REAL_NAME realName,
a.MOBILE mobile,
sps.`NAME` siteName,
ef.CREATE_TIME createTime,
a.`STATUS` status,
ra.`STATUS` siteStatus,
(
SELECT
aif.APPROVER_NAME
FROM
audit_info aif
WHERE
aif.TARGET_ID = a.ID AND af.AUDIT_TYPE=2
AND aif.CREATE_TIME = (
SELECT
MAX(af.CREATE_TIME)
FROM
audit_info af
WHERE
af.TARGET_ID = a.ID AND af.AUDIT_TYPE=2
)
) AS approverName
FROM
account a
INNER JOIN site_relation_account ra ON ra.ACCOUNT_ID = a.ID
INNER JOIN account_ext ef ON ra.ACCOUNT_ID = ef.ACCOUNT_ID
INNER JOIN service_provider_site sps ON sps.ID = ra.SITE_ID
LEFT JOIN audit_info af ON af.TARGET_ID = ef.ACCOUNT_ID
WHERE ra.ACCOUNT_TYPE = 2
I have this table: http://sqlfiddle.com/#!2/b4060/2
I then created two views as follow:
-- stack 1: hire
create view HS1H as
select a.* , min(a.`Effective_Date`)
from `Table1` a
left join `Table1` b on a.`Employee_ID` = b.`Employee_ID`
and a.`Effective_Date` > b.`Effective_Date`
where a.`Event_Type` = "1_Hire"
group by a.`Employee_ID`;
select * from `hs1h`;
-- stack 1: termination
create view HS1T as
select a.* , min(a.`Effective_Date`)
from `Table1` a
left join `Table1` b on a.`Employee_ID` = b.`Employee_ID`
and a.`Effective_Date` > b.`Effective_Date`
where a.`Event_Type` = "5_Term"
group by a.`Employee_ID`;
select * from `hs1t`;
I want to get the events that happen between first Hire date and first Term date. I used the qry below but returned no results:
select a.*
from `Table1` a
join `hs1h` b on a.`Employee_ID` = b.`Employee_ID`
join `hs1t` c on a.`Employee_ID` = c.`Employee_ID`
where a.`Effective_Date` between b.`Effective_Date` and c.`Effective_Date`;
I am not sure what went wrong. I was able run the following two qrys. One returned the events after first hire date, and the other returned the events before first term date. But when I combine them like the one above, it didn't work.
select a.*
from `Table1` a
join `hs1h` b on a.`Employee_ID` = b.`Employee_ID`
join `hs1t` c on a.`Employee_ID` = c.`Employee_ID`
where a.`Effective_Date` > b.`Effective_Date`;
select a.*
from `Table1` a
join `hs1h` b on a.`Employee_ID` = b.`Employee_ID`
join `hs1t` c on a.`Employee_ID` = c.`Employee_ID`
where a.`Effective_Date` < c.`Effective_Date`;
SELECT *
FROM table1
WHERE `effective date`
BETWEEN (select MIN(`effective date`) from `Table1` WHERE `event type` = '1_Hire')
AND
(select MIN(`effective date`) FROM table1 WHERE `event type` = '5_Term')
For the 2nd or 3rd 'hire', things get a little more complicated, but something like this should work...
SELECT a.*
FROM TH_Sample_Data a
JOIN (
SELECT x.*
, MIN(y.effective_date) end
, #i := #i+1 rank
FROM TH_Sample_Data x
JOIN TH_Sample_Data y
ON y.effective_date >= x.effective_date
AND y.event_type = '5_Term'
, (SELECT #i:=1) vars
WHERE x.event_type = '1_Hire'
GROUP
BY x.id
) b
ON a.effective_date BETWEEN b.effective_date and b.end
WHERE b.rank = 2;