Access 2007 Left join and nulls - sql

I ran the following simple query that looks like the example below:
(Briefly; one order has from 1 to N items and the foreign key is order_id in table items)
SELECT orders.*, items.*
FROM orders
LEFT JOIN
items ON orders.id= items.order_id
This shows something like:
order.id item.id
1 34
1 22
1 90
2 44
2 19
2 21
2 22
I want an output like:
order.id item.id
1 34
22
90
2 44
19
21
22
How to achieve that on Access 2007 ?
Thank you in advance,
Miloud B.

The only way I can think of doing this would be a self join with an aggregate. It will be easier/better handled in your application.
SELECT
IIF( i2.id = MIN(i.id), i.order_id, NULL) AS order_id,
i2.id AS item_id
FROM items i
INNER JOIN orders o
ON o.id= i.order_id
LEFT JOIN items i2
ON i2.order_id=i.order_id
GROUP BY i.order_id
ORDER BY i.order_id,
i2.id

Related

I need to categorize my customer for eligible for bonus and Ineligible based on orders count

I have 2 tables, the first one is Orders as the following:
Customer_ID
ORDER_ID
STATUS
A
11
completed
A
12
completed
B
13
completed
B
14
completed
B
15
completed
C
16
completed
B
17
cancelled
A
18
cancelled
And the second one is Customers as the following:
Customer_ID
Customer_status
join_date
A
15
2022-02-09
b
15
2022-02-10
c
10
2022-02-10
I tried a query to use as a sub-query but it didn't work, I'm new to this and still struggling.
SELECT T1.customer_id, count (T1.ORDER_ID) as Orders_count
FROM orders T1 LEFT join customers T2
on T1.Customer_ID = T2.Customer_ID
where T1.STATUS= 6 AND T2.Customer_status= 15
AND T2.join_date between timestamp'2022-02-10 'and timestamp '2022-02-11'
GROUP BY T1.Customer_ID ORDER BY T1.Customer_ID
I want to categorize the users as eligible or ineligible for a bonus. The eligibles are the ones whose user_status = 15, who made more than 1 order, and whose joining date is 2022-02-10, others are ineligible. I want the table to show both, I'm using redash for that matter.
group by and having can be used to create a list of customers who have placed many orders.
Then a left join to join this list and see who is eligible or not.
select c.*, if(s.Customer_ID is not null, 'YES','NO') as Eligible
from customers c
left join (
select Customer_ID
from orders
group by Customer_ID
having count(1) > 1
) as s on s.Customer_ID = c.Customer_ID and c.Customer_status = 15 and c.join_date = '2022-02-10'
Demo here

SQL Server select parts, fill with 0 no result

I have the following tables
Location (Id, locationName)
Inventory (productid, qty, locationid)
With the following data, I need to query to show all locations per productid, even when not in inventory table. example of records below.
Table Location
Id Location Name
--------------------
1 Plant
2 Warehouse
3 Container
Table Inventory:
Productid Qty Locationid
-----------------------------
45 30 1
45 56 2
3 15 1
3 50 3
15 25 3
Desired result for my query:
Productid Qty LocationName
---------------------------------
45 30 Plant
45 56 Warehouse
45 0 Container
3 15 Plant
3 0 Warehouse
3 50 Container
15 0 Plant
15 0 Warehouse
15 25 Container
So far I have tried many different ways but no luck, so any help will be appreciated.
You can use the following query:
SELECT p.ProductId,
COALESCE(qty,0) AS qty,
[Location Name]
FROM LOCATION l
CROSS JOIN (SELECT DISTINCT ProductId FROM Inventory) AS p
LEFT JOIN Inventory i ON l.Id = i.locationid AND p.Productid = i.Productid
ORDER BY Productid, [Location Name]
The query uses CROSS JOIN to get all possible combinations between locations and products.
Demo here
Select y., isNull(z.Quantity,0) as Quantity
From
(
Select Location., x.ProductId
From Location,(Select Distinct ProductId From Inventory) as x
) as y
Left Outer Join Inventory z ON y.Id = z.LocationId
and y.ProductId = z.ProductId

SQL query with one-to-many relationship with PostgreSQL

