SQL Price Scheduling, Select last timestamp thats <= UTC_TIMESTAMP() - mysql

I have a table called product_pricing.
In here, I can add multiple prices for each ID. What differentiates them is the product_pricing.timestamp_valid...it is what I use to schedule price changes in advance.
SELECT
`products`.`wo_id`,
`products`.`fty_id`,
`products`.`price` AS price1,
`product_attributes`.`fty_id`,
`product_attributes`.`cat_id`,
`product_attributes`.`design_id`,
`product_attributes`.`season_id`,
`products_u_ids`.`u_id`,
`products_u_ids`.`link_id`,
`product_designs`.`design_id`,
`product_designs`.`brand_id`,
COALESCE(`product_pricing`.`u_id`, NULL) AS price2_u_id,
COALESCE(`product_pricing`.`currency`, NULL) AS price2_currency,
COALESCE(`product_pricing`.`price`, NULL) AS price2,
COALESCE(`product_pricing`.`formula_id`, NULL) price2_formula_id,
COALESCE(`product_pricing`.`vat_calculated`) AS price2_vat_calculated,
COALESCE(`product_pricing`.`vat_id`, NULL) AS price2_vat_id,
COALESCE(`product_pricing`.`timestamp_valid`, NULL) price2_timestamp_valid,
COALESCE(`product_price_formulas`.`formula_id`, NULL) AS price2_formula_id,
COALESCE(`product_price_formulas`.`formula`, NULL) AS price2_formula,
COALESCE(`global_vat_tariffs`.`vat_id`, NULL) AS price2_vat_id,
COALESCE(`global_vat_tariffs`.`percentage`, NULL) AS price2_vat_tariff
FROM `products`
LEFT JOIN `product_attributes`
ON `products`.`fty_id` = `product_attributes`.`fty_id`
LEFT JOIN `products_u_ids`
ON `product_attributes`.`fty_id` = `products_u_ids`.`link_id`
LEFT JOIN `product_designs`
ON `product_attributes`.`design_id` = `product_designs`.`design_id`
LEFT JOIN `product_pricing`
ON `products_u_ids`.`u_id` = `product_pricing`.`u_id`
LEFT JOIN `product_price_formulas`
ON `product_pricing`.`formula_id` = `product_price_formulas`.`formula_id`
LEFT JOIN `global_vat_tariffs`
ON `product_pricing`.`vat_id` = `global_vat_tariffs`.`vat_id`
LEFT OUTER JOIN (
SELECT `product_pricing`.`u_id`, MAX(`timestamp_valid`) AS MaxDate
FROM `product_pricing`
WHERE `product_pricing`.`timestamp_valid` <= UTC_TIMESTAMP
GROUP BY `product_pricing`.`u_id`
) AS temp ON temp.u_id = `product_pricing`.`u_id` AND temp.MaxDate = `product_pricing`.`timestamp_valid`
WHERE `products`.`wo_id` IN ('028284', '018305', '031536')
The SQL I have above, returns all the rows for a given ID, instead of just returning product_pricing.timestamp_valid``<= UTC_TIMESTAMP().
There is a problem in the LEFT OUTER JOIN.
The other thing that this SQL does is, when there isn't anything set for price2, it shouldn't break the script, but just return NULL for price2. I am solving that by using COALESCE.
This is what I get:
This is what I should get:
Assuming the date of the query is 2018-5-7.
Any ideas how to solve this problem with the LEFT OUTER JOIN?

You're using a LEFT JOIN operation on the subquery containing the timestamp filter you mention. LEFT JOINs retain rows that don't match its ON condition. You might try an ordinary INNER join on that subquery; inner JOINs don't retain the rows matching the ON condition.

Related

this Query is taking 15 seconds

