I'm trying to get special value...
if empty p.price + ifnull = option_price_total
else special = option_price_total
.
SELECT
p.product_id,
p.price,
(SELECT ifnull(MIN(price), 'empty') AS FIELD_1 FROM etp_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND (ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW()) ORDER BY ps.priority, ps.price LIMIT 1) AS special,
///
IF special != 'empty'
Do this subquery ---v
(SELECT ifnull(MIN(price), '0.00') AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = p.product_id) AS option_price,
p.price + ifnull((SELECT MIN(price) AS FIELD_2 FROM etp_product_option_value pov WHERE pov.product_id = p.product_id GROUP BY p.product_id, p.price), '0.00') AS option_price_total,
/// ELSE
/// ex. special value equals 15
/// special AS option_price_total
/// Output: option_price_total = 15
(SELECT MIN(etp_product_option_value.price) AS FIELD_1 FROM etp_product_option_value WHERE etp_product_option_value.product_id = p.product_id GROUP BY etp_product_option_value.product_id) AS options,
(SELECT AVG(rating) AS total FROM etp_review r1 WHERE r1.product_id = p.product_id AND r1.`status` = '1' GROUP BY r1.product_id) AS rating
FROM
etp_product p
LEFT OUTER JOIN etp_product_description pd ON (p.product_id = pd.product_id)
LEFT OUTER JOIN etp_product_to_store p2s ON (p.product_id = p2s.product_id)
INNER JOIN etp_product_to_vendor ON (p.product_id = etp_product_to_vendor.product_id)
INNER JOIN etp_vendor ON (etp_product_to_vendor.vendor_id = etp_vendor.vendor_id)
Any idea ?
Related
Here is my query,
SELECT
p.pcode,
p.productName,
s.pcode,
MAX(s.in_stock) AS opening_stock,
SUM(s.soldQty) AS sold_qty,
MIN(s.remaining_qty) AS closing_stock,
s.date_created,
g.pcode,
g.qty,
g.received_qty,
g.received_on
FROM tbl_products p
LEFT JOIN tbl_sold_items s
ON p.pcode = s.pcode
LEFT JOIN tbl_shop_gr_items g
ON p.pcode = g.pcode
WHERE
(s.date_created = '2019-09-27') AND
(g.received_on = '2019-09-27')
GROUP BY p.pcode
stock_report_sample
When the above query is executed it return null values when one of the selected table doesn't have the item id for that particular day. I want it to show a list of all the items(products) available even though the item has not been sold or received so that in those instances it should show zero (0)
Maybe you can use ISNULL to show zero value if NULL
SELECT
p.pcode,
p.productName,
s.pcode,
ISNULL(MAX(s.in_stock),0) AS opening_stock,
ISNULL(SUM(s.soldQty),0) AS sold_qty,
ISNULL(MIN(s.remaining_qty),0) AS closing_stock,
s.date_created,
g.pcode,
g.qty,
g.received_qty,
g.received_on
FROM tbl_products p
LEFT JOIN tbl_sold_items s ON p.pcode = s.pcode
LEFT JOIN tbl_shop_gr_items g ON p.pcode = g.pcode
WHERE(s.date_created = '2019-09-27') AND (g.received_on = '2019-09-27')
GROUP BY p.pcode
SELECT
p.pcode,
p.productName,
CASE
WHEN gr_rows IS NULL AND sl_rows IS NULL THEN 0
WHEN gr_rows > 0 AND sl_rows IS NULL THEN received_stock
WHEN gr_rows > 0 AND sl_rows > 0 THEN opening_stock
ELSE 0
END AS op_stock,
IFNULL(received_stock, 0),IFNULL(sold_stock, 0)
FROM tbl_products p
LEFT JOIN (
SELECT COUNT(r.ID) AS gr_rows,r.pcode,r.received_on,SUM(r.received_qty) AS received_stock
FROM tbl_shop_gr_items r
WHERE r.received_on = '2019-10-03'
GROUP BY r.pcode
)AS r ON p.pcode = r.pcode
LEFT JOIN (
SELECT COUNT(s.ID) AS sl_rows,s.pcode,s.date_created,SUM(s.soldQty) AS sold_stock
FROM tbl_sold_items s
WHERE s.date_created = '2019-10-03'
GROUP BY s.pcode
)AS s ON p.pcode = s.pcode
LEFT JOIN (
SELECT t.ID,t.pcode,t.date_created,t.remaining_qty AS opening_stock
FROM tbl_sold_items t
WHERE t.date_created > '2019-10-03'
GROUP BY t.pcode ORDER BY t.ID
)AS t ON p.pcode = t.pcode
The above code has solved my issue
SELECT
SUM(CASE
WHEN db1.card_type = 'debit' THEN 1
ELSE 0
END) AS DEBIT,
SUM(CASE
WHEN db1.card_type = 'credit' THEN 1
ELSE 0
END) AS CREDIT,
DATE_FORMAT(Latest, '%Y-%m') AS Dato
FROM
(SELECT
pp.debtor_id, pp.card_type, MAX(pp.created) AS Latest
FROM
capital.credit_card cc
JOIN capital.card_to_debtor ctd ON cc.id = ctd.card_id
AND debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
FROM
analysis.full_settlement fs
WHERE
((reason = 'BANK_PAYMENT'
OR fs.reason = 'CARD_PAYMENT')
AND fs.amount < 0)
OR fs.REASON = 'TRIVIAL_BALANCE'
OR fs.id = 13793327
OR fs.id = 7451808)
JOIN analysis.full_settlement fs ON fs.debtor_id = ctd.debtor_id
JOIN (SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) pp ON fs.debtor_id = pp.debtor_id
AND pp.created <= fs.created
GROUP BY pp.debtor_id , fs.created) db
JOIN
(SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) db1 ON db1.debtor_id = db.debtor_id
AND db1.created = db.Latest
GROUP BY YEAR(Latest) , MONTH(Latest)
debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
debtor_id is one column, so you can only compare it to one other column. Change it to
debtor_id IN (SELECT
fs.debtor_id FROM...
I wrote mysql query which use subquery. I can't understand why mysql shows me 'Unknown column 'T.TICKETID' in 'where clause'' in subquery. For MSSQL it works without problem.
SELECT #PERIODTYPE := 'Y';
SELECT
#PERIODTYPE,
CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END `Period`,
DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
COUNT(*) Transactions,
SUM(SQ.`Sold Units`) `Sold Units`,
FORMAT(MAX(P.TOTAL), 2) `Largest Order`,
FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
INNER JOIN TAXES TX ON TXL.TAXID = TX.ID
INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID
INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
INNER JOIN TICKETS T ON R.ID = T.ID
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET) SQ
GROUP BY CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END
ORDER BY R.DATENEW
Update 1
I replaced table name (TICKETS) instead of alias (T). I got error. Please, see the screenshot.
SELECT #PERIODTYPE := 'D';
SELECT
#PERIODTYPE,
CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END `Period`,
DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
COUNT(*) Transactions,
FORMAT(SUM(IFNULL(SQ.`Sold Units`,0)),2) `Sold Units`,
FORMAT(MAX(P.TOTAL), 2) `Largest Order`,
FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
INNER JOIN TAXES TX ON TXL.TAXID = TX.ID
INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID
INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
INNER JOIN TICKETS ON R.ID = TICKETS.ID
-- LEFT JOIN (SELECT TL.TICKET, SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL GROUP BY TL.TICKET) SQ ON T.ID = SQ.TICKET
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE TICKETS.ID = SQ.TICKET) SQ
GROUP BY CASE
WHEN #PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
WHEN #PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
WHEN #PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
END
ORDER BY R.DATENEW
The query don't work in mysql beacuse mysql can't use an alias declared ad an upper level respect the subquery ..
in your subquery in Cross Join
CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units'
FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET) SQ
You use T.TICKETID
The alias T
INNER JOIN TICKETS T ON R.ID = T.ID
Is declare in a place not "accessible" by the scope of the subquery ..
Then if is possible i suggest you of build a subquery that non refer to T alias .. Try (if is possible ) building an equivalent subquery wihout alias
I'm trying to combine 3 queries for building a search menu in opencart, but they always fail and error.
This is example query
First query:
SELECT p.product_id,
(SELECT AVG(rating) AS total
FROM review r1
WHERE r1.product_id = p.product_id
AND r1.status = '1'
GROUP BY r1.product_id) AS rating
FROM product p
LEFT JOIN product_description pd
ON (p.product_id = pd.product_id)
LEFT JOIN product_to_store p2s
ON (p.product_id = p2s.product_id)
WHERE pd.language_id = '1'
AND p.status = '1'
AND p.date_available <= NOW()
AND p2s.store_id = '0'
AND ( LCASE(pd.name) LIKE '%dellxps13%'
OR MATCH(pd.tag) AGAINST('dellxps13')
)
OR LCASE(p.model) = 'dellxps13'
OR LCASE(p.sku) = 'dellxps13'
OR LCASE(p.upc) = 'dellxps13'
OR LCASE(p.ean) = 'dellxps13'
OR LCASE(p.jan) = 'dellxps13'
OR LCASE(p.isbn) = 'dellxps13'
OR LCASE(p.mpn) = 'dellxps13'
GROUP BY p.product_id
ORDER BY p.sort_order ASC, LCASE(pd.name) ASC
LIMIT 0, 15
Second query:
SELECT REPLACE (model," ","") AS 'FIND'
FROM product
WHERE REPLACE (model," ","")
It's change to p.model(for innerjoin, p.model = product.model)
Third query:
SELECT REPLACE (name," ","") AS 'FIND2'
FROM product_description
WHERE REPLACE (name," ","")
It's change to pd.name(for innerjoin, pd.name = product_description.name).
So I want replace all first, then select from replace like '%dellxps13%'
Anyone know how to combine 3 this query?
This is what I try to change and got error
AND ((LCASE(SELECT REPLACE (pd.name," ","")) FROM product_description
WHERE LCASE (REPLACE (pd.name," ",""))) LIKE '%dellxps13%'
OR MATCH(pd.tag) AGAINST('dellxps13')
)
OR LCASE((SELECT REPLACE (p.model," ","")) FROM product WHERE LCASE (REPLACE (p.model," ",""))) = 'dellxps13'
error 1064 'SELECT REPLACE (pd.name," ","")) FROM product_description
WHERE LCASE (REPLAC' at line 16.
And when I not use LCASE got error
SELECT p.product_id,
(SELECT AVG(rating) AS total
FROM review r1
WHERE r1.product_id = p.product_id
AND r1.status = '1'
GROUP BY r1.product_id) AS rating
FROM product p
LEFT JOIN product_description pd
ON (p.product_id = pd.product_id)
LEFT JOIN product_to_store p2s
ON (p.product_id = p2s.product_id)
WHERE pd.language_id = '1'
AND p.status = '1'
AND p.date_available <= NOW()
AND p2s.store_id = '0'
AND ((SELECT REPLACE (pd.name," ","") FROM pd WHERE REPLACE (pd.name," ","") LIKE '%dellxps13%')
OR MATCH(pd.tag) AGAINST('dellxps13'))
OR (SELECT REPLACE (p.model," ","") FROM p WHERE REPLACE (p.model," ","") LIKE '%dellxps13%')
OR LCASE(p.sku) = 'dellxps13'
OR LCASE(p.upc) = 'dellxps13'
OR LCASE(p.ean) = 'dellxps13'
OR LCASE(p.jan) = 'dellxps13'
OR LCASE(p.isbn) = 'dellxps13'
OR LCASE(p.mpn) = 'dellxps13'
GROUP BY p.product_id
ORDER BY p.sort_order ASC, LCASE(pd.name) ASC
LIMIT 0, 15
error : 1146 - Table 'example.pd' doesn't exist
Hi I have run into a dilemma, I am doing this query:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, pr.promo_name, pr.add_value_text, c.contract_id, c.cmeal_plan, c.cmin_markup, c.civa, c.tax_include, c.hotel_id, hi.hname, hi.hstars, im.image_file, pl.plan_name, ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi ON hi.hotel_id = c.hotel_id AND hi.destination_id = '6460'
INNER JOIN images AS im ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl ON pl.plan_code = c.cmeal_plan AND pl.lang = '1'
INNER JOIN hotel_categories AS hc ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca ON ca.category_code = hc.category_code AND ca.lang = '1'
LEFT JOIN
(SELECT
r.hotel_id, AVG(r.double) AS price
FROM
rates AS r ) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr ON pr.hotel_id = hi.hotel_id AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 AND pr.book_start <= '2012-11-01' AND pr.book_end >= '2012-11-02' AND travel_start <= '2012-11-23' AND travel_end >= '2012-11-30' AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id
I am getting all the desired results except for the sub select query.. each hotel has a price but it is only giving me back one result and the rest are all null. Is there an error in my query? If any additional information is needed please let me know and thank you in advance for any help!
You are missing the GROUP BY in your subquery, so MySQL will only return one-value. If you want all hotel_id's then you need to GROUP BY that field:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories,
pr.promo_name,
pr.add_value_text,
c.contract_id,
c.cmeal_plan,
c.cmin_markup,
c.civa,
c.tax_include,
c.hotel_id,
hi.hname,
hi.hstars,
im.image_file,
pl.plan_name,
ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi
ON hi.hotel_id = c.hotel_id
AND hi.destination_id = '6460'
INNER JOIN images AS im
ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl
ON pl.plan_code = c.cmeal_plan
AND pl.lang = '1'
INNER JOIN hotel_categories AS hc
ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca
ON ca.category_code = hc.category_code
AND ca.lang = '1'
LEFT JOIN
(
SELECT r.hotel_id, AVG(r.double) AS price
FROM rates AS r
GROUP BY r.hotel_id <-- add a GROUP BY hotel_id then you will get avg() for each hotel
) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr
ON pr.hotel_id = hi.hotel_id
AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0
AND pr.book_start <= '2012-11-01'
AND pr.book_end >= '2012-11-02'
AND travel_start <= '2012-11-23'
AND travel_end >= '2012-11-30'
AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01'
AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id