473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Is it possible to speed up this? SQL

Hey

Im wondering if its possible to speed up this code.

Taking ages.

Expand|Select|Wrap|Line Numbers
  1. SELECT Tabell1.Pnr, Tabell1.Handk, Tabell1.`From`, Tabell1.lon, Tabell1.Fri, Tabell1.Hsfil
  2. FROM fors Tabell1
  3. WHERE (Tabell1.Hsfil = (SELECT MAX(Tabell2.Hsfil)
  4. FROM fors Tabell2
  5. WHERE Tabell2.From <=20050101 and Tabell1.Handk <> 40 and Tabell1.Pnr = Tabell2.Pnr))
thanks
Aug 16 '10 #1

✓ answered by NeoPa

I did leave out one small, but important, clause from the subquery (Ooops!). Try this instead :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Pnr]
  2.      , Handk
  3.      , [From]
  4.      , [lon]
  5.      , [Fri]
  6.      , [Hsfil]
  7. FROM   fors AS Tabell1 INNER JOIN
  8.     (
  9.     SELECT   [Pnr]
  10.            , Max(Hsfil) AS MaxHSFil
  11.     FROM     fors
  12.     WHERE    ([From]<=20050101)
  13.     GROUP BY [Pnr]
  14.     ) AS Tabell2
  15.   ON   Tabell1.Pnr=Tabell2.Pnr
  16.  AND   Tabell1.Hsfil=Tabell2.MaxHSFil
  17. WHERE  (Tabell1.Handk<>40)

23 2596
NeoPa
32,556 Expert Mod 16PB
Does fors have some special meaning?

I cannot see how the SQL would work as the FROM clause seems to be invalid.
Aug 16 '10 #2
no, fors is an access table.
Aug 16 '10 #3
the code picks the max value of Hsfil and filter before and after this max event.
Aug 16 '10 #4
NeoPa
32,556 Expert Mod 16PB
Michael3432:
no, fors is an access table.
Of course. My memory.
Expand|Select|Wrap|Line Numbers
  1. FROM Fors Tabell1
is just another way of saying :
Expand|Select|Wrap|Line Numbers
  1. FROM Fors AS Tabell1
Let me look at this again now I understand what I'm looking at :D
Aug 16 '10 #5
NeoPa
32,556 Expert Mod 16PB
Try this. I'm not sure exactly what you're trying to do as your SQL is a little confused, but this should give the same results, only a little more quickly I would hope :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Pnr]
  2.      , Handk
  3.      , [From]
  4.      , [lon]
  5.      , [Fri]
  6.      , [Hsfil]
  7. FROM   fors AS Tabell1 INNER JOIN
  8.     (
  9.     SELECT [Pnr]
  10.          , Max(Hsfil) AS MaxHSFil
  11.     FROM   fors
  12.     WHERE  ([From]<=20050101)
  13.     ) AS Tabell2
  14.   ON   Tabell1.Pnr=Tabell2.Pnr
  15.  AND   Tabell1.Hsfil=Tabell2.MaxHSFil
  16. WHERE  (Tabell1.Handk<>40)
Aug 16 '10 #6
thanks. I will try this.
Aug 16 '10 #7
got an error. Think its this:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Pnr] , Max(Hsfil) AS MaxHSFil
  2.  
cant show both pnr (several values) and Max (one value)

help
Aug 17 '10 #8
NeoPa
32,556 Expert Mod 16PB
I did leave out one small, but important, clause from the subquery (Ooops!). Try this instead :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Pnr]
  2.      , Handk
  3.      , [From]
  4.      , [lon]
  5.      , [Fri]
  6.      , [Hsfil]
  7. FROM   fors AS Tabell1 INNER JOIN
  8.     (
  9.     SELECT   [Pnr]
  10.            , Max(Hsfil) AS MaxHSFil
  11.     FROM     fors
  12.     WHERE    ([From]<=20050101)
  13.     GROUP BY [Pnr]
  14.     ) AS Tabell2
  15.   ON   Tabell1.Pnr=Tabell2.Pnr
  16.  AND   Tabell1.Hsfil=Tabell2.MaxHSFil
  17. WHERE  (Tabell1.Handk<>40)
Aug 17 '10 #9
thanks. I will try this
Aug 17 '10 #10
I got this working and its alot faster. Thanks.

Now Im trying to change:
([From]<=20050101)

to ([From]<=?)
using a parameter, but get following error:

parameters are not allowed in queries that can't be displayed graphically

do you know any solution to this?
Aug 20 '10 #11
NeoPa
32,556 Expert Mod 16PB
I'm not clear on this Mikael.

It looks to me like a query that can be displayed. I don't know about graphically, but I've not come across that error before. Maybe ? is treated specially. Try [?].

Otherwise there are a few techniques that can be used, but which is best depends on how you are planning on using the query, which we don't know anything about as yet.
Aug 20 '10 #12
? or [?] works fine in access, but in MSQuery it dosnt work with JOIN, known error.

Im gonna have several excel sheets (each month), all using this query and it would be much easier if I can change From in the sheet as a parameter instead of going into the code and change From every time.
Aug 20 '10 #13
NeoPa
32,556 Expert Mod 16PB
It would help if you responded fully to my query Mikael, rather than dropping incidental info into the conversation and allowing me to deduce what is going on from what you let slip.

My deduction :
You are attempting to run your sql from Excel using MSQuery.

