Product with Last Purchase Date - sql-server-2000

I would like to query from SQL Server 2000 Database.I have got two tables. They are Purchase and PurchaseDetails. I would like to get product records with Last Purchase ID but I can't query with the following statements.So please help me.
SELECT TOP 100 PERCENT dbo.Purchase.PurchaseID AS LastOfPurchaseID, dbo.PurchaseDetails.ProductID,
MAX(dbo.Purchase.PurchaseDate) AS LastOfPurchaseDate
FROM dbo.Purchase INNER JOIN
dbo.PurchaseDetails ON dbo.Purchase.PurchaseID = dbo.PurchaseDetails.PurchaseID
GROUP BY dbo.PurchaseDetails.ProductID, dbo.Purchase.PurchaseDate,dbo.Purchase.PurchaseID
ORDER BY MAX(dbo.Purchase.PurchaseDate) DESC

If you are trying to retrieve the list of products based on the most recently entered order's date, then use a sub-query:
SELECT Purchase.PurchaseID, Purchase.PurchaseDate, PurchaseDetails.ProductID
FROM Purchase
INNER JOIN PurchaseDetails
ON Purchase.PurchaseID = PurchaseDetails.PurchaseID
WHERE (Purchase.PurchaseDate = (SELECT MAX(PurchaseDate) FROM Purchase))

Couldn't you just order by purchase ID in DESC ORDER? Everything gets a little weird when you try to do things with MSSQL ...

Related

Finding the most frequently occurring combination

I have two table with name Orders and Products,The order table contains the number of specific orders made by a customer and the products included in that order is in the Products table.
My requirement is to get the number of total orders against the most frequently coming products.
means for these products product 1,Product 2, product 3 what is the total orders,If an order contains 10 Products which contains Product 1 ,Product 2 and Product 3 that order should be counted.
For an order_id there can be multiple products will be there and i'm confused on how to get this result.Can anyone share or suggest a solution on how to get this?
I'm using PostgreSQL.
Below is the sample query ,
SELECT
"Orders"."order_id",pr.product_name
FROM
"data"."orders" AS "Orders"
LEFT JOIN data.items i On i."order_id"="Orders"."order_id"
LEFT join data.products pr on pr."product_id"=i."product_id"
WHERE TO_CHAR("Orders"."created_at_order",'YYYY-MM-DD') BETWEEN '2019-02-01' AND '2019-04-30'
ORDER BY "Orders"."order_id"
Desired Result will be like this(3 columns),The most purchased product combination with number of occurring orders.
Product 1, Product 2,Product 3,etc..... , Number Of Orders
This is the sample data output,Need the product list which is purchased in combination the most.(As of now i have given only 3 columns for sample but it may vary according to the number of PRODUCTS in an order).
and example
SELECT
"Orders"."order_id",
string_agg(DISTINCT pr.product_name,::character varying, ',') AS product_name
count(1) AS product_no
FROM
"data"."orders" AS "Orders"
LEFT JOIN data.items i On i."order_id"="Orders"."order_id"
LEFT join data.products pr on pr."product_id"=i."product_id"
WHERE TO_CHAR("Orders"."created_at_order",'YYYY-MM-DD') BETWEEN '2019-02-01' AND '2019-04-30'
GROUP BY "Orders"."order_id"
ORDER BY count(1);
You can try to use group by clause.
If you want to generally get the number of orders against some products then you can just count the number of orders grouped on the products from product table. Query should look something like this:
SELECT product_id, COUNT(*)
FROM data.products
GROUP BY product_id
ORDER BY COUNT(*)
LIMIT 1;
Hope this helps!
Try to use GROUP BY and take MOST counted value as below-
SELECT
pr.product_name,
COUNT(DISTINCT Orders.order_id)
FROM
"data"."orders" AS "Orders"
LEFT JOIN data.items i On i."order_id"="Orders"."order_id"
LEFT join data.products pr on pr."product_id"=i."product_id"
WHERE TO_CHAR("Orders"."created_at_order",'YYYY-MM-DD') BETWEEN '2019-02-01' AND '2019-04-30'
GROUP BY pr.product_name
ORDER BY COUNT(DISTINCT Orders.order_id) DESC
LIMIT 1 -- You can use the LIMIT or NOT as per requirement

stacking data from two tables with different dates

I have two tables, Sales and Returns. They have CustomerID, ProductCode, Name, SalesDate, SalesWeek, SalesAmount and ReturnsDate, ReturnsWeek, ReturnsAmount. What I really want to do is just join these tables and stack them on top of each other so the client has data in a single report for both sales and returns.
Sales and Returns dates are different but the product code, customer ID and Name can be same for a record in output table. For instance Customer A bought a product last month and returned it next month, so his record can appear in returns table.
To achieve this I have tried using union by selecting all columns between both tables but I am getting a mix of records for sales and returns with no consistency. All I want to do is see Nulls for Customers who have no business with Returns and vice versa. I was thinking Left Join in this case should work but it isn't working. So I am seeing mixed up data in all columns for Sales and Returns amounts. Attached is the picture that has two tables, the output I am seeing and the out put I want to see. Also I am performing Weekly aggregations for Sales and Returns amounts. What is the best and easiest way to achieve this ? I am sorry I may have not structured my question properly but the image might help #NewToSQL
Presumably, a customer can purchase a given product more than once. If so, a full join is the right solution, but you need a bit more logic to ensure non-duplication in the results:
select . . .
from (select s.*,
row_number() over (partition by product_code, customer_id order by (select NULL)) as seqnum
from sales s
) s full join
(select r.*,
row_number() over (partition by product_code, customer_id order by (select NULL)) as seqnum
from returns r
) r
on s.product_code = r.product_code and s.customer_id = r.customer_id;
I am leaving the name out, because I assume it is defined by the customer id.
You need to do a FULL JOIN sales table with returns table on those columns that you mentioned.
Something like :
SELECT *
FROM sales s
FULL JOIN returns r
ON s.product_code = r.product_code
AND s.customerid = r.customer_id