i added indexes as well but still it is taking 13 sec
I added compound index for all the columns that i've used here
SELECT carrierbil2_.IDENTITY AS col_0_0_,
carrier4_.CARRIER_NAME AS col_1_0_,
carrier4_.IDENTITY AS col_2_0_,
carrier4_.CARRIER_ID AS col_3_0_,
shipmentor0_.EXTERNAL_REFERENCE_ID AS col_4_0_,
invoicedet5_.INVOICE_NUMBER AS col_5_0_,`enter code here`
shipmentca1_.CARRIER_REFERENCE_NUMBER AS col_6_0_,
SUM(shipmentco9_.RATED_COST) AS col_7_0_,
SUM(shipmentco9_.COST) AS col_8_0_,
invoice6_.TOTAL_PAID_AMOUNT AS col_9_0_,
invoice6_.INVOICE_GENERATED_DATE AS col_10_0_,
shipmentor0_.ACTUAL_SHIP_DATE AS col_11_0_,
bolstatus15_.BOL_STATUS_ID AS col_12_0_,
shipmentlo10_.LOCATION_NAME AS col_13_0_,
country11_.COUNTRY_NAME AS col_14_0_,
postal14_.POSTAL_CODE AS col_15_0_,
state12_.STATE_NAME AS col_16_0_,
city13_.CITY_NAME AS col_17_0_,
shipmentlo16_.LOCATION_NAME AS col_18_0_,
country17_.COUNTRY_NAME AS col_19_0_,
postal20_.POSTAL_CODE AS col_20_0_,
state18_.STATE_NAME AS col_21_0_,
city19_.CITY_NAME AS col_22_0_,
shipmentor0_.IDENTITY AS col_23_0_,
shipmentca1_.IDENTITY AS col_24_0_,
shipmentno7_.NOTE AS col_25_0_
FROM
SHIPMENT_ORDER shipmentor0_
INNER JOIN
SHIPMENT_CARRIER shipmentca1_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentca1_.SHIPMENT_ORDER_ID
AND (shipmentca1_.IS_DELETED = 0)
LEFT OUTER JOIN
CARRIER_BILL_DETAILS carrierbil2_ ON shipmentca1_.SHIPMENT_CARRIER_ID = carrierbil2_.SHIPMENT_CARRIER_ID
LEFT OUTER JOIN
CARRIER_BILLS carrierbil3_ ON carrierbil2_.CARRIER_BILL_ID = carrierbil3_.CARRIER_BILL_ID
INNER JOIN
CARRIER carrier4_ ON shipmentca1_.CARRIER_ID = carrier4_.CARRIER_ID
LEFT OUTER JOIN
INVOICE_DETAILS invoicedet5_ ON shipmentor0_.SHIPMENT_ORDER_ID = invoicedet5_.SHIPMENT_ORDER_ID
LEFT OUTER JOIN
INVOICE invoice6_ ON invoicedet5_.INVOICE_ID = invoice6_.INVOICE_ID
LEFT OUTER JOIN
SHIPMENT_NOTES shipmentno7_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentno7_.SHIPMENT_ORDER_ID
AND (shipmentno7_.NOTE_TYPE = 4)
LEFT OUTER JOIN
SHIPMENT_COST shipmentco8_ ON shipmentor0_.SHIPMENT_ORDER_ID = shipmentco8_.SHIPMENT_ID
LEFT OUTER JOIN
SHIPMENT_COST_DETAILS shipmentco9_ ON shipmentco8_.SHIPMENT_COST_ID = shipmentco9_.SHIPMENT_COST_ID
AND (shipmentco9_.IS_DELETED = 0)
LEFT OUTER JOIN
SHIPMENT_LOCATION shipmentlo10_ ON shipmentor0_.ORIGIN_ID = shipmentlo10_.SHIPMENT_LOCATION_ID
AND (shipmentlo10_.LOCATION_TYPE_ID = 3)
LEFT OUTER JOIN
COUNTRY country11_ ON shipmentlo10_.COUNTRY_ID = country11_.COUNTRY_ID
LEFT OUTER JOIN
STATE state12_ ON shipmentlo10_.STATE_ID = state12_.STATE_ID
LEFT OUTER JOIN
CITY city13_ ON shipmentlo10_.CITY_ID = city13_.CITY_ID
LEFT OUTER JOIN
POSTAL postal14_ ON shipmentlo10_.POSTAL_ID = postal14_.POSTAL_ID
LEFT OUTER JOIN
BOL_STATUS bolstatus15_ ON shipmentor0_.ORDER_STATUS = bolstatus15_.BOL_STATUS_ID
LEFT OUTER JOIN
SHIPMENT_LOCATION shipmentlo16_ ON shipmentor0_.DESTINATION_LOCATION_ID = shipmentlo16_.SHIPMENT_LOCATION_ID
AND (shipmentlo16_.LOCATION_TYPE_ID = 4)
LEFT OUTER JOIN
COUNTRY country17_ ON shipmentlo16_.COUNTRY_ID = country17_.COUNTRY_ID
LEFT OUTER JOIN
STATE state18_ ON shipmentlo16_.STATE_ID = state18_.STATE_ID
LEFT OUTER JOIN
CITY city19_ ON shipmentlo16_.CITY_ID = city19_.CITY_ID
LEFT OUTER JOIN
POSTAL postal20_ ON shipmentlo16_.POSTAL_ID = postal20_.POSTAL_ID
CROSS JOIN
CLIENT client21_
WHERE
shipmentor0_.CLIENT_ID = client21_.CLIENT_ID
AND bolstatus15_.SEQUENCE_ID >= 700
AND (carrierbil3_.IS_APPROVED = 0
OR carrierbil3_.IS_APPROVED IS NULL)
AND (carrierbil3_.IS_DELETED = 0
OR carrierbil3_.IS_DELETED IS NULL)
AND (carrierbil2_.IS_DELETED = 0
OR carrierbil2_.IS_DELETED IS NULL)
AND (shipmentor0_.IS_DELETED = 0
OR shipmentor0_.IS_DELETED IS NULL)
GROUP BY invoice6_.INVOICE_GENERATED_DATE , shipmentca1_.IDENTITY , invoicedet5_.INVOICE_NUMBER , invoice6_.TOTAL_PAID_AMOUNT , shipmentca1_.CARRIER_REFERENCE_NUMBER , carrier4_.CARRIER_ID , CAST(carrier4_.IDENTITY AS SIGNED) , carrier4_.CARRIER_NAME , CAST(carrierbil2_.IDENTITY AS SIGNED) , shipmentor0_.SHIPMENT_ORDER_ID , shipmentno7_.NOTE , shipmentor0_.EXTERNAL_REFERENCE_ID , shipmentlo10_.LOCATION_NAME , country11_.COUNTRY_NAME , postal14_.POSTAL_CODE , state12_.STATE_NAME , city13_.CITY_NAME , shipmentlo16_.LOCATION_NAME , country17_.COUNTRY_NAME , postal20_.POSTAL_CODE , state18_.STATE_NAME , city19_.CITY_NAME , shipmentor0_.IDENTITY
ORDER BY shipmentor0_.SHIPMENT_ORDER_ID DESC;
The indexes are mostly useless because of OR, as in
AND (carrierbil3_.IS_APPROVED = 0
OR carrierbil3_.IS_APPROVED IS NULL)
The simple way to fix that is to pick either 0 or NULL to represent the flag. Then make sure all the data is consistent, and change the WHERE to just check for the one case.
Do you really mean
CROSS JOIN
CLIENT client21_
That is likely to be a performance-killer and generate a huge resultset.
Never mind. You have the ON in WHERE. Please use ON for relations and WHERE for filtering.
WHERE
shipmentor0_.CLIENT_ID = client21_.CLIENT_ID
I see a mixture of LEFT JOIN and JOIN. Check that the LEFT JOINs really need to be LEFT; that is, the 'right' table might have missing data.
To discuss further, please provide EXPLAIN SELECT ....
Eschew over-normalization:
You have 5 tables to describe a location (name, country, postal, state, city). Instead, I recommend a single table with those 5 columns. This, alone, would get rid of 8 JOINs.
CAST(carrier4_.IDENTITY AS SIGNED) -- Can't you fix the datatype to be SIGNED, or allow the value to be UNSIGNED?
But perhaps the main performance-killer is the "explode-implode" syndrone. First, it does a lot of JOINs, building a huge intermediate table, then it collapses that by doing GROUP BY. The remedy is
SELECT ...
FROM ( SELECT SUM(...), SUM(...) FROM ... GROUP BY ... ) AS a
JOIN ((whatever else is needed));
That is, first devise a minimal "derived table" that does the GROUP BY (and/or ORDER BY and/or LIMIT). Then see what else is needed to complete the query (namely all the normalization lookups).
After you have acted on most of my comments, we can discuss whether you have the optimal indexes. (It is premature to do so now.) If so, please start a new Question; it would be too much clutter to add to this one.
First of all, that's a lot of joins. However, the main reason your query is taking a significant time is because you're adding an order by clause. You need to figure out a way to avoid it, or may be come up with a different strategy