My response :
Wow. How can we have got so far without your even mentioning one of the most important pieces of information. This is an Access / VBA forum. If you're talking about non-Access SQL you need to say that up front (I'm so surprised I need to say this).

If this is the case then the technique I would use is to update the SQL string before you even submit it, to contain the actual value entered as a literal (using the correct delimiters of course). I forget the rules for MS-Query strings, but I suspect that they may require double-quotes (") instead of the single-quotes (') which are correct everywhere else.

See Quotes (') and Double-Quotes (") - Where and When to use them and Literal DateTimes and Their Delimiters (#) for other types of literals.
Aug 20 '10 #14
I dont know what you mean by non-Acess SQL.
Im using MSQuery to import an Microsoft Access table into excel.
Aug 20 '10 #15
You are using MSQuery to open anAccess databse and get the data into Excel, right?

This is sort of like using a Honda-labelled Hand Truck to lift a pallette of cargo to put into a tractor trailer, when another Honda-labelled forklift is sitting right beside it ready to go.

Do this right from Access and save yourself the needless messing around with MSQuery!

Access will do this with, mmm 1 comand?

docmd.transferspreadsheet
Aug 20 '10 #16
I dont know about docmd.transferspreadsheet

but can I use parameter with that?
Can I update with it?
Aug 21 '10 #17
NeoPa
32,556 Expert Mod 16PB
Mikael3432:
I dont know what you mean by non-Acess SQL.
Im using MSQuery to import an Microsoft Access table into excel.
The syntax rules for using SQL from MSQuery are not the same as for running SQL natively in Access. The Nz() function is an example of something available only when running Jet-SQL in Access. It won't work from Excel (or MSQuery).
Aug 21 '10 #18
NeoPa
32,556 Expert Mod 16PB
MBurns08109:
This is sort of like using a Honda-labelled Hand Truck to lift a pallette of cargo to put into a tractor trailer, when another Honda-labelled forklift is sitting right beside it ready to go.
I'm not sure I'm with you on your simile.

DoCmd.TransferSpreadsheet() is certainly available, and may well prove preferable in this case (I don't know enough about the OP's requirements to know), but there are certainly pros and cons to each approach. Sometimes using MSQuery is the most appropriate approach. It depends on many factors. MSQuery can be a very powerful and useful tool for dealing with data from within Excel. You have the added bonus of formatting the data more easily without needing to use Application Automation (which is often a very important consideration).
Aug 21 '10 #19
NeoPa
32,556 Expert Mod 16PB
Mikael3432:
Can I update with it?
You can send data to the spreadsheet. You may be able to append to existing data, but I suspect not. I think it replaces any current data but you'd need to check to confirm that. I'm pretty sure you cannot use it as you might an UPDATE query and change data within pre-existing rows.
Aug 21 '10 #20
I have tried using export in access. Sure it works fine with 1 or 2 excel files and you can save the export.
But if you have alot of excel files, it takes time to do it. Especially when you need to type in different parameters each time. What is the best way to solve this?
Aug 24 '10 #21
NeoPa
32,556 Expert Mod 16PB
Solve what?

What are you asking?
Aug 24 '10 #22
what is the best method. export from access or import from excel? Given that you need to do that to several excel files.
Aug 24 '10 #23
NeoPa
32,556 Expert Mod 16PB
If you're asking that as an unrelated question, I would say that exporting from Access would be my preferred route.

However, this works best when dealing with straightforward transfers of data. Depending on your requirements beyond that (Merging with existing data; formatting so the data looks nice and is easier for operators to work with), there may be Excel specific work to be done. This is more easily done from within Excel, but can be done from the Access process itself using Application Automation.

Many transfers certainly points towards an Excel approach, but as I say, there may be other factors involved for your requirements.
Aug 25 '10 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: MetalOne | last post by:
def buildBitmap(rawData, w, h, maxColorVal): """ppm format, http://netpbm.sourceforge.net/doc/ppm.html Constructs a wxBitmap. The <rawData> input is a raw grayscale image, 8 bits per pixel."""...
11
by: grv | last post by:
So it is supposed to be very fast to have an array of say 5 million integers stored in a binary file and do a = numarray.fromfile('filename', (2, 2, 2)) numarray.add(a, 9, a) but how is that...
6
by: floortje | last post by:
Hi, I got a forum on my website .... it;s located in 2 databases: 1 for the topic names and one for the text and user info: the first has 20371 entries ... the second 2124 CREATE TABLE...
2
by: Roy Gourgi | last post by:
Hi, My program seems to slow down drastically because as I fill my array and table with many values, the program suffers tremendously. The first thing my program does is to search the jagged...
7
by: Peter | last post by:
I have the following code: _transColl is a collection of classes and it contains about 42000 objects My question is there any way I can make this code run faster? I have found out that line 2 is...
15
by: Chris | last post by:
This is just some dummy code to mimic what's being done in the real code. The actual code is python which is used as a scripting language in a third party app. The data structure returned by the...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
17
by: garrickp | last post by:
While creating a log parser for fairly large logs, we have run into an issue where the time to process was relatively unacceptable (upwards of 5 minutes for 1-2 million lines of logs). In contrast,...
7
rajiv07
by: rajiv07 | last post by:
select trackinfo.title, trackinfo.artist, trackinfo.trackid, trackinfo.duration, trackinfo.price, trackinfo.lyrisist, albuminfo.title from trackinfo, albuminfo where...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.