Access 2002 SQL for joining three tables

I have been trying to get this to work for a while now. I have 3 tables. First table has the Sales for customers which include the CustomerID, DateOfSales (Which always has the first of the month). The second table has the CustomerName, CustomerID. The third table has which customers buy what product lines. They are stored by CustomerID, ProductID.
I want to get a list (from one SQL hopefully) that has ALL the customers that are listed as buying a certain ProductID AND the maxDate from the Sales. I can get all of them IF there are sales for that customer. How the heck do I get ALL customers that buy the certain ProductID AND the maxDate from Sales or NULL if there is no sales found?
SalesList |CustomerList|WhoBuysWhat
----------|------------|-----------
maxDate |CustomerID |CustomerID
CustomerID| |ProductID=17
This is as close as I got. It gets all max dates but only if there have been sales. I want the CustomerID and a NULL for the maxDate if there were no sales recorded yet.
SELECT WhoBuysWhat.CustomerID, CustomerList.CustomerName,
Max(SalesList.MonthYear) AS MaxOfMonthYear FROM (CustomerList INNER
JOIN SalesList ON CustomerList.CustomerID = SalesList.CustomerID) INNER
JOIN WhoBuysWhat ON CustomerList.CustomerID = WhoBuysWhat.CustomerID
WHERE (((SalesList.ProductID)=17)) GROUP BY WhoBuysWhat.CustomerID,
CustomerList.CustomerName;
Is it possible or do I need to use multiple SQL statements? I know we should get something newer than Access 2002 but that is what they have.
You want LEFT JOINs:
SELECT cl.CustomerID, cl.CustomerName,
Max(sl.MonthYear) AS MaxOfMonthYear
FROM (CustomerList as cl LEFT JOIN
(SELECT sl.*
FROM SalesList sl
WHERE sl.ProductID = 17
) as sl
ON cl.CustomerID = sl.CustomerID
) LEFT JOIN
WhoBuysWhat wbw
ON cl.CustomerID = wbw.CustomerID
GROUP BY cl.CustomerID, cl.CustomerName;

SQL - Selection of the oldest order for every customer (join two tables)

I have the following tables:
- CUSTOMERS (ID_CUSTOMER, LASTNAME_CUSTOMER)
- ORDERS (ID_ORDER, DATE_ORDER, ID_CUSTOMER)
and I need SQL request to show the oldest order for each customer, can anyone advise me effective way how to do this?
I got stuck here:
SELECT customers.lastname_customer, orders.date_order
FROM customers
JOIN orders
ON customers.id_customer = orders.id_customer
WHERE orders.date_order IS NOT NULL
ORDER BY orders.date_order
Thanks!
EDIT
Thanks for answers guys, but the solutios not work properly on ORACLE 11g (In the result I still see all orders for each customer, not the only oldest)
To get the oldest order
SELECT LASTNAME_CUSTOMER,MIN(DATE_ORDER) FROM customers c
INNER JOIN orders o on o.ID_CUSTOMER= c.ID_CUSTOMER
GROUP BY o.ID_CUSTOMER,LASTNAME_CUSTOMER

SQL statement: How to retrieve and display each user and the many items they purchase

I want to retrieve and display 3 customers per page and also display all the items they have purchased.
Currently, my sql statment below can retrieve 3 rows of result but are the same customer if they purchase 2 or more items. I know it is possible to get what I want with multiple sql queries but is it possible to do it in one query? Please suggest if you have an idea on how people usually does it. Thanks!
SELECT *
FROM customers, item
WHERE customers.customer_id = customers_item.customer_id
LIMIT 0, 3
Database Table: Customers
- customer_id
- customer_name
Database Table: Item
- item_id
- item_name
Database Table: Customers_item
- customer_id
- item_id
I can't perform the syntax check, but the solution can be like the following:
select c.*, i.*
from (select * from Customers limit 0, 3) c
join Customer_item ci on c.customer_id = ci.customer_id
join item i on ci.item_id = i.item_id
order by c.customer_name, i.item_name
The inner query will return the needed customers, and all the rest will join all items of the selected customers.
Hope that this will help You
Here's what you need:
select * from item where customer_id in (select customers_id from customers)
provide a sample output you are trying to obtain.
select c.*, i.* from Customer c
join
Customer_item ci
on c.customer_id = ci.customer_id
join
item i
on ci.item_id = i.item_id
the code above gives all customers and all items bought by them.