A simple question

David_Skov

Active Member
Joined
Jun 29, 2004
Messages
267
I have a table with 100k+ rows. In one coloumn I have an indication of the period of the year (i.e. 200309 = sept. 2003) each period are represented several times.

I need to retrieve the quarters of each year. So for that I use the coloumn with periods in a query. I then sum by totals and through a calculation I get the quarters. Here is the SQL view:

INSERT INTO Kvartal ( Periode, Kvartal )
SELECT Data.Periode, IIf(Right([Periode],2)<4,1,IIf(Right([Periode],2)>3 And Right([Periode],2)<7,2,IIf(Right([Periode],2)>6 And Right([Periode],2)<10,3,4))) AS Kvartal
FROM Data
GROUP BY Data.Periode, IIf(Right([Periode],2)<4,1,IIf(Right([Periode],2)>3 And Right([Periode],2)<7,2,IIf(Right([Periode],2)>6 And Right([Periode],2)<10,3,4)))
HAVING (((IIf(Right([Periode],2)<4,1,IIf(Right([Periode],2)>3 And Right([Periode],2)<7,2,IIf(Right([Periode],2)>6 And Right([Periode],2)<10,3,4))))<=[Choose quarter (ie 3):]));

Problem is that it's so **** slow since it has to retrieve all that data, create totals and then do the calculations. So it's not a wonder it's slow.

But is there an easier way to do this? I hope there is :p

In advance tx :)
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi David, I presume that [Periode] is indexed but I don't think that will be the problem. I suspect is will be the Right(... bits of the formula that are slowing it down. I can see why you are doing and what the effect is but I wonder if you should have a new field in your table for the quarter and use that field instead of calculating it in the query, e.g.

Period Quarter

200309 3
200310 4
200311 4
200312 4
200401 1
etc.

BUT before you do this, what about creating a new table that looks like this instead?

Period Quarter Year

200309 3 2003
200310 4 2003
200311 4 2003
200312 4 2003
200401 1 2004
etc.

Then you could link this table of periods and quarters to your main data table (through the period field) and then use the periods / quarters from this table in your query. This would be a more efficient way of doing it. It will however require some modification to your query.

There are a couple of options there but I'm pretty sure it will be the calculations within the query that is slowing it down.

HTH, Andrew. :)
 
Upvote 0
Tx Andrew, I found a better way:

INSERT INTO Kvartal ( Periode, Kvartal )
SELECT Data.Periode, Int(Right(([Periode]-1),2)/3)+1 AS Kvartal
FROM Data
GROUP BY Data.Periode, Int(Right(([Periode]-1),2)/3)+1
HAVING (((Int(Right(([Periode]-1),2)/3)+1)<=[Choose quarter (ie 3):]));

works like a charm :)
 
Upvote 0

Forum statistics

Threads
1,221,828
Messages
6,162,213
Members
451,752
Latest member
freddocp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top