How to calculate Inventory in Maximo Query - db2

I'm having a hard time calculating or getting the right values for the inventory in maximo.
Required fields are:
Item
Description
Default Bin
Issue Unit
Current Balance
Hard Reserved Quantity Not Staged
Hard Reserved Quantity Shipped
Total Quantity Shipped
Expired Quantity in Stock
Quantity Available
Quantity Currently Reserved
Hard Reserved Quantity
Soft Reserved Quantity
Quantity Staged Quantity in Holding Location
Missing fields:
Hard Reserved Quantity Shipped
Total Quantity Shipped
Expired Quantity in Stock
Quantity Available
Quantity Currently Reserved
Hard Reserved Quantity
Soft Reserved Quantity
Quantity Staged Quantity in Holding Location
here is my partial query:
select
inventory.orgid,
inventory.siteid,
inventory.location,
inventory.itemnum,
item.description,
invbalances.binnum
(select sum(invbalances.curbal) from invbalances),
(Sum (INVRESERVE.RESERVEDQTY)) - Sum (INVRESERVE.STAGEDQTY) as HardReservedQuantityNotStaged,
Sum (INVRESERVE.SHIPPEDQTY) as HardReservedQuantityShipped
from inventory
inner join ITEM on item.itemnum = inventory.itemnum
left outer join INVBALANCES on item.itemnum = invbalances.itemnum
left outer join INVRESERVE on item.itemnum = invreserve.itemnum
left outer join ASSET on item.itemnum = asset.itemnum
where inventory.itemnum = '11453'
group by inventory.orgid, inventory.siteid, inventory.location,inventory.itemnum, item.description, invbalances.binnum
Can anyone help me how to get the values of the missing fields?