Select from joined table only if record exists

I have the following SQL query:
SELECT
Customers.CustomerName AS FullName,
Customers.Id AS CustomerId,
Customers.UserRoleId AS UserRoleId,
Customers.Email AS Email,
IFNULL(Customers.StudentId, '') AS CustomersStudentId,
IFNULL(Customers.MagentoId, '') AS MagentoId,
Sections.Id AS SectionId,
Sections.SectionNumber AS SectionNumber,
Sections.SectionName AS SectionName,
Courses.Id AS CourseId,
IFNULL(Courses.CourseName, '') AS CourseName,
IFNULL(Courses.CourseNumber,'') AS CourseNumber,
IFNULL(Courses.CourseDepartment, '') AS CourseDepartment,
IFNULL(Courses.Notes, '') AS CourseNotes,
IFNULL(Courses.Year, '') AS CourseYear,
IFNULL(Courses.CourseType, '') AS CourseType,
StudentsCourses.Id AS StudentsCoursesId,
IFNULL(StudentsCourses.StudentId, '') AS StudentsCoursesStudentId,
IFNULL(SiteProfile.StudentIdField, '') AS StudentIdField,
IFNULL(SiteProfile.SchoolEmailDomain, '') AS SchoolEmailDomain,
IFNULL(Orders.Id, '') AS OrderId
FROM Customers
LEFT JOIN StudentsCourses ON Customers.Id = StudentsCourses.CustomerId
LEFT JOIN Sections ON StudentsCourses.SectionId = Sections.Id
LEFT JOIN Courses ON StudentsCourses.CourseId = Courses.Id
LEFT JOIN BooksCourses ON Courses.Id = BooksCourses.CourseId
LEFT JOIN Products ON BooksCourses.ISBN = Products.ISBN
LEFT JOIN EbookVendors ON Products.EbookVendorId = EbookVendors.Id
LEFT JOIN Orders ON Customers.Id = Orders.CustomerId
LEFT JOIN SiteProfile ON Courses.SchoolCode = SiteProfile.SchoolCode
WHERE Customers.Id <> 10
AND StudentsCourses.SectionId IS NOT NULL
AND StudentsCourses.Delete <> 2
AND Courses.SchoolCode = '{$criteria["school_code"]}'
AND Courses.Year = {$criteria["year"]}
AND Courses.CourseType LIKE '{$criteria["term"]}'
Records will always exist in the Customers table. But sometimes there will be no associated records in any of the other joined tables.
How do I modify the query so that the additional SELECT and WHERE clauses don't break the results when there are only records in the Customers table?
EDIT:
When the record only exists in Customers, I want that record and I want the WHERE clauses that don't pertain to the Customers table to be ignored.
If the record exists in a joined table, I want the WHERE clause that pertains to that joined table to work.
You need to change the where statement to deal with nulls. Like this
WHERE Customers.Id <> 10
-- AND StudentsCourses.SectionId IS NOT NULL
AND COALESCE(StudentsCourses.Delete,0) <> 2
AND COALESCE(Courses.SchoolCode,'{$criteria["school_code"]}') = '{$criteria["school_code"]}'
AND COALESCE(Courses.Year,{$criteria["year"]}) = {$criteria["year"]}
AND (Courses.CourseType is null or Courses.CourseType LIKE '{$criteria["term"]}')
When you left join and the value does not exist you will have null for those items -- to still see the row you need to not have your where statement filter out those items.
There is another way to do it which is to put the criteria in the joins. So for example course type would look like this:
LEFT JOIN Courses ON StudentsCourses.CourseId = Courses.Id and Courses.CourseType LIKE '{$criteria["term"]}'
If you do this then you don't need to add the filter to the where -- it will only be applied to the join and will return null for the table columns if the join does not exist.
When you left join, you're going to get NULLs in the fields for which there is no corresponding "right" record, so you have to account for that:
WHERE Customers.Id <> 10
-- AND StudentsCourses.SectionId IS NOT NULL
AND (StudentsCourses.Delete <> 2 OR StudentsCourses.Delete IS NULL)
AND (Courses.SchoolCode = '{$criteria["school_code"]}' OR Courses.SchoolCode IS NULL)
AND ( Courses.Year = {$criteria["year"]} OR Courses.Year IS NULL)
AND (Courses.CourseType LIKE '{$criteria["term"]}' OR Courses.CourseType IS NULL)
Both wrong. You can't have a query that returns two different shapes of tuples: these columns from here if this exists, but those columns from both here and there if hat exists. One query, one shape.
That having been sternly said, let's relax a little bit.
Just do an outer join, and if the data to be joined doesn't exist (= can't be found), NULL values will be silently, painlessly filled into the indicated columns. "Populated" is a fancier word for that.

