I need an SQL query to sum all items in an SQL between a specific data range - sql

I need an sql query that will sum the sales of all products sold, by product, within a specific date range. Column "Product" Contains all product items. Column Date contains the date the item was sold and column value contains the actual sales value. So the result should contain just 2 columns, one for the product Name and one for the value, and several rows, which will show the sales total of each product item.

Are you just looking for GROUP BY with filtering?
select product, sum(sales)
from t
where date >= #date1 and date <= #date2
group by product;

Related

Sum dates with different timestamps and picking the min date?

Beginner here. I want to have only one row for each delivery date but it is important to keep the hours and the minutes. I have the following table in Oracle (left):
As you can see there are days that a certain SKU (e.g SKU A) was delivered twice in the same day. The table on the right is the desired result. Essentially, I want to have the quantities that arrived on the 28th summed up and in the Supplier_delivery column I want to have the earliest delivery timestamp.
I need to keep the hours and the minutes otherwise I know I could achieve this by writing sth like: SELECT SKU, TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD'), SUM(QTY) FROM TABLE GROUP BY SKU , TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD')
Any ideas?
You can use MIN():
SELECT SKU, MIN(SUPPLIER_DELIVERY), SUM(QTY)
FROM TABLE
GROUP BY SKU, TRUNC(SUPPLIER_DELIVERY);
This assumes that SUPPLIER_DELIVERY is a date and does not need to be converted to one. But it would work with TO_DATE() in the GROUP BY as well.

how to convert date from rows to columns as week numbers and get the price from the highest week number

Problem: I am trying to convert date from rows to columns as week numbers and get the price from the highest week number and call it givenPrice.
expected:
See below. In your case, shopName is the row field, date is your pivot field, and price is your crosstab field. Since it appears that you are not doing anything to the crosstab values, we can just use a Max() function as a dummy because we don't need to ensure that the values in the pivot field are unique. So, I came up with this:
TRANSFORM Max(price)
SELECT shopName
FROM YourTable
GROUP BY shopName
PIVOT date;
NOTE: This query is Access SQL.
https://i.stack.imgur.com/492Lb.png

Working with dates in tsql query

I want to create a report that gives me a total for sales by month so
select customer,month(dated), sum(invtotal)
from salestable
group by customer,dated
gives me the my result but I get multiple lines returned if a customer had three orders for a particular month.
I was expecting the month(dated) to strip out the day part of the date and just return everything for a particular month as one whole, it does not appear to do that.
Any ideas on what I am doing wrong?
select customer, month(dated), sum(invtotal)
from salestable
group by customer, month(dated)

List Dimension Members if selected date falls between Start Date and End Date in fact records SSAS MDX

I have a fact table that contains invoice line items, and since these line items are subscriptions, there is a Start Date and an End Date involved
LineItem Customer Product OrderDate StartDate EndDate
1 Customer A Product A 1/1/2013 1/1/2013 3/1/2013
2 Customer A Product B 1/1/2013 1/1/2013 4/1/2013
3 Customer B Product A 1/1/2013 2/1/2013 6/1/2013
The client wants a list of Active Customers for a selected date in Excel(PivotTable). They want to select a date, and if the date falls between the Start Date and End Date of any Invoice Line Item record, then the Customer should be displayed. For example:
If '1/5/2013' is selected, the Customer List should return (LineItem: 1, 2):
Customer A
If '2/10/2013' is selected, the Customer List should return (LineItem: 1,2,3):
Customer A
Customer B
If '5/15/2013' is selected, the Customer List should return (LineItem: 3):
Customer B
Next, the client wants to filter by Products as well, so:
If '3/20/2013' is selected and Product A is selected, the Customer List should return (LineItem: 3):
Customer B
In SQL this is very easy:
Select Distinct Customer from Fact where #SelectedDate between StartDate and EndDate
I am unsure on how to approach this problem in SSAS and what to do with the 'Selected Date' as in, should this be another dimension? if so how is it going to relate to the Fact Table?
Or can this be done on Excel/PowerPivot side using in some other way?
Also my initial approach is to create a Named Set of customers - but I am not sure how to create it based on date range etc.
Any help will be appreciated!
Thanks
If you are able to write the MDX, then you can do this as follows, assuming there is a date dimension table with two foreign keys to it from the fact table, the role playing dimensions are named [Start Date] and [End Date], and #SelectedDate is a string matching the format of your date keys:
SELECT {}
ON COLUMNS,
[Customer].[Customer Name].Members
ON ROWS
FROM [Cube]
WHERE (null : StrToMember('[Start Date].[Date].[' + #SelectedDate + ']'))
*
(StrToMember('[End Date].[Date].[' + #SelectedDate + ']'): null)
The WHERE clause is a cross product of two sets: one set of start dates that contains all from the first one appearing in the dimension to the selected date, and one of end dates that contains all end dates from the selected date to the last one in the cube.
However, I do not think it is possible for users to get Excel to run this type of statement somehow, except via a VBA or Excel plugin solution. I think that should be possible, but have no experience with that.

Sql query help, grabbing dates, counts, averages

I have a table called payhistory, and from this table I need to grab the Year, Month when a payment was made. I then need to get an average of payments grouped by Year and then month.
select AVG(totalpaid) from payhistory
This table has a column called datepaid that is where I need to grab the date from. It has a column called totalpaid which is where I need to grab the average from.
I then need a count of every payment within that month.
And finally a column displaying which Year/Month it was from.
Important columns in the table:
number (account number can use this to get a count)
datepaid (contains the date the payment was entered '2009-09-28 00:00:00.000')
totalpaid (contains the specific payment for that date)
This should work:
select AVG(totalpaid),
COUNT(number),
MONTH(datepaid),
YEAR(datepaid)
from payhistory
group by
MONTH(datepaid),
YEAR(datepaid)