Graph of top four products each month for last 6 months?
(Yeah I know I typ'od the title)
I need create a chart which displays the four highest selling products for each month in the last 6 months to be displayed in a report.
Ive managed to get all the products for each month for the last six months but im unsure how to filter so it displays only the top four for each month.
Ive done some programming but I thiinnk im going about this wrongly.
This is my code so far -
DECLARE @StartDate SMALLDATETIME -- Variables for the dashboard
-
DECLARE @EndDate SMALLDATETIME
-
-
SET @StartDate = DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-6,0)) -- get todays month minus 6
-
SET @EndDate = GETDATE() -- get todays date
-
-
----
-
-
DECLARE @WhereDateStart SMALLDATETIME
-
DECLARE @WhereDateEnd SMALLDATETIME
-
-
SET @WhereDateStart = CONVERT(SMALLDATETIME,'01/' + LTRIM(STR(MONTH(@StartDate))) + '/' + LTRIM(STR(YEAR(@StartDate))))
-
-
SET @WhereDateEnd = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@EndDate)+1,0))-1
-
-- we want each date/month minus six months for filter
-
-
SELECT -- Set up columns and graph output.
-
-
LEFT(Products.ProductId,14) AS ChartLabel,
-
Products.ProductId,
-
CONVERT(INT, SUM(ManufacturingOrders.QuantityMade)) AS QuantityMade
-
,
-
CONVERT(CHAR(10),
-
LTRIM(STR(YEAR(ManufacturingOrders.ReleaseDate))) + '/' +
-
LTRIM(STR(MONTH(ManufacturingOrders.ReleaseDate)))) AS ReleaseDate -- trim dates
-
FROM
-
ManufacturingOrders
-
INNER JOIN Products ON ManufacturingOrders.Product = Products.Product -- Get the product type
-
INNER JOIN Classifications ON Products.Classification = Classifications.Classification -- Get the Product number
-
WHERE
-
-
Products.ProductId IN (
-
-
SELECT Products.ProductId
-
-- sales code
-
FROM Products INNER JOIN Classifications ON Products.Classification = Classifications.Classification
-
-
WHERE Classifications.ClassificationId = 'BOOK'
-
)
-
-
AND ManufacturingOrders.ReleaseDate BETWEEN @WhereDateStart AND @WhereDateEnd
-
AND ManufacturingOrders.SystemType = 'B'
-
AND QuantityMade > 0
-
-
GROUP BY
-
-
Products.ProductId
-
,
-
YEAR(ManufacturingOrders.ReleaseDate)
-
,
-
MONTH(ManufacturingOrders.ReleaseDate)
-
ORDER BY
-
ReleaseDate
-
,
-
ProductId
-
This produces ChartLabel,Prod uctId,QunatityM ade,ReleaseDate columns.
but its everything for each month, I just want the top 4 for each month to be sent to the graph.
Aug 13 '10
12 2986 NeoPa 32,571
Recognized Expert Moderator MVP MicroBlitz:
Yes that makes a lot more sense as soon as you mentioned the "AS" keyword.
I so agree. It's an optional keyword and I always use it myself as I feel clarity is so important, especially when reading someone-else's code.
Generally, I find that where subqueries become nested and complicated it is worthwhile to develop each in turn, starting from the innermost and proceeding to the overall. Clearly it's important to have a good understanding of what precisely is required at each level, but once the innermost one is developed and working you can think of it in its own little black box (within parentheses) and know that it does its job. The last SQL of post #9 for instance, is easy to work on if you know you can rely on (QueryZ) to do all that's required of it. MicroBlitz:
I guess it doesnt help to only have been supplied with one book which is Sams teach yourself Microsoft SQL Server T-SQL.
Any suggestions for a quick rummage in amazon.co.uk for a better book than this. It is ... well erm.. brief.. :)
Not really, but I'm finding out all I need from the Context-Sensitive Help system. I find it is invaluable to me. I'm in a position not far different from yours in fact. I may well have a few years of Jet SQL experience, but T-SQL I'm picking up as I go along (after dipping in to T-SQL 2000 at a very basic level once or twice over the last few years). Not a day goes by at this time without my nose deeply buried in the Help system. It answers almost all of my questions well. If it weren't an electronic resource it would certainly be well thumbed by now, and I've only been even looking at this in the last month or two.
Anyway, good luck with your learning. Whichever way you find works best.
This is the final version I added to the code. It's not the same method as the suggestion above but I managed to get that way working after I got the report to function properly. If I can find the method detailed here I will post it for comparison - Microblitz -
DECLARE @Classification bigint
-
DECLARE @StartDate smalldatetime
-
DECLARE @EndDate smalldatetime
-
DECLARE @MonthEndDate smalldatetime
-
DECLARE @TempDate smalldatetime
-
-
SET @Classification = (SELECT Classification FROM Classifications WHERE Classifications.ClassificationId = 'STOCKITEM')
-
SET @StartDate = CONVERT(smalldatetime,'01 ' + RIGHT(CONVERT(nvarchar,getdate(),106),8))
-
SET @EndDate = DATEADD(d,-1,@StartDate)
-
SET @StartDate = DATEADD(m,-6,@StartDate)
-
-
CREATE TABLE #DETAILS
-
(
-
SalesYear int,
-
SalesMonth int,
-
ProductId nvarchar(100),
-
Quantity decimal(17,5)
-
)
-
-
SET @TempDate = @StartDate
-
-
WHILE @TempDate < @EndDate
-
BEGIN
-
SET @MonthEndDate = DATEADD(d,-1,DATEADD(m,1,@TempDate))
-
-
-
INSERT INTO #DETAILS
-
(
-
SalesYear,
-
SalesMonth,
-
ProductId,
-
Quantity
-
)
-
SELECT TOP 4
-
YEAR(@TempDate),
-
DATEPART(m,@TempDate),
-
Products.ProductId,
-
SUM(SalesInvoiceItems.Quantity) AS QuantityInvoiced
-
FROM SalesInvoiceItems
-
INNER JOIN SalesInvoices ON SalesInvoiceItems.SalesInvoice = SalesInvoices.SalesInvoice
-
AND dbo.wfn_GetSimpleDate(SalesInvoices.EffectiveDate) BETWEEN @TempDate AND @MonthEndDate
-
AND SalesInvoices.SystemType = 'F'
-
AND SalesInvoices.SourceType = 'I'
-
INNER JOIN Products ON SalesInvoiceItems.Product = Products.Product
-
AND Products.Classification = @Classification
-
GROUP BY
-
Products.ProductId
-
ORDER BY
-
QuantityInvoiced DESC
-
-
-
SET @TempDate = DATEADD(m,1,@TempDate)
-
END
-
-
SELECT *
-
FROM #DETAILS
-
-
DROP TABLE #DETAILS
-
The alternative code using the technique as explained above by ck9663 for comparison. -
DECLARE @Classification bigint
-
SET @Classification = (SELECT Classification FROM Classifications WHERE Classifications.ClassificationId = 'STOCKITEM')
-
-
select * from
-
(
-
select
-
Dtbl.SalesYear
-
Dtbl.SalesMonth,
-
Dtbl.ProductId,
-
row_number() over (partition by SalesYear,SalesMonth order by QuantityInvoiced desc) AS Ranking,
-
Dtbl.QuantityInvoiced
-
from
-
(
-
SELECT
-
YEAR(SalesInvoices.EffectiveDate) AS SalesYear,
-
DATEPART(m,SalesInvoices.EffectiveDate) AS SalesMonth,
-
Products.ProductId,
-
SUM(SalesInvoiceItems.Quantity) AS QuantityInvoiced
-
FROM SalesInvoiceItems
-
INNER JOIN SalesInvoices ON SalesInvoiceItems.SalesInvoice = SalesInvoices.SalesInvoice
-
AND dbo.wfn_GetSimpleDate(SalesInvoices.EffectiveDate) BETWEEN '01 Mar 2010' AND '31 Aug 2010'
-
AND SalesInvoices.SystemType = 'F'
-
AND SalesInvoices.SourceType = 'I'
-
INNER JOIN Products ON SalesInvoiceItems.Product = Products.Product
-
AND Products.Classification = @Classification
-
GROUP BY
-
YEAR(SalesInvoices.EffectiveDate),
-
DATEPART(m,SalesInvoices.EffectiveDate),
-
Products.ProductId
-
) AS Dtbl
-
) As DTbl2
-
where Ranking <= 4
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: sandy |
last post by:
Hi,
Using Java script I am trying to create code where when you place in
the start date it automatically calculates 6 months for the
experations date. For example when I place 01/01/04 as the issue date
the experation date should automaically generate as 06/01/04. I would
appreciate it if anyone could help me.
Thank you
Sandy
|
by: Finlay |
last post by:
Hi Group
I am designing a report that should show a meter reading for each
month and the previous meter reading for the previous month. The
months are text stored in a field tMonth.
The current month is easy to pull out of the data base but I don't
know how to get the previous months meter reading.
The previous reading is subtracted from the current reading to get the
|
by: Robin Tucker |
last post by:
Hiya,
I need to test "relative dates" in my program, such as "last six months" or
"last 3 months" or "in the last week" etc. How can I do this with a
DateTime structure?
ie. If NodeDate >= CurrentDate - six months (etc.)
|
by: remya1000 |
last post by:
i'm using ASP with MSAccess as database.
i have two buttons and two textbox in my page.
when i press my first button (First month) i need to display the current month in one textbox and last one months date in another textbox.
and while pressing the second button (submit) i need to display the date displayed in the textbox as itself and the records between thoses two dates. if that date is still there in the textbox only i can sort the...
|
by: nsymiakakis |
last post by:
Hello Everyone, I am almost completed with this database, the only thing left is to graph the results of a query. I built a parameter query that gives me totals for a date range that I enter. This works great. I created a column called "Month", and I am entering the month and year corresponding to the "Date" field. ie. (03/15/06 for the date) (Mar-06) for Month. If I do the Query to show me 1/1/2006 to 12/31/2006, I get the results with Date...
| |
by: Johan Mcgillicutty |
last post by:
Could use some help on this one-
I’m new to MS Access, and even newer to SQL. I’m building a database for clients of a treatment center, and need to keep track of when people are due for their annual physical. As near as I can tell, the simplest way to do this is to simply record when the physical was, and build a query to compile a list of all the entries from the current month, one year ago. This would enable a report to be created with...
|
by: rekhasc |
last post by:
hi all...............
in my proj i am using a mschart ....
now what i want is i want to display the graph amount v/s months
I did the graph which shows date v/s amount its working good...
i used the sql query for that.... for month also i wrotequery but its not working properly....
what i need is it should add all the amounts which are entered in the particular dates on that month then it show the graph...
plz if u know that send me...
|
by: Harlin Seritt |
last post by:
Is there a module that can pull str values for say the last 3 months?
Something like:
print lastMonths(3)
Thanks
|
by: Supermansteel |
last post by:
I have a Query/Report that I am running a Date Parameter so it only pulls in the information for the End of Month Results for that person. It runs the Avg for everyone for that month also. I was trying to figure out a way to pull in there results from the following month. I found DateSerial(Year(Date),Month(Date)-1,1) that pulled in last month, but how do I pull in Last month Production, calls/hour etc on how the individual did. Is there a way...
|
by: mikewin86 |
last post by:
Hello ,
I would like to ask a question concerned with SQL Server 2000 query.
I've got a table PurchaseDetails and I would like to query from it.
I want all product records with last purchase ID. The following is my query statements.
SELECT MAX(PurchaseID) AS LastID, ProductID, BatchNo
FROM PurchaseDetails
GROUP BY ProductID, BatchNo
When I run this query statements , I got all products and all records (i.e. not only last...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |