I want to show how many orders have been shipped 10 days before the required date in Access SQL, but I can't seem to get the syntax right.
SELECT COUNT(*)
FROM Orders
WHERE DATEDIFF(day,RequiredDate,ShippedDate)=10;
SELECT Count(*)
FROM Orders
WHERE (((DateDiff("d",[Orders].[RequiredDate],[Orders].[ShippedDate]))=10));
Related
trying to work the below on big query. can anyone tell me where i am going wrong.
also the date is in timestamp
SELECT (COUNT(Items_purchased])) / (SELECT COUNT(Order_id) FROM table where Order_id IS NOT NULL) OVER (ORDER BY Order_date)
FROM table
GROUP BY Items_purchased, Order_date
tried using the avg function itself, but bq has been throwing errors even when checking for avg no of orders paced per item.
I have a sales data table with cust_ids and their transaction dates.
I want to create a table that stores, for every customer, their cust_id, their last purchased date (on the basis of transaction dates) and the count of times they have purchased.
I wrote this code:
SELECT
cust_xref_id, txn_ts,
DENSE_RANK() OVER (PARTITION BY cust_xref_id ORDER BY CAST(txn_ts as timestamp) DESC) AS rank,
COUNT(txn_ts)
FROM
sales_data_table
But I understand that the above code would give an output like this (attached example picture)
How do I modify the code to get an output like :
I am a beginner in SQL queries and would really appreciate any help! :)
This would be an aggregation query which changes the table key from (customer_id, date) to (customer_id)
SELECT
cust_xref_id,
MAX(txn_ts) as last_purchase_date,
COUNT(txn_ts) as count_purchase_dates
FROM
sales_data_table
GROUP BY
cust_xref_id
You are looking for last purchase date and count of distinct transaction dates ( like if a person buys twice, it should be considered as one single time).
Although you mentioned you want count of dates but sample data shows you want count of distinct dates - customer 284214 transacted 9 times but distinct will give you 7.
So, here is the SQL you can use to get your result.
SELECT
cust_xref_id,
MAX(txn_ts) as last_purchase_date,
COUNT(distinct txn_ts) as count_purchase_dates -- Pls note distinct will count distinct dates
FROM sales_data_table
GROUP BY 1
I have a table with the following columns:
Inventory
ID,
SerialComputer,
RegistrationDate,
Deadline
I want to make a query in which the first column shows me all the dates on which computers have been delivered, and next to the amount of computers that were delivered on that date, how can I make that possible in SQL Server?
I know I get the dates this way:
SELECT DISTINCT Deadline
FROM Inventory
ORDER BY Deadline
How do I add the COUNT () with the SerialComputer column?
Do aggregation using GROUP BY clause :
SELECT Deadline, COUNT(*) AS [# computers delivered]
FROM Inventory
GROUP BY Deadline;
DISTINCT will remove duplicate values so, that will not help you.
Presumably, deadline is the delivery date. If so, you want aggregation:
SELECT Deadline, COUNT(*)
FROM Inventory
GROUP BY Deadline
ORDER BY Deadline
I am trying to query on which day of the week most orders occur. I have written a query that returns a result. But, the result is, I believe, the day of the week possibly based on most orders, but the day for every order, so there are dozens. I am wondering how to only return the one day.
The table is:
Orders
OrderID
OrderDate
SELECT MAX(DAYNAME(OrderDate)) as WeekDay
FROM Orders O
GROUP BY OrderDate
OrDER BY WeekDay DESC;
The query you are trying to write is:
SELECT TOP (1) DAYNAME(OrderDate) as WeekDay, COUNT(*)
FROM Orders O
GROUP BY DAYNAME(OrderDate)
OrDER BY COUNT(*) DESC;
Although this query appears to answer your question, you have to be very careful. After all, if Orders had only one day's worth of data, then that day of the week would predominate. My suggestion would be to take data from a specified number of weeks -- preferably with no holidays -- to get this information.
Your question doesn't specify the database (although I could guess). The specific date/time logic for this depends on the database.
This will let you know how many orders were placed on which day.
SELECT
DAYNAME((OrderDate)) AS dayofweek, COUNT(OrderID) AS ordered
FROM
Orders
GROUP BY dayofweek
ORDER BY ordered DESC;
I have a table users that lists all users who have entered orders in to the system.
Each order has a order_number and user amongst a ton of other columns.
Each week I am looking to get a list of the total orders that each user has entered in to the system, I guess this will need a subquery. I have looked at grouping and subquery but am really lost.
The idea is to SELECT a count of orders entered that week, entry_date between sysdate and sysdate-5 etc. which I don't have a problem doing, but I don't understand how to then count it per user.
So for e.g. Jane entered 150 orders, Steve entered 450 orders etc.
Can someone point me in the right direction here please?
I don't really think you need a sub-query. Won't a GROUP BY solve your problem?
SELECT USER, COUNT(*)
FROM ORDERS
WHERE ENTRY_DATE BETWEEN SYSDATE - 5 AND SYSDATE
GROUP BY USER
Use GROUP BY. I made up some column names.
SELECT u.user, COUNT(*) FROM orders o, users u
WHERE o.user = u.user AND o.entry_date BETWEEN o.sysdate - 5 AND o.sysdate
GROUP BY u.user