MySQL join only on row existing

I have the following query
SELECT custconcompany, custconfirstname, custconlastname, custconemail, custconphone, shipaddress1, shipaddress2, shipcity, stateabbrv, shipzip, countryname, websitecheck.formfieldfieldvalue websitevalue, excludecheck.formfieldfieldvalue excludevalue
FROM obcisc_customers
JOIN ( (obcisc_shipping_addresses JOIN obcisc_countries
ON obcisc_shipping_addresses.shipcountryid = obcisc_countries.countryid)
LEFT JOIN obcisc_country_states
ON obcisc_shipping_addresses.shipstateid = obcisc_country_states.stateid
LEFT JOIN obcisc_formfieldsessions websitecheck
ON obcisc_shipping_addresses.shipformsessionid = websitecheck.formfieldsessioniformsessionid
LEFT JOIN obcisc_formfieldsessions excludecheck
ON obcisc_shipping_addresses.shipformsessionid = excludecheck.formfieldsessioniformsessionid)
ON obcisc_customers.customerid = obcisc_shipping_addresses.shipcustomerid
WHERE custgroupid = 11
AND websitecheck.formfieldfieldid = 24
AND excludecheck.formfieldfieldid = 30
AND excludecheck.formfieldfieldvalue != 'a:1:{i:0;s:3:"Yes";}'
ORDER BY shipstate, shipcity
This works great except I also need it to return rows where "excludecheck.formfieldfieldid=30" does not exist... right now it's not returning them
When writing a LEFT JOIN, any criteria on the table you're joining with should be put into the ON clause. If you put it into the WHERE clause, you'll filter out the results in the first table that don't have a matching row in the second table, because the NULL value that comes from the outer join will not match the criteria.
SELECT custconcompany, custconfirstname, custconlastname, custconemail, custconphone, shipaddress1, shipaddress2, shipcity, stateabbrv, shipzip, countryname, websitecheck.formfieldfieldvalue websitevalue, excludecheck.formfieldfieldvalue excludevalue
FROM obcisc_customers
JOIN obcisc_shipping_addresses
ON obcisc_customers.customerid = obcisc_shipping_addresses.shipcustomerid
JOIN obcisc_countries
ON obcisc_shipping_addresses.shipcountryid = obcisc_countries.countryid)
LEFT JOIN obcisc_country_states
ON obcisc_shipping_addresses.shipstateid = obcisc_country_states.stateid
LEFT JOIN obcisc_formfieldsessions websitecheck
ON obcisc_shipping_addresses.shipformsessionid = websitecheck.formfieldsessioniformsessionid
AND websitecheck.formfieldfieldid = 24
LEFT JOIN obcisc_formfieldsessions excludecheck
ON obcisc_shipping_addresses.shipformsessionid = excludecheck.formfieldsessioniformsessionid
AND excludecheck.formfieldfieldid = 30
AND excludecheck.formfieldfieldvalue != 'a:1:{i:0;s:3:"Yes";}')
WHERE custgroupid = 11
ORDER BY shipstate, shipcity
Another way to do it is by putting (excludecheck.formfieldfieldid = 30 OR excludecheck.formfieldfieldid IS NULL) in the WHERE clause. But this is more verbose and also I believe it's harder for MySQL to optimize, especially if you have several tables you're joining like this.