Below query worked for me and able to fetch the correct records.
Please check and let me know if it works for you.
SELECT
item.itemnum item,
item.description description,
inventory.binnum default_bin,
inventory.issueunit issue_unit,
(
SELECT
nvl(SUM(ib.curbal),0)
FROM
invbalances ib
WHERE
ib.itemnum = inventory.itemnum
AND ib.location = inventory.location
AND ib.itemsetid = inventory.itemsetid
AND ib.siteid = inventory.siteid
) current_balance,
( (
SELECT
nvl(SUM(reservedqty),0)
FROM
invreserve
WHERE
inventory.itemnum = invreserve.itemnum
AND inventory.location = invreserve.location
AND inventory.itemsetid = invreserve.itemsetid
AND inventory.siteid = invreserve.storelocsiteid
AND invreserve.restype IN (
'APHARD',
'HARD'
)
) - (
SELECT
SUM(invuseline.quantity)
FROM
invuseline
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
JOIN invuse ON invuseline.invusenum = invuse.invusenum
AND invuse.siteid = invuseline.siteid
AND invuse.status IN (
'SHIPPED',
'STAGED'
)
WHERE
inventory.itemnum = invuseline.itemnum
AND inventory.location = invuseline.fromstoreloc
AND inventory.siteid = invuseline.siteid
AND inventory.itemsetid = invuseline.itemsetid
) + (
SELECT
nvl(SUM(matrectrans.quantity),0)
FROM
matrectrans
JOIN invuse ON invuse.invuseid = matrectrans.invuseid
AND invuse.receipts = 'PARTIAL'
JOIN invuseline ON invuseline.invuselineid = matrectrans.invuselineid
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
WHERE
inventory.itemnum = matrectrans.itemnum
AND inventory.location = matrectrans.fromstoreloc
AND inventory.siteid = matrectrans.siteid
AND inventory.itemsetid = matrectrans.itemsetid
AND matrectrans.status = 'COMP'
) ) hard_reserved_qty_not_staged,
( (
SELECT
nvl(SUM(invuseline.quantity),0)
FROM
invuseline
JOIN invuse ON invuseline.invusenum = invuse.invusenum
AND invuse.siteid = invuseline.siteid
AND invuse.status IN (
'SHIPPED',
'COMPLETE'
)
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
WHERE
inventory.itemnum = invuseline.itemnum
AND inventory.location = invuseline.fromstoreloc
AND inventory.siteid = invuseline.siteid
AND inventory.itemsetid = invuseline.itemsetid
) - (
SELECT
nvl(SUM(matrectrans.quantity),0)
FROM
matrectrans
JOIN invuse ON invuse.invuseid = matrectrans.invuseid
AND invuse.receipts = 'PARTIAL'
JOIN invuseline ON invuseline.invuselineid = matrectrans.invuselineid
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
WHERE
inventory.itemnum = matrectrans.itemnum
AND inventory.location = matrectrans.fromstoreloc
AND inventory.siteid = matrectrans.siteid
AND inventory.itemsetid = matrectrans.itemsetid
AND matrectrans.status = 'COMP'
) ) hard_reserved_qty_shipped,
(
SELECT
nvl(SUM(invuseline.quantity),0)
FROM
invuseline
JOIN invuse ON invuseline.invusenum = invuse.invusenum
AND invuse.siteid = invuseline.siteid
AND invuse.status = 'SHIPPED'
WHERE
inventory.itemnum = invuseline.itemnum
AND inventory.location = invuseline.fromstoreloc
AND inventory.siteid = invuseline.siteid
AND inventory.itemsetid = invuseline.itemsetid
) - (
SELECT
nvl(SUM(quantity),0)
FROM
matrectrans
WHERE
inventory.itemnum = matrectrans.itemnum
AND inventory.location = matrectrans.fromstoreloc
AND inventory.siteid = matrectrans.siteid
AND inventory.itemsetid = matrectrans.itemsetid
AND status = 'COMP'
) total_quantity_shipped,
(
SELECT
nvl(SUM(ib.curbal),0)
FROM
invbalances ib
WHERE
ib.itemnum = inventory.itemnum
AND ib.location = inventory.location
AND ib.itemsetid = inventory.itemsetid
AND ib.siteid = inventory.siteid
) - ( (
SELECT
nvl(SUM(reservedqty),0)
FROM
invreserve
WHERE
inventory.itemnum = invreserve.itemnum
AND inventory.location = invreserve.location
AND inventory.itemsetid = invreserve.itemsetid
AND inventory.siteid = invreserve.storelocsiteid
AND invreserve.restype IN (
'APHARD',
'HARD'
)
) - ( (
SELECT
nvl(SUM(invuseline.quantity),0)
FROM
invuseline
JOIN invuse ON invuseline.invusenum = invuse.invusenum
AND invuse.siteid = invuseline.siteid
AND invuse.status IN (
'STAGED',
'SHIPPED'
)
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
WHERE
inventory.itemnum = invuseline.itemnum
AND inventory.location = invuseline.fromstoreloc
AND inventory.siteid = invuseline.siteid
AND inventory.itemsetid = invuseline.itemsetid
) ) + (
SELECT
nvl(SUM(matrectrans.quantity),0)
FROM
matrectrans
JOIN invuse ON invuse.invuseid = matrectrans.invuseid
AND invuse.receipts = 'PARTIAL'
JOIN invuseline ON invuseline.invuselineid = matrectrans.invuselineid
JOIN invreserve ON invreserve.requestnum = invuseline.requestnum
AND invreserve.restype IN (
'APHARD',
'HARD'
)
WHERE
inventory.itemnum = matrectrans.itemnum
AND inventory.location = matrectrans.fromstoreloc
AND inventory.siteid = matrectrans.siteid
AND inventory.itemsetid = matrectrans.itemsetid
AND matrectrans.status = 'COMP'
) ) quantity_available,
(
SELECT
nvl(SUM(reservedqty),0)
FROM
invreserve
WHERE
inventory.itemnum = invreserve.itemnum
AND inventory.location = invreserve.location
AND inventory.itemsetid = invreserve.itemsetid
AND inventory.siteid = invreserve.storelocsiteid
) quantity_currently_reserved,
(
SELECT
nvl(SUM(reservedqty),0)
FROM
invreserve
WHERE
inventory.itemnum = invreserve.itemnum
AND inventory.location = invreserve.location
AND inventory.itemsetid = invreserve.itemsetid
AND inventory.siteid = invreserve.storelocsiteid
AND invreserve.restype IN (
'APHARD',
'HARD'
)
) hard_reserved_quantity,
(
SELECT
nvl(SUM(reservedqty),0)
FROM
invreserve
WHERE
inventory.itemnum = invreserve.itemnum
AND inventory.location = invreserve.location
AND inventory.itemsetid = invreserve.itemsetid
AND inventory.siteid = invreserve.storelocsiteid
AND invreserve.restype IN (
'APSOFT',
'SOFT'
)
) soft_reserved_quantity,
(
SELECT
nvl(SUM(invuseline.quantity),0)
FROM
invuseline
JOIN invuse ON invuseline.invusenum = invuse.invusenum
AND invuse.siteid = invuseline.siteid
AND invuse.status = 'STAGED'
WHERE
inventory.itemnum = invuseline.itemnum
AND inventory.location = invuseline.fromstoreloc
AND inventory.siteid = invuseline.siteid
AND inventory.itemsetid = invuseline.itemsetid
) quantity_staged
FROM
inventory
JOIN item ON item.itemsetid = inventory.itemsetid
AND item.itemnum = inventory.itemnum

Related

how to shorten or optimize JOIN query in postgres

I got following query, in which I am joining 4 times. But it takes lot of time to return the result.
Is there any way to optimize this query ?
SELECT T1.wbd_date as WBD_DATE, to_char(T1.wbd_block_no, 'fm00') as BLOCK_NO, T2.wbd_value as AmbHum, T3.wbd_value as AmbTemp,
T4.wbd_value as SWT, T5.wbd_value as Expval
FROM (
SELECT wbd_attribute_id, wbd_date, wbd_block_no, wbd_value
FROM wb_block_data ) T1
JOIN wb_block_data T2 on T1.wbd_date = T2.wbd_date AND T1.wbd_block_no = T2.wbd_block_no and T2.wbd_attribute_id = 152112 and to_char(T2.wbd_date,'MM YYYY')='07 2019'
JOIN wb_block_data T3 on T1.wbd_date = T3.wbd_date AND T1.wbd_block_no = T3.wbd_block_no and T3.wbd_attribute_id = 152116 and to_char(T3.wbd_date,'MM YYYY')='07 2019'
JOIN wb_block_data T4 on T1.wbd_date = T4.wbd_date AND T1.wbd_block_no = T4.wbd_block_no and T4.wbd_attribute_id = 152120 and to_char(T4.wbd_date,'MM YYYY')='07 2019'
JOIN wb_block_data T5 on T1.wbd_date = T5.wbd_date AND T1.wbd_block_no = T5.wbd_block_no and T5.wbd_attribute_id = 150661 and to_char(T5.wbd_date,'MM YYYY')='07 2019'
You are essentially doing a pivot, doing that using conditional aggregation might be faster:
select wbd_attribute_id, block_no, wbd_date,
max(wbd_value) filter (where wbd_attribute_id = 152112) as AmbHum,
max(wbd_value) filter (where wbd_attribute_id = 152116) as AmbTemp,
max(wbd_value) filter (where wbd_attribute_id = 152120) as swt,
max(wbd_value) filter (where wbd_attribute_id = 152120) as Expval
from (
SELECT wbd_attribute_id, wbd_date, to_char(wbd_block_no, 'fm00') as block_no, wbd_value
FROM wb_block_data
where wbd_attribute_id in (152112, 152116, 152120, 150661)
and wbd_date >= DATE '2019-07-01'
and wbd_date < DATE '2019-08-01'
) t
group by wbd_attribute_id, wbd_date, block_no
You want an index on (wbd_attribute_id, wbd_date) for that.
An index such as the following might work:
CREATE INDEX idx ON wb_block_data (wbd_date, wbd_block_no, wbd_attribute_id);
However, we would also need to make the join conditions sargable:
SELECT T1.wbd_date as WBD_DATE, to_char(T1.wbd_block_no, 'fm00') as BLOCK_NO, T2.wbd_value as AmbHum, T3.wbd_value as AmbTemp, T4.wbd_value as SWT, T5.wbd_value as Expval
FROM
(
SELECT wbd_attribute_id, wbd_date, wbd_block_no, wbd_value
FROM wb_block_data
) T1
JOIN wb_block_data T2 on T1.wbd_date = T2.wbd_date AND T1.wbd_block_no = T2.wbd_block_no and T2.wbd_attribute_id = 152112 and T2.wbd_date >= '2019-07-01' and T2.wbd_date < '2019-08-01'
JOIN wb_block_data T3 on T1.wbd_date = T3.wbd_date AND T1.wbd_block_no = T3.wbd_block_no and T3.wbd_attribute_id = 152116 and T3.wbd_date >= '2019-07-01' and T3.wbd_date < '2019-08-01'
JOIN wb_block_data T4 on T1.wbd_date = T4.wbd_date AND T1.wbd_block_no = T4.wbd_block_no and T4.wbd_attribute_id = 152120 and T4.wbd_date >= '2019-07-01' and T4.wbd_date < '2019-08-01'
JOIN wb_block_data T5 on T1.wbd_date = T5.wbd_date AND T1.wbd_block_no = T5.wbd_block_no and T5.wbd_attribute_id = 150661 and T5.wbd_date >= '2019-07-01' and T5.wbd_date < '2019-08-01';
The only change I made to your query was to express the date requirement as a range, without using to_char:
wbd_date >= '2019-07-01' and wbd_date < '2019-08-01'
This is sargable, meaning that Postgres might able to use an index on the wbd_date column to satisfy the July, 2019 date range.
I changed query to this and with this I am getting desired result quickly
SELECT T1.wbd_date as WBD_DATE, to_char(T1.wbd_block_no, 'fm00') as BLOCK_NO, T2.wbd_value as AmbHum, T3.wbd_value as AmbTemp,
T4.wbd_value as SWT, T5.wbd_value as Expval
FROM (
SELECT wbd_attribute_id, wbd_date, wbd_block_no, wbd_value
FROM wb_block_data where to_char(wbd_date,'MM YYYY')='07 2019') T1
JOIN wb_block_data T2 on T1.wbd_date = T2.wbd_date AND T1.wbd_block_no = T2.wbd_block_no and T2.wbd_attribute_id = 152112
JOIN wb_block_data T3 on T1.wbd_date = T3.wbd_date AND T1.wbd_block_no = T3.wbd_block_no and T3.wbd_attribute_id = 152116
JOIN wb_block_data T4 on T1.wbd_date = T4.wbd_date AND T1.wbd_block_no = T4.wbd_block_no and T4.wbd_attribute_id = 152120
JOIN wb_block_data T5 on T1.wbd_date = T5.wbd_date AND T1.wbd_block_no = T5.wbd_block_no and T5.wbd_attribute_id = 150661

