Hybris How to distinguish if a voucher applies on order entry or on the order itself - e-commerce

How to show vouchers against products (order entry) in order details page.
In OrderData I see appliedVouchers but unable to distinguish voucher applied to which order entry.
final OrderData orderData = orderFacade.getOrderDetailsForCode(orderCode);
final List<VoucherData> voucherList = orderData.getAppliedVouchers();

flexible query to check which voucher is used in which order
select {vi.code}, {o.code} from
{ VoucherInvalidation as vi
join Order as o
on {o.pk} = {vi.order}
}
Basically voucher usage is stored in VoucherInvalidation

Related

Displaying a sum incrementally per row using window function in postgresql

I need to enter the total length of verblijfsduur (stay) per reisnr (trip) incrementally according to the order in which the celestial objects are visited.
This what I need: (ignore the tot_duur column, that is the sum of all verblijfsduur)
This what I get, see it dosn't show incrementally according to order, it shows the total perreisnr (trip number):
I don't know how I could indicate this in the PARTITION BY part of my query:
SELECT re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
,sum(be.verblijfsduur) OVER (PARTITION BY re.reisnr ORDER BY re.reisnr ) as
inc_duur
FROM reizen re INNER JOIN bezoeken be ON re.reisnr = be.reisnr
ORDER BY re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
I found the problem, thanks/sorry if you had an answer, I had to order by be.volgnr
SELECT re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur
,SUM(be.verblijfsduur) OVER (PARTITION BY re.reisnr ORDER BY be.volgnr) as inc_duur
FROM reizen re INNER JOIN bezoeken be ON re.reisnr = be.reisnr
ORDER BY re.reisnr, be.volgnr, be.objectnaam, be.verblijfsduur

SQL query not working properly (not sure about it)