How can I simplify this hideous query?

I've gotten to the point with this query where it's become too unwieldy to modify and I'm sure it's also become rather inefficient.
I have a table of items which may have any variable number of properties so these properties are listed in small tables ... item id and property id usually make up the index, and the property tables will have additional information. In essence they're used for filtering a huge number of items to at least the relevant subset.
SELECT * FROM item
INNER JOIN itemwearTable
ON (id=itemwearTable.itemID AND itemwearTable.wearlocID=1)
WHERE item.id IN (
SELECT id FROM item
LEFT JOIN itemalignTable
ON item.id=itemalignTable.itemID
WHERE
itemalignTable.itemID IS NULL OR
(itemalignTable.itemID=item.id AND itemalignTable.alignID=1)
)
AND
item.id IN (
SELECT id FROM item
LEFT JOIN itemgenderTable
ON itemgenderTable.itemID=item.id
WHERE
itemgenderTable.itemID IS NULL OR
(itemgenderTable.itemID=item.id AND itemgenderTable.genderID=1)
)
AND
(item.id IN (
SELECT id FROM item
LEFT JOIN itemgenreTable
ON item.id=itemgenreTable.itemid
WHERE
itemgenreTable.itemid IS NULL OR
(itemgenreTable.itemID=item.id AND itemgenreTable.genreID=1)
)
OR
item.id IN (
SELECT id from item
LEFT JOIN itemclassTable
ON item.id=itemclassTable.itemid
WHERE
itemclassTable.itemid IS NULL OR
(itemclassTable.itemID=item.id AND itemclassTable.classID=1)
))
AND
item.id IN (
SELECT id from item
LEFT JOIN itemlocTable ON
item.id=itemlocTable.itemid
WHERE
itemlocTable.itemid IS NULL OR
(itemlocTable.itemID=item.id AND itemlocTable.locID=1)
)
AND
item.minlvl <= 50
ORDER BY item.sdesc
Note I've used =1 here in many places to replace the php variables that would normally be part of the script generating the query. The problem is that while these items are used to narrow the results, I have another table itemaffTable that has additional data (amount for example) which matches on item.id=itemaffTable.itemID but I can't seem to see how to get that inserted without seriously messing up what's already there.
Any help is appreciated.
I think this should do it:
SELECT DISTINCT i.*, iwt.* FROM item i
INNER JOIN itemwearTable iwt ON i.id=iwt.itemID
LEFT JOIN itemalignTable iat ON i.id = iat.itemID
LEFT JOIN itemgenderTable igt ON i.id = igt.itemID
LEFT JOIN itemgenreTable igrt ON i.id = igrt.itemID
LEFT JOIN itemclassTable ict ON i.id = ict.itemID
LEFT JOIN itemlocTable ilt ON i.id = ilt.itemID
WHERE (iat.itemID IS NULL OR iat.alignID = 1)
AND (igt.itemID IS NULL OR igt.genderID = 1)
AND (igrt.itemID IS NULL OR igrt.genreID = 1)
AND (ict.itemID IS NULL OR ict.classID = 1)
AND (ilt.itemID IS NULL OR ilt.locID = 1)
AND i.minlvl <= 50
AND iwt.wearlocID=1
ORDER BY i.sdesc
Something like this should work.
SELECT item.*, itemwearTable.* FROM item
INNER JOIN itemwearTable
ON (id=itemwearTable.itemID AND itemwearTable.wearlocID=1)
LEFT JOIN itemalignTable
ON item.id=itemalignTable.itemID
LEFT JOIN itemgenderTable
ON itemgenderTable.itemID=item.id
LEFT JOIN itemgenreTable
ON item.id=itemgenreTable.itemid
LEFT JOIN itemclassTable
ON item.id=itemclassTable.itemid
LEFT JOIN itemlocTable
ON item.id=itemlocTable.itemid
WHERE (itemalignTable.itemID IS NULL OR itemalignTable.alignID=1)
AND (itemgenderTable.itemID IS NULL OR itemgenderTable.genderID=1)
AND (itemlocTable.itemid IS NULL OR itemlocTable.locID=1)
AND ((itemgenreTable.itemid IS NULL OR itemgenreTable.genreID=1)
OR (itemclassTable.itemid IS NULL OR itemclassTable.classID=1))
AND item.minlvl <= 50
ORDER BY item.sdesc