magento 2.3 when Product export gets "Message is added to queue, wait to get your file soon"

I got error when export product
Im a bit late, and glad you got your issue sorted.
I Found this post a while back and had other problems wit magento's export, so posting my solution here for other readers. I got thrustrated with Magento's silly export (its worse if you have 20k+ products with 100's of attributes (it completely breaks)).
So I created a small standalone php script for my own exports. Anyone is welcome to use it, but will have to edit it to fit their need's (and db). But will be a massive time (and headache) saver if they a massive store
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$connect = mysqli_connect("your","db","details","here") OR die ('Could not connect to MySQL');
$brandlist = "SELECT `value` FROM `eav_attribute_option_value` LEFT JOIN `eav_attribute_option` ON `eav_attribute_option`.`option_id` = `eav_attribute_option_value` . `option_id` WHERE `eav_attribute_option_value`.`store_id` = 0 AND `eav_attribute_option`.`attribute_id` = 165 ORDER BY `value` ASC";
if ($_SERVER["REQUEST_METHOD"] != "POST") {
?>
<form method="post" action="<?php /* #escapeNotVerified */ ?>" enctype="multipart/form-data">
Brand: <select name="brand" id="brand">
<?php
foreach (($connect->query($brandlist)) as $row){
echo '<option value="' . $row['value'] . '">' . $row['value'] . '</option>';
}
?>
</select>
<input class="button" type="submit" name="submit" value="Submit">
</form>
<?php
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$brand = $_POST["brand"];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($connect, "SELECT
e.sku,
v1.value AS 'name',
v3.value AS 'product_online',
d1.value AS 'price',
v7.value AS 'url_key',
v10.value AS 'special_price',
v8.`value` AS 'special_to',
v9.value AS 'special_from'
FROM catalog_product_entity e
LEFT JOIN catalog_product_entity_varchar v1 ON e.entity_id = v1.entity_id
AND v1.store_id = 0
AND v1.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'name'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN catalog_product_entity_decimal d1 ON e.entity_id = d1.entity_id
AND d1.store_id = 0
AND d1.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'price'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN catalog_product_entity_int v3 ON e.entity_id = v3.entity_id
AND v3.store_id = 0
AND v3.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'status'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN catalog_product_entity_int v4 ON e.entity_id = v4.entity_id
AND v4.store_id = 0
AND v4.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'manufacturer'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN eav_attribute_option_value v5 ON v4.value = v5.option_id
AND v5.store_id = 0
AND v5.option_id = v4.value
LEFT JOIN catalog_product_entity v6 ON e.entity_id = v6.entity_id
LEFT JOIN catalog_product_entity_varchar v7 ON e.entity_id = v7.entity_id AND v7.store_id = 0 AND v7.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'url_key'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_product'))
LEFT JOIN
(SELECT product_id,
GROUP_CONCAT(c.category_id SEPARATOR ',') AS category_ids,
GROUP_CONCAT(cv.value SEPARATOR ',') AS category_names
FROM catalog_category_product c
INNER JOIN catalog_category_entity_varchar cv ON c.category_id = cv.entity_id
AND cv.store_id = 0
AND cv.attribute_id =
(SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'name'
AND entity_type_id =
(SELECT entity_type_id
FROM eav_entity_type
WHERE entity_type_code = 'catalog_category'))
GROUP BY product_id) cids ON e.entity_id = cids.product_id
LEFT JOIN catalog_product_entity_datetime v8 ON e.entity_id = v8.entity_id AND v8.attribute_id = 159 AND (DATE_FORMAT(v8.`value`, '%Y-%m-%d') >= CURDATE())
LEFT JOIN catalog_product_entity_datetime v9 ON e.entity_id = v9.entity_id AND v9.attribute_id = 157 AND (DATE_FORMAT(v9.`value`, '%Y-%m-%d') <= CURDATE()) AND (DATE_FORMAT(v8.`value`, '%Y-%m-%d') >= CURDATE())
LEFT JOIN catalog_product_entity_decimal v10 ON e.entity_id = v10.entity_id AND v10.attribute_id = 155 AND v9.attribute_id = 157 AND (DATE_FORMAT(v9.`value`, '%Y-%m-%d') <= CURDATE()) AND (DATE_FORMAT(v8.`value`, '%Y-%m-%d') >= CURDATE())
WHERE v3.value = 1
AND v5.value = '$brand'
AND NOT v6.attribute_set_id = 39
AND NOT v1.value LIKE '%(EOL)%' AND NOT v1.value LIKE '%Discontinued%' AND NOT v1.value LIKE 'Product Not For Sale' AND NOT d1.value = 0 AND NOT v6.attribute_set_id = 37;");
if ($connect->query($result) === TRUE) {
echo "db connect pass";}
else {
echo $connect->error;
}
function setExcelContentType() {
if(headers_sent())
return false;
header('Content-type: application/vnd.ms-excel');
return true;
}
function setDownloadAsHeader($filename) {
if(headers_sent())
return false;
header('Content-disposition: attachment; filename=' . $filename);
return true;
}
function csvFromResult($stream, $result, $showColumnHeaders = true) {
if($showColumnHeaders) {
$columnHeaders = array();
$nfields = mysqli_num_fields($result);
for($i = 0; $i < $nfields; $i++) {
$field = mysqli_fetch_field($result);
$columnHeaders[] = $field->name;
}
fputcsv($stream, $columnHeaders);
}
$nrows = 0;
while($row = mysqli_fetch_row($result)) {
fputcsv($stream, $row);
$nrows++;
}
return $nrows;
}
function csvFileFromResult($filename, $result, $showColumnHeaders = true) {
$fp = fopen($filename, 'w');
$rc = csvFromResult($fp, $result, $showColumnHeaders);
fclose($fp);
return $rc;
}
function csvToExcelDownloadFromResult($result, $showColumnHeaders = true, $asFilename = 'product-export.csv') {
$asFilename = 'product-export-' . date('m-d-Y-His') . '.csv';
setExcelContentType();
setDownloadAsHeader($asFilename);
return csvFileFromResult('php://output', $result, $showColumnHeaders);
}
csvToExcelDownloadFromResult($result);
}
?>
maybe its because your crons not run then you need to run: bin/magento cron:run
If not work with that line then try manually with bin/magento queue:consumers:start exportProcessor
Good luck

Writing Syntax Case When

I need help from you guys regarding my task.
1.
SELECT * FROM DBMGR.RTREDC00
WHERE EDC_PARM = '9940-RESOE'
AND EDC_LOT = 'H28BRAD'`
2.
SELECT LSPEC.GET_LSPEC_MIN((SELECT RL_PO FROM DBMGR.RTRLOT01 WHERE
RL_FACILITY = 'MKLPROD' AND RL_LOT = 'H28BRAD'),'9940-RES','M') MIN_RAS,
LSPEC.GET_LSPEC_MAX((SELECT RL_PO FROM DBMGR.RTRLOT01 WHERE RL_FACILITY
= 'MKLPROD' AND RL_LOT = 'H28BRAD'),'9940-RES','M') MAX_RAS
FROM DUAL
I need to create syntax using CASE WHEN where edc_value between MIN_RAS and MAX_RAS the output will come out blank and if the edc_value not in between MIN_RAS and MAX_RAS the output will be edc_lot.
I believe this is what you are looking for.
--solution using common table expression and cross join
WITH
B AS ( --B for BOUNDS
SELECT
LSpec.Get_lspec_min(
(
SELECT rl_po
FROM DBMgr.rtrlot01
WHERE rl_facility = 'MKLPROD'
AND rl_lot = 'H28BRAD'
),
'9940-RES',
'M'
) AS min_ras,
LSpec.Get_lspec_max(
(
SELECT rl_po
FROM DBMgr.rtrlot01
WHERE rl_facility = 'MKLPROD'
AND rl_lot = 'H28BRAD'
),
'9940-RES',
'M'
) AS max_ras
FROM dual
)
SELECT
R.edc_facility,
R.edc_parm,
R.edc_lot,
R.edc_unit,
R.edc_waf_id,
R.edc_seq,
R.edc_value,
CASE
WHEN R.edc_value >= B.min_ras AND edc_value <= B.max_ras THEN NULL
ELSE R.edc_lot
END AS logic_requirement -- here is your requirement
FROM DBMgr.RTREDC00 R
CROSS JOIN B
WHERE R.edc_parm = '9940-RESOE'
AND R.edc_lot = 'H28BRAD'
;
--solution using scalar subqueries within CASE statement of SELECT clause
SELECT
R.edc_facility,
R.edc_parm,
R.edc_lot,
R.edc_unit,
R.edc_waf_id,
R.edc_seq,
R.edc_value,
CASE
WHEN edc_value >= (
SELECT
LSpec.Get_lspec_min(
(
SELECT rl_po
FROM DBMgr.rtrlot01
WHERE rl_facility = 'MKLPROD'
AND rl_lot = 'H28BRAD'
),
'9940-RES',
'M'
)
FROM Dual
)
AND edc_value <= (
SELECT
LSpec.Get_lspec_max(
(
SELECT rl_po
FROM DBMgr.rtrlot01
WHERE rl_facility = 'MKLPROD'
AND rl_lot = 'H28BRAD'
),
'9940-RES',
'M'
) AS max_ras
FROM Dual
)
THEN NULL
ELSE edc_lot
END AS logic_requirement
FROM DBMgr.RTREDC00
WHERE edc_parm = '9940-RESOE'
AND edc_lot = 'H28BRAD'
;

PostgreSQL 9.3: REPLACE string

I have a string to replace with the expected form.
Input: I have the following string.
'A,B,C,D,E,F,G,H,I,J,K,L'
And I want to replace the above string into the following format:
'x.A = z.A ,
x.B = z.B ,
x.C = z.C ,
x.D = z.D ,
x.E = z.E ,
x.F = z.F ,
.........
.........
x.L = z.L'
My try:
SELECT 'x.'||REPLACE('A,B,C,D,E,F,G,H,I,J,K,L',',',' = z.')
SELECT 'x.' || col || '=z.' || col
FROM (
SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) col
) t
You can use string_agg and FORMAT:
SELECT string_agg(FORMAT('x.%s = z.%s', t,t) , ',')
FROM (SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) AS t
) AS sub;
You can use as delimeter E',\r\n' to get carriage return:
SELECT string_agg(FORMAT('x.%s = z.%s', t,t) , E',\r\n')
FROM (SELECT unnest(regexp_split_to_array('A,B,C,D,E,F,G,H,I,J,K,L', ',')) AS t)
AS sub
Output:
x.A = z.A,
x.B = z.B,
x.C = z.C,
x.D = z.D,
x.E = z.E,
x.F = z.F,
x.G = z.G,
x.H = z.H,
x.I = z.I,
x.J = z.J,
x.K = z.K,
x.L = z.L

How do I get the max from a partitioned query using Rank ()

I want the result to return the max Rank when partitioned using the rank function.
I am using the following query.
SELECT DISTINCT dbo.pomst.co_num
,dbo.pomst.wh_num
,dbo.pomst.po_number
,dbo.pomst.po_suffix
,dbo.pomst.vendor_id
,dbo.item.uom
,dbo.item.upc_num
,dbo.item.item_desc
,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
,dbo.auditlog.abs_num
,dbo.auditlog.item_qty
,dbo.auditlog.lot
,dbo.auditlog.packer
,auditlog.comments
,auditlog.date_time
,rank() OVER (
PARTITION BY auditlog.comments ORDER BY auditlog.date_time ASC
) AS CorrectTrans
FROM dbo.auditlog
INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
AND dbo.auditlog.wh_num = dbo.pomst.wh_num
AND dbo.auditlog.po_number = dbo.pomst.po_number
AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
AND dbo.auditlog.wh_num = dbo.item.wh_num
AND dbo.auditlog.abs_num = dbo.item.abs_num
WHERE (dbo.pomst.co_num = 'AC01')
AND (dbo.pomst.wh_num = 'KU22')
AND (dbo.pomst.row_status = 'C')
AND (dbo.auditlog.trans_type = 're')
AND item_qty NOT LIKE '-%'
I figured it out! I was trying to get the max result of a rank, but if I flip order of rank from asc to desc and use a CTE I can select the the results that always have 1 as the rank as opposed to trying to get the Max. I would still like to know how to get the Max rank but this solution suits my needs.
;with cte as
(SELECT DISTINCT dbo.pomst.co_num
,dbo.pomst.wh_num
,dbo.pomst.po_number
,dbo.pomst.po_suffix
,dbo.pomst.vendor_id
,dbo.item.uom
,dbo.item.upc_num
,dbo.item.item_desc
,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
,dbo.auditlog.abs_num
,dbo.auditlog.item_qty
,dbo.auditlog.lot
,dbo.auditlog.packer
,auditlog.comments
,auditlog.date_time
,rank() OVER (
PARTITION BY auditlog.comments ORDER BY auditlog.date_time desc
) AS CorrectTrans
FROM dbo.auditlog
INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
AND dbo.auditlog.wh_num = dbo.pomst.wh_num
AND dbo.auditlog.po_number = dbo.pomst.po_number
AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
AND dbo.auditlog.wh_num = dbo.item.wh_num
AND dbo.auditlog.abs_num = dbo.item.abs_num
WHERE (dbo.pomst.co_num = 'AC01')
AND (dbo.pomst.wh_num = 'KU22')
AND (dbo.pomst.row_status = 'C')
AND (dbo.auditlog.trans_type = 're')
AND item_qty NOT LIKE '-%'
)
Select * from cte
where CorrectTrans = 1
Add select and group by and use your existing query as a sub query.
Try ..
select max([CorrectTrans]), Vendor_Id, Item_qty, Lot, Pallet_id
from (
-- Your existing query --
SELECT DISTINCT dbo.pomst.co_num
,dbo.pomst.wh_num
,dbo.pomst.po_number
,dbo.pomst.po_suffix
,dbo.pomst.vendor_id
,dbo.item.uom
,dbo.item.upc_num
,dbo.item.item_desc
,RIGHT(dbo.auditlog.pallet_id, 8) AS pallet_id
,dbo.auditlog.abs_num
,dbo.auditlog.item_qty
,dbo.auditlog.lot
,dbo.auditlog.packer
,auditlog.comments
,auditlog.date_time
,rank() OVER (
PARTITION BY auditlog.comments ORDER BY auditlog.date_time ASC
) AS CorrectTrans
FROM dbo.auditlog
INNER JOIN dbo.pomst ON dbo.auditlog.co_num = dbo.pomst.co_num
AND dbo.auditlog.wh_num = dbo.pomst.wh_num
AND dbo.auditlog.po_number = dbo.pomst.po_number
AND dbo.auditlog.po_suffix = dbo.pomst.po_suffix
INNER JOIN dbo.item ON dbo.auditlog.co_num = dbo.item.co_num
AND dbo.auditlog.wh_num = dbo.item.wh_num
AND dbo.auditlog.abs_num = dbo.item.abs_num
WHERE (dbo.pomst.co_num = 'AC01')
AND (dbo.pomst.wh_num = 'KU22')
AND (dbo.pomst.row_status = 'C')
AND (dbo.auditlog.trans_type = 're')
AND item_qty NOT LIKE '-%'
-- =======================================
) x
group by Vendor_id, Item_qty, Lot, Pallet_id