I'm trying to aggregate a list of cart items with a query that relates through a line_items table. I've abstracted a simple example of my use case:
my expected result would look like this:
cart_id cart_total shopper_id items payment_id
1 345 34 [{"name":"egg","price:"39"}] AS34gsdVSWET4
2 54 45 [{"name":"biscut","price:"5"},{"name":"apple","price:"15"}] JFHERHJE$Y#$H
given a schema and data like:
carts:
id cart_total shopper_id
1 39 34
2 20 45
line_items:
id cart_id item_name item_price
1 1 egg 39
2 2 biscut 5
3 2 apple 15
payment:
id cart_id payment_id
1 1 AS34gsdVSWET4
2 2 JFHERHJE$Y#$H
How to get all cart list and get a list of carts for particular shopperId?
Here is my solution using json_build_object:
SELECT c.id AS cart_id,
c.cart_total,
c.shopper_id,
json_agg(
json_build_object(
'item_name', i.item_name,
'item_price', i.item_price::text
)
) AS items,
p.payment_id
FROM carts AS c
JOIN line_items AS i ON i.cart_id = c.id
JOIN payment AS p ON p.cart_id = c.id
GROUP BY c.id, c.cart_total, c.shopper_id, p.payment_id;
Are you sure you want the price as string?

How to make joins in my criteria in SQL SERVER?

I have three tables:
**1.FT_ViewItemMovement **
StoreID ItemLookupCode QTY
-------------------------------
1001 11121111 222
1001 11121111 1
1201 11121111 333
1201 11121111 2
2.Item
ItemLookupCode ID
------------------------
11121111 111
3.ItemDynamic
ItemID StoreID SnapShotQuantity
-------------------------------------
111 1201 50
111 1001 25
111 5000 75
111 7777 100
Expecting Result
ItemID StoreID QTY SnapShotQuantity
-------------------------------------
111 1201 335 50
111 1001 223 25
111 5000 0 75
111 7777 0 100
I Tried this following Query, But I didn't get
SELECT
ViewItemMovement.ItemLookupCode,
ViewItemMovement.StoreID,
Sum(ViewItemMovement.Quantity) Quantity,
ItemDynamic.SnapShotQuantity SanpShotQuantity
FROM
FT_ViewItemMovement ViewItemMovement
left join Item with(NoLock) on Item.ItemLookupCode = ViewItemMovement.ItemLookupCode
left join ItemDynamic with(NoLock) on ItemDynamic.ItemID = Item.ID and ItemDynamic.StoreID = ViewItemMovement.StoreID
WHERE brand = 'PEPSI'
Group By
ViewItemMovement.ItemLookupCode,
ViewItemMovement.StoreID,
ViewItemMovement.ItemDescription,
ViewItemMovement.Brand,
Item.Cost,
ItemDynamic.SnapShotQuantity
first I want to summarise the quanity of FT_ViewItemMovement.Qty by StoreID and ItemLookupCode and then if it matches the same itemID with ItemDynamic it has to bring SnapShotQuanity
If there is no FT_ViewItemMovement.Qty, then show FT_ViewItemMovement.Qty = 0 as Expecting output
Simplify your query by looking at what you need, which is two SUM's and a GROUP BY containing the other selections, while using StoreID from FT_ViewItemMovement to allow the SUM(v.QTY) to work.
SELECT id.ItemID, v.StoreID, SUM(v.QTY), SUM(id.SnapShotQuantity)
FROM FT_ViewItemMovement v
INNER JOIN Item i ON v.ItemLookupCode = i.ItemLookupCode
INNER JOIN ItemDynamic id ON i.ID = id.ItemID
GROUP BY id.ItemID, v.StoreID
Your GROUP BY clause looks too complicated. I think this is what you want:
SELECT vim.ItemLookupCode, vim.StoreID,
COALESCE(Sum(vim.Quantity), 0) as Quantity,
id.SnapShotQuantity as SanpShotQuantity
FROM FT_ViewItemMovement vim LEFT JOIN
Item i
ON i.ItemLookupCode = vim.ItemLookupCode LEFT JOIN
ItemDynamic id
ON id.ItemID = i.ID and id.StoreID = vim.StoreID
WHERE vim.brand = 'PEPSI'
GROUP BY vim.ItemLookupCode, vim.StoreID,
id.SnapShotQuantity;
Your WHERE clause references brand. I have no idea where that comes from based on your question.
With your data sample, you must select from ItemDynamic then use join other table.
I hope it will work for you.
SELECT
ItemDynamic.ItemID,
ItemDynamic.StoreID,
SUM(ViewItemMovement.Quantity) AS Quantity,
MAX(ItemDynamic.SnapShotQuantity) AS SanpShotQuantity
FROM
ItemDynamic with(NoLock)
left join Item with(NoLock) on Item.ID = ItemDynamic.ItemID
left join FT_ViewItemMovement ViewItemMovement on ViewItemMovement.ItemLookupCode = Item.ItemLookupCode and ViewItemMovement.StoreID = ItemDynamic.StoreID
Group By
ItemDynamic.ItemID,
ItemDynamic.StoreID

Inner join on two tables, with clause

with one as
(Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, Complaint.ComplaintType_id, Complaint.complaintProfileId, Complaint.Description,
Complaint.Email, Complaint.PriorityLevel_id, Complaint.Date_Complained, Complaint.Status, Complaint.AdminComments, Complaint.Phone, Complaint.Evidence
from Complaints Complaint )
The result of this query is (not the entire result)
sno complaintProfileId
1 14
2 15
3 15
4 14
5 14
6 13
The second subquery:
two as
(SELECT Complaint.complaintProfileId,
CASE
WHEN MMB_Name IS NOT NULL THEN MMB_Name
WHEN UPPMembership.profile_id IS NOT NULL THEN 'UPP'
ELSE 'Not found'
END as Name
FROM Complaints Complaint
LEFT JOIN MMBMembership
ON MMBMembership.profile_id = Complaint.complaintProfileId
left JOIN MMB_BusinessProfiles mmbProfiles
ON mmbProfiles.MMB_id = MMBMembership.MMB_id
LEFT JOIN UPPMembership
ON UPPMembership.profile_id = Complaint.complaintProfileId)
complaintProfileId Name
14 UPP
15 Marlon
15 Marlon
14 UPP
14 UPP
13 Rodolfo
So this is where I am having trouble with
select one.*, two.Name
from one join two
on one.complaintProfileId = two.complaintProfileId
This query returns 36 records. id 14 is being returned 9times and 15-6times and so on..
I am doing a inner join but still not sure
Thanks
Sun
You need to join on a unique key. Each '14' on the left side is being joined to each of the three '14's on the right side. (3x3=9)