Replace Default Null Values Returned From Left Outer Join

I have a Microsoft SQL Server 2008 query that returns data from three tables using a left outer join. Many times, there is no data in the second and third tables and so I get a null which I think is the default for left outer join. Is there a way to replace the default values in the select statement? I have a workaround in that I can select into a table variable but it feels a little dirty.
SELECT iar.Description, iai.Quantity, iai.Quantity * rpl.RegularPrice as 'Retail',
iar.Compliance FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
I would like the Quantity and RegularPrice to default to zero if possible.
That's as easy as
IsNull(FieldName, 0)
Or more completely:
SELECT iar.Description,
ISNULL(iai.Quantity,0) as Quantity,
ISNULL(iai.Quantity * rpl.RegularPrice,0) as 'Retail',
iar.Compliance
FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
In case of MySQL or SQLite the correct keyword is IFNULL (not ISNULL).
SELECT iar.Description,
IFNULL(iai.Quantity,0) as Quantity,
IFNULL(iai.Quantity * rpl.RegularPrice,0) as 'Retail',
iar.Compliance
FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
MySQL
COALESCE(field, 'default')
For example:
SELECT
t.id,
COALESCE(d.field, 'default')
FROM
test t
LEFT JOIN
detail d ON t.id = d.item
Also, you can use multiple columns to check their NULL by COALESCE function.
For example:
mysql> SELECT COALESCE(NULL, 1, NULL);
-> 1
mysql> SELECT COALESCE(0, 1, NULL);
-> 0
mysql> SELECT COALESCE(NULL, NULL, NULL);
-> NULL
For Oracle you can use:
NVL(columnName,deafultValue) :- NVL is used to convert a null value to a default value in the query output. eg. If you want to replace null values with 'NA' then use something like this.
SELECT NVL(columnName,'NA') FROM tableName