Flexible search query using three tables - sap-commerce-cloud

I have three tables Order,Customer,Address.
Edited :
I want to fetch all customer's who are registeretd with website.
If the customer has placed any order's i want to fetch the latest order from list of order's,
If customer has not placed any order then i want to put the field as blank as shown in below table
For eg :
Customer_Name Email ID Country Phone Latest_Order_Code
A xxxx xxxx x 1234
B yyyy yyyy y
C ffff tttt l 3456
D zzzz iiii o
Any help would be appreciated?

Refer to below query, where I have only fetch order code and Customer name. You can write more join and select fields based on your requirement.
select {o.code} as orderCode,
{c.name} as name,
{a.cellphone} as cellphone
from {order as o
join Customer as c on {c.pk} = {o.user}
join Address as a on {o.deliveryaddress} = {a.pk}
}
where {o.code} in ({{select max({code}) from {order} group by {user}}})
Update: Fetch all registered customers and their last order information
select t1.name, t2.orderCode, t2.cellphone
from
({{
select {pk} as userPk, {name} as name from {Customer}
}}) as t1
LEFT JOIN
({{
select
{o.code} as orderCode,
{o.user} as user,
{a.cellphone} as cellphone
from {order as o
join Address as a on {o.deliveryaddress} = {a.pk}
}
where {o.code} in ({{select max({code}) from {order} group by {user}}})
}}) as t2
on t2.user = t1.userPk
Detail post how to write complex flexible search query join using the dynamic tables?

Related

Formula Language: Select all child documents

id
itemId
correlationId
1
A
2
B
A
3
A
4
C
B
5
D
B
6
E
D
Hello, I have a Notes database with a similar structure like the table above. This table contains a unique id for each document. It also contains an itemId, which is not unique, and a correlationId which creates a relation to another item (itemId).
I want to select all documents, which have a specific itemId (in this example A) and every document which is correlated to this itemId (directly or indirectly). The result should look similar to my table.
I was wondering if I need to write multiple requests or if it is possible to write only one formula.
SELECT #Contains(itemId; "A");
SELECT #Contains(correlationId;"A") => retrieve itemId: B ==>SELECT #Contains(correlationId;"B") => retrieve itemId: C, D ==> SELECT #Contains(correlationId;"C") (no result) and SELECT #Contains(correlationId;"D")

sqlite combine 2 queries from different tables to make one

I recently took to using sql again, the last time I used it was in microsoft access 2000 so please bear with me if I'm behind the times a little.
I have 2 pointless virtual currencies on my discord server for my players to play pointless games with. Both of these currencies' transactions are currently stored in individual tables.
I wish to sum up all the transactions for each player to give them a single current amount for each currency. Individually I can do this:
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblGorillaTears.Amount)
FROM
tblPlayers
INNER JOIN
tblGorillaTears
ON
tblPlayers.PlayerID = tblGorillaTears.PlayerID
GROUP BY
tblPlayers.PlayerID;
and
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblKebabs.Amount)
FROM
tblPlayers
INNER JOIN
tblKebabs
ON
tblPlayers.PlayerID = tblKebabs.PlayerID
GROUP BY
tblPlayers.PlayerID;
What i need is a table that outputs the user name the id and the total for each currency on one row, but when i do this:
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
SUM(tblGorillaTears.Amount) AS GT,
0 as Kebabs
FROM
tblPlayers
INNER JOIN
tblGorillaTears
ON
tblPlayers.PlayerID = tblGorillaTears.PlayerID
GROUP BY
tblPlayers.PlayerID
UNION
SELECT
tblPlayers.PlayerID AS PlayerID,
tblPlayers.Name AS Name,
0 as GP,
SUM(tblKebabs.Amount)
FROM
tblPlayers
INNER JOIN
tblKebabs
ON
tblPlayers.PlayerID = tblKebabs.PlayerID
GROUP BY
tblPlayers.PlayerID;
the results end in a row for each player for each currency. How can i make it so both currencies appear in the same row?
Previously in MSAccess i was able to create two queries and then make a query of those two queries as if they were a table, but I cannot figure out how to do that in this instance. Thanks <3
UNION will add new rows for sure, you can try like following query.
SELECT TP.playerid AS PlayerID,
TP.NAME AS NAME,
(SELECT Sum(TG.amount)
FROM tblgorillatears TG
WHERE TG.playerid = TP.playerid) AS GT,
(SELECT Sum(TG.amount)
FROM tblkebabs TG
WHERE TG.playerid = TP.playerid) AS Kebabs
FROM tblplayers TP

How can I fetch 'distinct' data keeping row ID in the lookup view?

Assume I have an entity with the following structure:
ID (it would be the GUID of the record, I'll use number in this example for easier read)
Name
Product (lookup to product)
Price List (custom lookup to standard price list)
I now need to build a lookup view in this fashion:
(Sample data)
ID Name Product PriceList
-----------------------------------
1 Rec1 P1 PL1
2 Rec1 P1 PL2
3 Rec1 P1 PL3
4 Rec2 P2 PL1
5 Rec2 P2 PL2
(Desired result in lookup view)
ID Name
-----------------------------------
1 Rec1
4 Rec2
To explain the requirement in plain english, it is:
In the lookup view I only want one row per Name: Product and Price List don't matter.
Here's what I'd do if I could use straight T-SQL:
;WITH cte AS
(
SELECT ROW_NUMBER()
OVER (PARTITION BY Name ORDER BY Name ASC) AS RowNo, ID, Name
FROM FilteredEntity
)
SELECT ID, Name FROM cte WHERE RowNo = 1
But I can't figure out how to achieve the same result set in a FetchXML query.
The only way I can think of to do this would go like this:
In JS on your form, determine the unique names (Rec1 and Rec2 from your example), and store the ids of two records that reference Rec1 and Rec2 (let's call these ids Id1 and Id2)
Generate a fetch query that returns only those two records. Your fetch query would specifically say show me records where id is Id1 or Id2.
Filter the lookup using addCustomFilter and addPreSearch (link).
I can see two approaches here, neither of which are pure FetchXML queries.
Use distinct, (I'm not 100% on this but worth giving a go). This will then hopefully get you a record for every name, and you just grab the first record of each in code.
<attribute name='name' distinct='true'/>
Perform the filtering client side using Linq.

SQ Server : how to find the fifth order for each customer

How to find the fifth order for each customer and return title_order or null if the customer doesn't have the fifth order
Tables are
customer with columns Id, firstname, lastname...
order with columns order_id, title_order, id_custmer, date...
It can be done only with a query or do I need to create a function
Thanks in advance
You can use OUTER APPLY with OFFSET-FETCH:
select c.firstname, oa.title_order
from customer c
outer apply(select title_order from order o
where o.id_custmer = c.Id
order by date
offset 4 ROW FETCH next 1 ROW only)oa

Give the employee(fname and lname) that make the most money in each dept

SELECT max(salary), dept
FROM Employees
GROUP BY dept;
How to add fname and lname without the errors? If I add them in group by, I no longer get the max salary.
This is how I would do it in SQL Server... You need a subquery that returns the max salary for each department, then join that back to the employee table to figure out which employee has that salary for that department:
SELECT D.Dept, D.MaxSalary, Employees.FName, Employees.LName
FROM (
SELECT MAX(Salary) AS MaxSalary, Dept
FROM Employees
GROUP BY dept
) D
INNER JOIN Employees
ON Employees.Dept = D.Dept
AND Employees.Salary = D.MaxSalary

Resources