Show dates older than current date in sql query - sql

In my sql statement I have an SQL statement which i need to show which customers return dates are overdue i.e. older than current date, so far my query looks like the following:
SELECT customers.firstname, items.itemname, CustEquipment.ReturnDate, customers.phonenumber
FROM items join
CustEquipment
on items.ItemId = CustEquipment.ItemId join
customers
on CustEquipment.customerID=customers.customerID
This currently shows all customers and their return dates (along with other info i need) but i want to show the return dates which have gone past the current date, anyone care to point me in the right direction please?

You can do the date comparison in the where clause:
SELECT customers.firstname, items.itemname, CustEquipment.ReturnDate, customers.phonenumber
FROM items join
CustEquipment
on items.ItemId = CustEquipment.ItemId join
customers
on CustEquipment.customerID=customers.customerID
where ReturnDate <= trunc(sysdate);
I also adjusted your query to use correct join syntax.

select C.Firstname,I.ItemName,CE.ReturnDate,C.PhoneNumber
from Items I
inner join CustEquipments CE
on
CE.ItemID=I.ItemID
inner join Customers C
on
C.CustomerID=CE.CustomerID
where
convert(varchar(10),getdate(),101)>convert(varchar(10),CE.ReturnDate,101)

Related

Transferring Data from one table to another

I need help with my sql code, is there a way for me to transfer the data from "AS subtotal" from the first SELECT statement to the "receipt.totalAmount" from the second SELECT statement. Kind of new with sql I tried to look it up online but I didn't see any solution for it. I can't declare subtotal from the first SELECT statement to the second SELECT statement. Also is it possible to sum up all the subtotals to the total column?
SELECT productServices.productId, productServices.proPrice, orders.orderId, orders.quantity, productServices.proPrice*orders.quantity AS subtotal , orders.dateOrdered
FROM productServices
JOIN orders
ON productServices.productId=orders.productId
SELECT receipt.receiptNo, receipt.customerId, receipt.orderId, receipt.employeeId, receipt.totalAmount, receipt.paymentMethod, receipt.dateOfPurchase
FROM receipt
JOIN customerInfo ON customerInfo.customerId=receipt.customerId
JOIN employeeInfo ON employeeInfo.employeeId=receipt.employeeId
JOIN orders ON orders.orderId=receipt.orderId
You need to write it into a single select statement. Something like this. Or use inner queries to get what you need.
SELECT receipt.receiptNo, receipt.customerId, receipt.orderId, receipt.employeeId, receipt.totalAmount, receipt.paymentMethod, receipt.dateOfPurchase,
productServices.productId, productServices.proPrice, orders.orderId, orders.quantity, productServices.proPrice*orders.quantity AS subtotal , orders.dateOrdered
FROM receipt
JOIN customerInfo ON customerInfo.customerId=receipt.customerId
JOIN employeeInfo ON employeeInfo.employeeId=receipt.employeeId
JOIN orders ON orders.orderId=receipt.orderId
JOIN productServices on productServices.productid = orders.productid

SQL: Left Join with three tables

I have three tables
Products (idProduct, name)
Invoices(typeinvoice, numberinvoice, date)
Item-invoices(typeinvoice, numberinvoice, idProduct)
My query has to select all the products not selled in the year 2019. I can use a function to obtain the year from the date, for example year(i.date). I know that the products that don't appear in the Item-invoice table are the not selled products. So I have tried with this two codes and obtain a good output.
SELECT p.name
FROM Products p
EXECPT
SELECT ii.idProduct
FROM Item-invoices ii, Invoices i
WHERE ii.typeinvoice=i.typeinvoice
AND ii.numberinvoice=i.numberinvocice
AND year(i.date)=2019
And the other code use a sub-query:
SELECT p.name
FROM Products p
WHERE p.idProduct NOT IN
(SELECT ii.idProduct
FROM Item-invoices ii, Invoices i
WHERE ii.typeinvoice=i.typeinvoice
AND ii.numberinvoice=i.numberinvocice
AND year(i.date)=2019)
The answer is how can i use the left join command to have the same output. I've tried with
SELECT p.name
FROM Products p
LEFT JOIN Item-invoices ii ON
p.IdProduct=ii.idProduct
LEFT JOIN Invoices i ON
ii.typeinvoice=i.typeinvoice
AND ii.numberinvoice=i.numberinvocice
WHERE year(i.date)=2019
AND ii.idProduct IS NULL
I know this is wrong but can't find the solution
Any help?
You are almost there. You just need to move the condition on the invoice date to from the from clause to the on clause of the join.
Conditions in the WHERE clause are mandatory, so what you did actually turned the LEFT JOI to an INNER JOIN, which can never be fulfilled (since both conditions in the WHERE clause cannot be true at the same time).
SELECT p.name
FROM Products p
LEFT JOIN Item-invoices ii ON
p.IdProduct=ii.idProduct
LEFT JOIN Invoices i ON
ii.typeinvoice=i.typeinvoice
AND ii.numberinvoice=i.numberinvocice
AND i.date >= '2019-01-01'
AND i.date < '2020-01-01'
WHERE ii.idProduct IS NULL
Note that I changed your date filter to a half-open filter that operates directly on the stored date, without using date functions; this is a more efficient way to proceed (since it allows the database to use an existing index).

