I have a table with the TotalPrice column and BookedOn column which contains a date in which a particular order was placed. I want to generate a report in SSRS that will show the date on which the highest sales were made.
There are several orders made on a particular date and I want to sum all the price and then compare in which date highest sales were made.
select top 1 CONVERT(date, BookedOn) Date, sum(TotalPrice) SumTotalPrice
from your_table_name
group by BookedOn
order by SumTotalPrice desc
I used convert because didn't know the format of your date column(whether it is datetime or date), you can use just date column otherwise
Related
I am working with some sales data and pulling the metrics for a particular week I am defining in the filter.
However, I want to add another column (first_sale_date) to my query. This will show the first time this asin/mp combo shows up in my table regardless of the date filter I am trying to pull the other metrics for.
Because I am already
filtering by date I don't know how to look back to all of the data in the table to find it's first appearance as it is before the week I am filtering for.
select date,
,asin
,marketplace
,SUM(ordered_product_sales) as OPS
,SUM(cogs) as cogs
**,min(date) as first_sale_date**
from prod.sales
where date > '2023-01-01'
group by 1,2,3,4
you can use a correlated subquery for this
select year
,week
,asin
,marketplace
,SUM(ordered_product_sales) as OPS
,SUM(cogs) as cogs
,( SELECT MIN(date) FROM sales sal1 WHERe sal1.asin = sal.asin and sal1.marketplace = sal.marketplace) as first_sale_date
from prod.sales sal
where year = '2023' and week = '1'
group by 1,2,3,4
I am trying to calculate a weekly average, and add it as a new column along side what I already have. I have added week starting dates and week ending dates as I thought that would be useful (and how I found do it in a more familiar MS Excel environment).
SELECT
settlementdate as Trading_Interval,
rrp as Trading_Interval_QLD_RRP,
NEXT_DAY(TRUNC(settlementdate-7),'MONDAY')AS Week_Starting,
NEXT_DAY(TRUNC(settlementdate),'SUNDAY')AS Week_Ending
FROM fullauth.tradingprice
WHERE settlementdate > to_date('31/12/2019','dd/mm/yyyy')
AND settlementdate <= to_date('01/01/2020','dd/mm/yyyy')
AND regionid = 'QLD1'
ORDER BY settlementdate DESC;
This code returns a table with 4 columns. I want to find the average price of the Trading_Interval_QLD_RRP when the Trading_Interval falls between the Week_Starting and Week_Ending dates and insert/create this weekly price as a new column
Thanks!
You can use AVG as an analytical function as following:
SELECT
settlementdate as Trading_Interval,
rrp as Trading_Interval_QLD_RRP,
NEXT_DAY(TRUNC(settlementdate-7),'MONDAY')AS Week_Starting,
NEXT_DAY(TRUNC(settlementdate),'SUNDAY')AS Week_Ending,
-- required changes as following
AVG(Trading_Interval_QLD_RRP) OVER (PARTITION BY TRUNC(settlementdate,'IW')) AS AVG_RRP
FROM fullauth.tradingprice
WHERE settlementdate > to_date('31/12/2019','dd/mm/yyyy')
AND settlementdate <= to_date('01/01/2020','dd/mm/yyyy')
AND regionid = 'QLD1'
ORDER BY settlementdate DESC;
Also, Note that TRUNC(settlementdate,'IW') and NEXT_DAY(TRUNC(settlementdate-7),'MONDAY') are same.
Cheers!!
In my SSRS report there are 4 parameters StartDate, EndDate, MeterId, & DisplayBy
Start Date: datetime datatype
EndDate : datetime datatype
MeterId : is a drop down list and this will populate based on SQL query
DisplayBy: is a drop down list and this has the following values (Hour,day,Month & year)
The Database that stores hourly values for Meters, the following are the DB table columns: (MeterId,ReadingDate,Hours,Quantity,Price)
When I select the startdate, end date and the meter Id and display i want to show report based on the startdate & enddate and then by display values.
If the display is hour, the we got display all the 24 hour values for the MeterId,Quantity, Price for the date range.
If the display is day, we got display total quantity and total price for the MeterId for that date range.
If the display is Month, we got display total quantity and total price for the MeterId for that date range.
If the display is Year, we got display total quantity and total price for the MeterId for that date range. Say for example If i select start date as 1-1-2016 and end date as 12-31-2016. My result should show 12 rows for each month with their total Quantity, Total price for that particular MeterID.
my DB table stores all the hourly values i know how to show the values on screen if the user selects the display dropdown as hour. But, dont know how to show the result for day/month/year or how to group it. Do I need to use "case" statement and if so what should i need to give on display parameters.
Please suggest your idea...
Row Grouping:
SELECT I.CustomerName, I.ReadingDate, I.IntegratedHour, I.IntegratedUsage, I.IntegratedGeneration, DATEPART(dd, I.ReadingDate) AS [Reading Day], DATEPART(mm,
I.ReadingDate) AS [Reading Month], DATEPART(yyyy, I.ReadingDate) AS [Reading Year]
FROM IntegratedHour_MV90 AS I INNER JOIN
CustRptMeterExtract AS CT ON CT.CustomerName = I.CustomerName
WHERE (I.ReadingDate >= #StartDate) AND (I.ReadingDate <= #EndDate) AND (I.CustomerName IN (#FacilityName))
Expected Result:
SSRS Current Output: Doesnot match
Depending on your layout you could set row grouping to an expression something like this
=SWITCH
(
Parameters!ReportBy.Value=1, Fields!Hour.Value,
Parameters!ReportBy.Value=2, Fields!Day.Value,
Parameters!ReportBy.Value=3, Fields!Month.Value,
Parameters!ReportBy.Value=4, Fields!Year.Value,
True, 0)
This assumes you have already have the hours/days/months/years in your dataset, if not then you would have to replace the field references with expressions to return the relevant month etc.
Based on what I can see above you'll need to add a grouping level for Customer before the group expression. Also, you Quantity expression should be a sum something like this
=SUM(FIelds!IntegratedGeneration.Value)
You may still have a problem though. I'm assuming Price is a unit price, so it does not make sense to sum that too. To get round this, you should calculate the LineValue (Qty * Price) in your dataset then change the price expression to be something like
=(SUM(FIelds!LineValue.Value)/SUM(Fields!IntegratedGeneration.Value))
and this will give you the average price.
However, this may be slow and personally I would do the work in your dataset. Again assuming you have the months, years in your table then you could do something like this.
--DECLARE #ReportBy int = 1 -- uncomment for testing
select
MeterID, Price
, CASE #ReportBy
WHEN 1 THEN [Month]
WHEN 2 THEN [Year]
ELSE NULL
END AS GroupByColumn
INTO #t
from dbo.MyDataTable
SELECT
GroupByColumn
, SUM(Price) as Price
FROM #t
Group BY GroupByColumn
Order by GroupByColumn
This assumes your report parameter is called ReportBy, if not just swap the name out.
So I have created a table that has the following columns from a transaction table with all customer purchase records:
1. Month-Year, 2.Customer ID, 3. Number of Transactions in that month.
I'm trying to create a table that has the output of
1. Month-Year, 2. Number of active customers defined by having at least 1 purchase in the previous year.
The code that I have currently is this but the case when obviously only capturing one date and the where clause isn't dynamic. Would really appreciate your help.
select month_start_date, cust_ID,
(case when month_start_Date between date and add_months(date, -12) then count(cust_ID) else 0 end) as active
from myserver.mytable
where
month_start_Date>add_months(month_start_date,-12)
group by 1,2
EDIT: I'm just trying to put a flag next to a customer if they are active in each month defined as having at least one transaction in the last year thanks!
You might use Teradata's proprietary EXPAND ON synax for creating time series:
SELECT month_start_date, COUNT(*)
FROM
( -- create one row for every month within the next year
-- after a customer's transaction
SELECT DISTINCT
BEGIN(pd) AS month_start_date,
cust_ID
FROM myserver.mytable
EXPAND ON PERIOD(month_start_date, ADD_MONTHS(month_start_date,12)) AS pd
BY ANCHOR MONTH_BEGIN -- every 1st of month
FOR PERIOD (DATE - 500, DATE) -- use this to restrict to a specific date range
) AS dt
GROUP BY month_start_date
ORDER BY month_start_date
I have a database table where there is a field- billed_amount which keeps the records of the billed amount for a particular person and another field- billing_date. Now,I want to display the total billed amount for all people for a particular day, for example today, to generate day to day sales report.
For a particular day you could run the following, changing the date to whatever date you're running it for.
select sum(billed_amount)
from tbl
where billing_date = '2014-07-19'
Note that each database varies with its default date format. (you didn't specify a database)
To get the total for each date ("grouped by" date), you can use the following:
select billing_date, sum(billed_amount)
from tbl
group by billing_date
order by billing_date
You can group by day, and sum the amounts billed:
select cast(billing_date as date)
, sum(billing_amount)
from YourTable
group by
cast(billing_date as date)
Date operations vary by database. In SQL Server 2008+, you can cast a datetime to the date type to strip off the time.