I have 2 tables: ORDER_INFO_TABLE and APPROVE_TABLE
The first one contains the information related to "orders".. DO NOTE that a single order may contain different "order lines" (thats why you'll see the order number repeated in the image below).Also, there exist a field called "PRICE" (this field is not SHOWN in my image) and CANCELLED (order line status)
The second table contains the information related to the "approvers" for the different order lines. Inside this table you will see a field called "APPROVERID", ITEMID (order line id) and "APPROVED" (order line status). The approvers must check if the price is okay or not. If the order line is okay, the approver will put a number 1 in the field "APPROVED". If the price is not correct, he will put a number 1 in the field CANCELLED in the other table.
Take a look at these images:
I have tried without success to obtain all the cancelled orders (an order is cancelled when all ITS order lines are cancelled) AND the approved order (an approved order may contain cancelled order lines) for an specific approver
I tried so many times, using count operator, left join but i am completely lost :(
A guy suggested me to use this query:
SELECT web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON (oif.item_id = apt.item_id)
GROUP BY web_order_id
HAVING SUM(cancelled) = COUNT(*);
The problem is that it is not woking properly (it shows this error message: operand data type is invalid for sum operator) AND using this query i will not be able to obtain the CANCELLED and APPROVED orders for a specific approver.
EDIT:
SELECT web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON (oif.item_id = apt.item_id **and apt.APPROVERID = 'RANDOM#MAILNATOR.COM'**)
GROUP BY web_order_id
HAVING SUM(cancelled) = COUNT(*);
Edited: If you just want to select order from specific approver then just use simple select with join:
SELECT oif.web_order_id, oif.item_id, apt.approverid,
oif.cancelled, apt.approved
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON oif.item_id = apt.item_id
WHERE apt.approverid = 'RANDOM#MAILNATOR.COM'
ORDER BY oif.web_order_id, oif.item_id;
Seems that your cancelled column datatype is VARCHAR. You should convert it to number before using SUM function
SELECT oif.web_order_id
FROM ORDER_INFO_TABLE oif
INNER JOIN APPROVE_TABLE apt
ON oif.item_id = apt.item_id
GROUP BY oif.web_order_id
HAVING SUM(CAST(oif.cancelled AS int)) = COUNT(*);

How to return 1st record from group by

Trying to return only the 1st supplier code and have all other fields unaffected.
`Select
Container.Part_Key,
Part.Part_No,
Part.Name,
Part.Revision,
Container.Quantity,
Container.Container_Status,
OP.Operation_No,
Opp.Operation_Code,
Part.Part_Status,
Supplier.Supplier_Code
From Part_v_Container as Container
Join Part_v_Part as Part
ON Part.Part_Key = Container.Part_Key
Join Part_v_Part_Operation as Op
On Op.Part_Operation_Key = Container.Part_Operation_Key
Join Part_v_Operation as OPP
On OPP.Operation_Key = OP.Operation_Key
Join part_v_approved_supplier as Approved
On Approved.part_key = container.part_key
Join common_v_Supplier as Supplier
On Supplier.supplier_no = Approved.Supplier_No
Where Container.Active = '1'
group by container.part_key`
There will be duplicate part numbers, revisions, etc. Not worried about that. I just want the part no to list only one approved supplier to the far right, even though for any given part, there are multiple approved suppliers listed in the database.
Furthermore, the order the database lists the approved suppliers does not matter.
Thanks!
Add a sub query in your select list
( select top 1 supplier.supplier_code
From supplier
Where Supplier.supplier_no = Approved.Supplier_No order by Supplier.supplier_no) as supplier code
This can be the last field in the select list
You could add An appropriate order by.
This would work in SQL Server

sql - calculating the price of an order and sum of all orders on a receipt

I have three tables as follows:
MenuItem
MenuItemID ItemName ItemPrice
X0001 LatteSmall $15
X0002 LatteBig $18
X0003 MochaSmall $16
Orders
OrderID MenuItemID ReceiptID`
O000001 X0001 R000001
O000002 X0002 R000001
O000003 X0001 R000002
O000004 X0003 R000003
Receipt
ReceiptID ReceiptPrice
R00000001 ???????????
R00000002 ???????????
R00000003 ???????????
R00000004 ???????????
What I am trying yo do is: Calculate the price of each order, and then sum up orders that belong to each receipt. List the summed up values in the ReceiptPrice field on the Receipt table.
How do I do this with a single query on Microsoft Access 2010?
Any help is appreciated =))
From the sample data you have provided it's a little hard to tell, but I assume that each receipt may contain multiple orders.
The following query will return each order on each receipt with a total menu item cost per order. This assumes that ItemPrice is numerical, and doesn't actually contain the '$' character, maybe you could confirm that?
SELECT O.ReceiptID, O.OrderID, SUM(MI.ItemPrice) AS TotalReceiptPrice
FROM Orders AS O INNER JOIN MenuItem AS MI ON O.MenuItemID = MI.MenuItemID
GROUP BY O.ReceiptID, O.OrderID
If you wish to only see the total for each receipt, including multiple orders, then try this;
SELECT O.ReceiptID, SUM(MI.ItemPrice) AS TotalReceiptPrice
FROM Orders AS O INNER JOIN MenuItem AS MI ON O.MenuItemID = MI.MenuItemID
GROUP BY O.ReceiptID
You want to update the database. Here is one way:
update receipt
set receiptprice = (select sum(itemprice)
from menuitem inner join
orders
on menuitem.menuitemid = orders.menuitemid
where orders.receiptid = receipt.receiptid
);

Linq group by and order by multiple conditions

I have the following:
myFilteredContractors = (From c In myFilteredContractors
Join cc In myConClasses On c.ContractorId Equals cc.ContractorId
Where inClassifications.Contains(cc.ClassificationId)
Group c By Key = cc.ContractorId Into Group, Count = Count()
Order By Count Descending
Select (From c1 In myContractors Where c1.ContractorId = Key).FirstOrDefault).ToList
This is properly ordering this list of contractors by the number of classifications that they have.
I also want to order them by whether or not they have a field set (CompanyOverview), which if is an empty string should be below those contractors who have set their CompanyOverview. Also, after the CompanyOverview is ordered I want to order by Registration Date.
So it should order by:
Number of Classifications
Whether Overview has been set (c.CompanyOverview)
Registration Date (c.AppliedDate)
Is it possible to all of this in LINQ?
Yes, just add one order by clause after another: order by X, order by Y, ...
If you fancy lambda check OrderBy() and ThenBy() functions.