How To Display Customers Even If They Have No Sales (results)

I have a code that pulls all of a Reps new customers and their year to date sales. The only problem is it only pulls customers that have sales in the invdate range, but I need it to show all of the accounts with a 0 if they do not have any sales. Is there any way to achieve this? I tried using COALESCE and it didn't seem to work. I also tried using left, right, full outer joins. Any help would be appreciated!
select
a.Acctnum,
sum(a.invtotal) as total
from invoices a right join accounts b on a.acctnum = b.acctnum where
a.invdate between '1/1/2017' and '12/31/2017'
and a.sls = '78'
and b.sls = '78'
and b.activetype = 'y' and b.startdate > (getdate()-365)
group by a.acctnum
order by total desc
You are restricting your results in your WHERE clause AFTER you join your table causing records to drop. Instead, switch to a LEFT OUTER JOIN with your accounts table driving the query. Then restrict your invoices table in your ON clause so that invoices are dropped BEFORE you join.
SELECT a.Acctnum,
sum(a.invtotal) AS total
FROM accounts b
LEFT OUTER JOIN invoices a ON
a.accntnum = b.acctnum AND
--Put the restrictions on your left most table here
--so they are removed BEFORE joining.
a.invdate BETWEEN '1/1/2017' AND '12/31/2017'
AND a.sls = '78'
WHERE
b.sls = '78'
AND b.activetype = 'y'
AND b.startdate > (getdate() - 365)
GROUP BY a.acctnum
ORDER BY total DESC
It's a bit like doing a subquery in invoices before joining in as the left table. It's just easier to drop the conditions into the ON clause.
Your problem is you where clauses are changing the right join to an inner join. Put all the ones that are aliased by a. into the ON clause.

Using a where statement against left outer join

I have a query that returns ALL the appointments for an advisor in a week, and what items they sold at that appointment (if non have been sold it still shows the appointment) I have done this by using a left outer join on the appointments table and ithe items table.
In this scenario I only want to display sold items in stock ( in stock is a field in the items table)
If I use a 'where instock true' against items, I loose all the appointments where no items have been sold ?
Do I need to nest these queries somehow - if so how ? Thanks
The key to what you want to do is to move the condition on the stock to the on clause from the where clause. Presumably, your existing query looks something like this:
select <whatever>
from appointments a left join
sells s
on a.appointmentid = s.appointmentid
where s.instock = 1;
The where clause is turning the left join into an inner join. When there is no match, the value of instock is NULL, which does not match 1. The solution is to move the condition to the on clause:
select <whatever>
from appointments a left join
sells s
on a.appointmentid = s.appointmentid and
s.instock = 1;
You should use ON instead WHERE. That way, if the items aren't in stock the wont match and the right part of your join will be NULL.
SELECT A.*, S.* FROM appointments A LEFT JOIN sells S ON ... AND sells.instock = 1;
I could be more precise if I had the SQL you've tried to write.

Show Zero Sales For Item in SQL Query

Being new to SQL I have a problem with my query that I am sure has a trivial solution. I am trying to make a report for item sales in a specific period and I need to display 0 or empty cell if there are no sales. Instead I don't get item displayed at all.
Current query is:
SELECT stock.stock_item_code, stock.physical_quantity, sales.quantity
FROM stock_branch_level AS stock LEFT OUTER JOIN
booked_sales AS sales ON stock.stock_item_code = sales.stock_item_code
WHERE (stock.replen_type = 1) AND (sales.created_date > #DateFrom)
Thanks
LEFT OUTER JOIN is the right way to get rows even if there are no sales.
The problem is with your where clause on sales.
Note that when there are no sales all sales columns will be null in that row!
The solution is to move the restriction on sales.created_date to the join clause:
SELECT stock.stock_item_code, stock.physical_quantity, sales.quantity
FROM stock_branch_level AS stock LEFT OUTER JOIN
booked_sales AS sales ON stock.stock_item_code = sales.stock_item_code
AND sales.created_date > #DateFrom
WHERE (stock.replen_type = 1)