SQL Not Working - "At Most One Record Can Be Returned by this Subq

What is wrong with this SQL?  I am trying to use a Median function and trying 
to have the median calculate for each JobCode.

SELECT tEmployeeMasterCopy.JobCode, (SELECT 
Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1 
WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
FROM tEmployeeMasterCopy
GROUP BY tEmployeeMasterCopy.JobCode;

Thanks in advance.
0
Utf
12/28/2009 6:26:01 PM
access.modulesdaovba 1670 articles. 0 followers. Follow

6 Replies
820 Views

Similar Articles

[PageSpeed] 0

It seems that

(SELECT
Median("tEmployeeMasterCopy","HourlyRate")
FROM tEmployeeMasterCopy as M1
WHERE M1.JobCode = tEmployeeMasterCopy.JobCode)


returns more than one record.  Is there only ONE record per JobCode?



Vanderghast, Access MVP




"ScottMsp" <ScottMsp@discussions.microsoft.com> wrote in message 
news:7B6FD707-8B94-4CA7-8822-CE5F083DB84F@microsoft.com...
> What is wrong with this SQL?  I am trying to use a Median function and 
> trying
> to have the median calculate for each JobCode.
>
> SELECT tEmployeeMasterCopy.JobCode, (SELECT
> Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1
> WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
> FROM tEmployeeMasterCopy
> GROUP BY tEmployeeMasterCopy.JobCode;
>
> Thanks in advance. 

0
vanderghast
12/28/2009 6:59:12 PM
Vanderghast,

There is more than one record per job code. 

20,000 records and approximately 1,800 different job codes.

What am I doing wrong?

Thanks in advance.

"vanderghast" wrote:

> It seems that
> 
> (SELECT
> Median("tEmployeeMasterCopy","HourlyRate")
> FROM tEmployeeMasterCopy as M1
> WHERE M1.JobCode = tEmployeeMasterCopy.JobCode)
> 
> 
> returns more than one record.  Is there only ONE record per JobCode?
> 
> 
> 
> Vanderghast, Access MVP
> 
> 
> 
> 
> "ScottMsp" <ScottMsp@discussions.microsoft.com> wrote in message 
> news:7B6FD707-8B94-4CA7-8822-CE5F083DB84F@microsoft.com...
> > What is wrong with this SQL?  I am trying to use a Median function and 
> > trying
> > to have the median calculate for each JobCode.
> >
> > SELECT tEmployeeMasterCopy.JobCode, (SELECT
> > Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1
> > WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
> > FROM tEmployeeMasterCopy
> > GROUP BY tEmployeeMasterCopy.JobCode;
> >
> > Thanks in advance. 
> 
0
Utf
12/28/2009 8:41:02 PM
What results or error are you actually getting? The original post omitted 
that.

"ScottMsp" <ScottMsp@discussions.microsoft.com> wrote in message 
news:7B6FD707-8B94-4CA7-8822-CE5F083DB84F@microsoft.com...
> What is wrong with this SQL?  I am trying to use a Median function and 
> trying
> to have the median calculate for each JobCode.
>
> SELECT tEmployeeMasterCopy.JobCode, (SELECT
> Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1
> WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
> FROM tEmployeeMasterCopy
> GROUP BY tEmployeeMasterCopy.JobCode;
>
> Thanks in advance. 


0
David
12/28/2009 9:52:52 PM
Hi Scott,

     What are the possible parameters for your Median() function?  How about
you post its declaration:

Public Function Median(??????) as ??????

               Clifford Bass

ScottMsp wrote:
>What is wrong with this SQL?  I am trying to use a Median function and trying 
>to have the median calculate for each JobCode.
>
>SELECT tEmployeeMasterCopy.JobCode, (SELECT 
>Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1 
>WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
>FROM tEmployeeMasterCopy
>GROUP BY tEmployeeMasterCopy.JobCode;
>
>Thanks in advance.

-- 
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/access-modules/200912/1

0
Clifford
12/28/2009 11:12:43 PM
Since the expression returns a 'table',  the outermost statement is illegal. 
Remember that a field can contain only ONE value (one row, one column), or 
what we can call 'a scalar', but your actual result want to look like:

JobCode        MedianByJobCode                             ' fields
------------    -----------------------------------
Code1        ??? --- need a table here --- ???
Code2        ??? --- need a table here --- ???
....                    ...


and you 'need a table here'  since the (SELECT ... )  returns... many rows 
(one column), which is NOT a scalar, not something a field can contain.


Sure, all the values (in the many rows)  are all the same, right? ***IF*** 
So, you can pick up just ONE of them.

Instead of:

(SELECT Median("tEmployeeMasterCopy","HourlyRate")
 FROM tEmployeeMasterCopy as M1
 WHERE M1.JobCode = tEmployeeMasterCopy.JobCode)

you can use:

(SELECT FIRST(Median("tEmployeeMasterCopy","HourlyRate"))
 FROM tEmployeeMasterCopy as M1
 WHERE M1.JobCode = tEmployeeMasterCopy.JobCode)



or MIN, or MAX, or LAST, even AVG if you want, in this case (if my 
assumption is right).


That would work, but will be excessively slow. Further more, even if you use 
just one row from your  (SELECT ... ) , your program is still computing ALL 
these rows, over and over, before eliminating them all, but one. That is a 
hint that something very inefficient occurs.

Indeed, maybe your Median procedure should accept a third argument, the 
JobCode, and your query would become:



SELECT JobCode,
        Median("tEmployeeMasterCopy","HourlyRate", "JobCode=" & JobCode)
                 AS MedianByJobCode

FROM tEmployeeMasterCopy

GROUP BY JobCode;


Note that if JobCode is ALPHA numerical, rather than being numerical, the 
syntax is likely to be:


    Median("tEmployeeMasterCopy","HourlyRate", "JobCode=""" & JobCode & 
"""" )



Assuming your function Median will see the third argument as DLookup, or 
DCount, or DSum, DMax, ... would see it.



Vanderghast, Access MVP



"ScottMsp" <ScottMsp@discussions.microsoft.com> wrote in message 
news:C5DC5486-6B24-4210-BC7F-BA254CF710D3@microsoft.com...
> Vanderghast,
>
> There is more than one record per job code.
>
> 20,000 records and approximately 1,800 different job codes.
>
> What am I doing wrong?
>
> Thanks in advance.
>
> "vanderghast" wrote:
>
>> It seems that
>>
>> (SELECT
>> Median("tEmployeeMasterCopy","HourlyRate")
>> FROM tEmployeeMasterCopy as M1
>> WHERE M1.JobCode = tEmployeeMasterCopy.JobCode)
>>
>>
>> returns more than one record.  Is there only ONE record per JobCode?
>>
>>
>>
>> Vanderghast, Access MVP
>>
>>
>>
>>
>> "ScottMsp" <ScottMsp@discussions.microsoft.com> wrote in message
>> news:7B6FD707-8B94-4CA7-8822-CE5F083DB84F@microsoft.com...
>> > What is wrong with this SQL?  I am trying to use a Median function and
>> > trying
>> > to have the median calculate for each JobCode.
>> >
>> > SELECT tEmployeeMasterCopy.JobCode, (SELECT
>> > Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as 
>> > M1
>> > WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
>> > FROM tEmployeeMasterCopy
>> > GROUP BY tEmployeeMasterCopy.JobCode;
>> >
>> > Thanks in advance.
>> 

0
vanderghast
12/29/2009 2:11:24 PM
Try this modification.  A sub-query used in the select clause must return only 
one value and one record.  Your sub-query has in theory the potential to 
return multiple records.  Use First or one of the other aggregate SQL 
operators (Max, Min, Avg, or Last) to limit the rows returned.

SELECT tEmployeeMasterCopy.JobCode
, (SELECT First(Median("tEmployeeMasterCopy","HourlyRate"))
    FROM tEmployeeMasterCopy as M1
    WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
FROM tEmployeeMasterCopy

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

ScottMsp wrote:
> What is wrong with this SQL?  I am trying to use a Median function and trying 
> to have the median calculate for each JobCode.
> 
> SELECT tEmployeeMasterCopy.JobCode, (SELECT 
> Median("tEmployeeMasterCopy","HourlyRate") FROM tEmployeeMasterCopy as M1 
> WHERE M1.JobCode = tEmployeeMasterCopy.JobCode) AS MedianByJobCode
> FROM tEmployeeMasterCopy
> GROUP BY tEmployeeMasterCopy.JobCode;
> 
> Thanks in advance.
0
John
12/29/2009 2:18:37 PM
Reply:

Similar Artilces:

Calling Dex Procedures from SQL
Is there any way to call dex procs from SQL or VB code? Jed There is an undocumented and unsupported method of calling Dex code from VBA. However, I know of no method of calling Dexterity from SQL. Please email me if you want an example, just remove the online. from the address below. David Musgrave [MSFT] Senior Development Consultant MBS Services - Asia Pacific Microsoft Business Solutions http://www.microsoft.com/BusinessSolutions mailto:dmusgrav@online.microsoft.com Any views contained within are my personal views and not necessarily Microsoft Business Solutions policy. This p...

Need with with 4125 RWW Not Working
SBS2008 Remote from Win 7 Pro and XP Pro clients I am having an issue with getting RWW to work in the SBS 2008 environment. Basically I can connect to the server via \remote from a off-site location but when I try and "Connect to a Computer" I get the following message. "The wizard cannot configure Remote Desktop Connection settings. Make sure that the client version of the Remote Desktop Protocol (RDP) 6.0 or later is installed on this computer." This also includes when I try and connect to the SBS 2008 server itself. At first I thought when this didn...

CDialogs not working ????
HI, I am using the following code as CQChatDlg dlg; m_pMainWnd = &dlg; dlg.initMembers(); int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } AfxMessageBox("Created"); qPub.Create(IDD_DIALOG_PUB); qPub.SetFocus(); qPub.EnableWindow(); AfxMessageBox("Created"); ******** The problem is that qPub dialog is not created and on debug I found ...

Rules not working #8
Does anybody have any experience of rules not working when the email arrives, but when "run" from the rules window it works perfectly? Oldmoondog Oldmoondog <oldmoondog@ntlworld.com> wrote: > Does anybody have any experience of rules not working when the email > arrives, but when "run" from the rules window it works perfectly? There are a lot of postings about that. Before anyone can answer, you'll need to state your Outlook version and give a description of the rule that doesn't work and an example of a message on which you believe that rule s...

is SQL 2000 architecture different SQL Server 2005 / 2008 architecture?
Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I have been told is a good book. I am going backwards... reading about the sql server architecture in chapter 3 of the book. As I'm reading... I was wondering is it different SQL Server 2000 vs SQL Server 2005/2008? Also where can I find a book/or link that can give me more details about the internals on architecture? Thank you norm (normanchan@gmail.com) writes: > Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I > have been told is a good book. I am g...

External E-Mail Forwarding Not Working Properly
I have a situation where I need only certain people to be able to send and receive internet e-mail and I also need certain peoples e-mails to be forwarded to internet e-mail addresses. I've followed the instructions in this article for restricting internet e- mail access: http://www.msexchange.org/tutorials/MF009.html As well as this article for forwarding e-mails: http://www.msexchange.org/tutorials/MF015.html The problem now, is that if someone in the "No Internet E-Mail" group tries to send an e-mail to a forwarder-enabled recipient, they receive an NDR and the m...

Moving customizations from SQL 2k to SQL 2k5 CRM
This may be a non-issue but I wanted to check to see if anyone has done this and ran into issues. I have the XML customization files from our previous install of CRM 3.0 with SQL 2k database. I now want to import and publish these same customizations to CRM 3.0 on SQL 2k5. Has anyone done this or can point me to documentation that tells about the success or issues for doing this? Thanks. On 7 Feb., 06:35, Chris Treanor <ChrisTrea...@discussions.microsoft.com> wrote: > This may be a non-issue but I wanted to check to see if anyone has done this > and ran into issues. > &...

How do i find out what version of excel or works is on a computor?
Can anyone tell me how I can find out what version of works and also excel is on a computor? The usual quick method would be to click Help on the application's menu and then select About......in the dropdown list (usually at the bottom of the list). Rob "tootsieroll321" <anonymous@discussions.microsoft.com> wrote in message news:141001c4ac3b$73e5bb60$a401280a@phx.gbl... > Can anyone tell me how I can find out what version of > works and also excel is on a computor? Good answer from Rob but if you want to do it dynamically use =INFO("release") Value f...

Budget not working?
I'm trying to figure out the Advanced Budget in Money 2006. Either I am not understanding or it is not working correctly. Let me start with a simple example. I get paid a commission check of $1200 every 3 months. That works out to $400 per month. I enter that as a recurring bill, which automatically adds it to my budget. On the Enter Your Income screen it shows correctly - $1200 every 3 months which is $400 per month. On the Enter Your Expenses screen I enter a simple expense of $100 per month. In the lower right corner it shows the Total Monthly Income of $400 and Total Month Expe...

CRM 3.0 Install Error with SQL Reporting Services and SQL 2005
Hi, Doing a CRM 3.0 Server installation. During the last page of the install wizard (or what I assume is the last page) when I goes through and does verification I get a red X next to SQL Reporting Services. The message I receive when I look at the details is: ***** The specified path is not a metabase path. Parameter name: path ***** Now SRS is installed and working. I put in the URL: http://<servername>/reportserver and every other variation on that I can find. Thoughts? Alan try http://servername/reports also assuming you did not use ssl... =======================...

Outbound Mail has stopped working
Why would outbound mail all of the sudden stopped working on a MS Exchange 2000 Server. ...

SQL 'for xml' and C# example??
I'm missing some piece of the puzzle. I'm using 'for xml' type queries from SQL2K - and now trying to change the front-end from ADO to C#, I can't quite figure it out.. It seems that I could use an XmlReader.. and somehow use that instead of a DataReader.. but that didn't work.. I tried using a DataReader and get the byte array - but that wasn't right. SQL returns formatted XML, I need to load that XML into an XmlDocument somehow without ANY molestation of that dom. Any ideas? Examples? Missing links???? thanks THANKS!! I've been looking for the answer to ...

sql summarizing help
Hi All, Given: table1 --------- id, cdate, weeksago, amount, typeid, timeclose 496, 2010-02-11, 0, 450, 3, 1 490, 2010-02-01, 1, 200, 4, 1 491, 2010-02-01, 1, 350, 2, 2 493, 2010-02-01, 1, 500, 5, 1 489, 2010-01-21, 3, 150, 2, 1 136, 2010-01-12, 4, 500, 3, 2 137, 2010-01-12, 4, 100, 3, 1 138, 2010-01-12, 4, 500, 2, 1 1. i'm trying to create a sql that will give me a summary resultset grouped by weeksago. 2. i want to sum the columns and get counts based on typeid. 3. avg of timeclose here's the sql i have: select weeksago, , (sum(timeClose)/(count(timeclo...

can't get CListCtrl groups to work
Env.: VS2003, Common Controls v6 Hi All, I have this CListCtrl in report view displaying a bunch of items. I wanted to use item groups (a new feature in common controls v6). You know, such as in the Network Connections in Explorer under Win XP : Dial-up, Incoming, LAN or High-speed Internet... Docs are close to inexistant but I thought it shouldn't be to difficult : - call List.EnableGroupView() - call List.InsertGroup() to create each group. - call List.MoveItemToGroup() to place each item in a group. Even though not clearly documented in MS docs, these steps are the same as describ...

Update multivalue field in table from form with SQL
Access 2007 on Vista I am using a form to establish the variables for a report before it is created. One field on the underlying table is a multivalue field, and the form includes a multi-select combo box control. After selections are made and the OK button on the multi-select combo drop down is clicked, focus moves to the next control. I have discovered though, that the underlying table does not update immediately, but does so correctly when I close the form. This sequence does not deliver the proper report data, so I need a way to immediately update the underlying table (m...

Refer to SQL statements globally
I have two large SQL statements that I need to use across several forms, reports, etc. Rather than repeat them in each form, I'm trying to store and reference them globally. For example, I'd like to store strSQL1 and strSQL2 in a global module, so I can use either like this in a form: Random form: Me!lstResults.RowSource = strSQL1 Random report: Me.Report.RecordSource = strSQL2 Would I just store the SQL statements in a global module like: Public Function SQLSource() As String Dim strSQL1 As String Dim strSQL2 As String strSQL1 = "SELECT blah blah...

Does Publisher 2002 work in Win98SE? in Windows 2000? #2
I looked here (http://www.microsoft.com/office/previous/publisher/2002sysreqs.asp) and it seems to, but why do I remember a thread in this ng a few months back saying that it either doesn't, really, or with great difficulty? Can anyone confirm who uses 2002 on Win98SE? TIA. ...

Does this work?
Has any one tried StudioTraffic? The site advertises itself as pay for watching ads. I have tried and they paid some money ($5) for surfing. Is this kind of thing real or scam? Their fundamentals seems fine. Heres the link: http://www.studiotraffic.com/index.php?refid=1233245&username=bookfish Regards, Bookfish ...

To SQL or not to SQL?
I have a very vertical product developed 100% in M/S Access. I developed it myself in 1993 in the initial release of Access and been with it ever since. It is currently very stable and running in Access 2003. My big question is how or even *if* we should move it to SQL. My customers are getting bigger all the time with more users. Typically our customers run one or two workstations generally never more than 5. Performance, however, is starting to slow with database sizes passing 500 meg. We also have a LOT of code behind forms so are bound tightly to the Access development environ...

PostgreSQL or other SQL servers in RMS
Has anyone successfully implemented RMS with one of the free SQL servers? Theoretically, is there any reason why we have to use MS SQL Server 2000 for large databases? David "dh" <dh@discussions.microsoft.com> ha scritto nel messaggio news:1783A90D-AF3E-4D91-A445-0039D4E7E379@microsoft.com... > Has anyone successfully implemented RMS with one of the free SQL servers? > Theoretically, is there any reason why we have to use MS SQL Server 2000 > for > large databases? RMS has implemented only for MS SQL Server (2000/MSDE) and isn't possible to work with ano...

Problems Getting a Field to Work in a New Mail Message form
I have Outlook 2000 SR-1 using IMO (Internet Mail Only - not Exchange Server). Does anyone know of a way to make sure the From: field appears in a new mail message? For reasons too lengthy to get into, it would be very helpful for me to see the From: field (with my default email address already filled in, of course) as well as they typical To:, CC: and Subject: fields when composing a new email. I did try to create a new form (Tools >Forms >Design a Form >Message) but when I used it, the default email address did not appear. What am I doing wrong? Thanks, Sangrateo SangrateoATanonymo...

writeback to SQL 2005
hello! I use excel 2007 iI open a connection agianst SQL server 2005 and choose a table. In my workbook i can now see this data from this table. if i change a value in some cell i wish to writeback to table in SQL server. Is this possible? It's possible through the use of VBA and ADO, but not simply using the data connection you've already established. Ross "CJ" <CJ@discussions.microsoft.com> wrote in message news:D54EC521-0DD5-4B3D-971B-4FF3394D6FA8@microsoft.com... > hello! I use excel 2007 > iI open a connection agianst SQL server 2005 and choose a table...

sql.request #2
Does anyone have a comprehensible guide on how to use it that a dolt like me can follow? Every time I try I get a #N/A. The only guide I can find is at http://www.bygsoftware.com/examples/zipfiles/UsingSqlRequest.zip but it's really hard to follow. From the MSDN Library: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/SEC41.asp Not sure how familiar you are with ODBC; but if you are getting #N/A as a result, it is usually because of problems either with the SQL syntax or the connection string. There are a few examples of connection strings in the re...

MSDE or SQL Personal or SQL Developer? or SQL Workstation?
Hi, which one is better for personal non-server computer? MSDE? SQL Personal? SQL Developer? SQL Workstation? Thank you. It depends on what edition of GP you are going to run. If you are going to use Professional, you need to go with SQL Server developer, assuming you are installing on a non-server OS. "ano" wrote: > Hi, which one is better for personal non-server computer? MSDE? SQL Personal? > SQL Developer? SQL Workstation? > Thank you. Is the function of SQL Developer the same as Enterprise edition? How about personal edition? Can it be used for GP Pro? Can dev...

Excel RoundUp in Transact-SQL
Last week I posted the following computation in Excel... roundup(roundup(rate * 500),4) - roundup(rate * 200,4) + polfee,2) ((6.108934 * (500000/1000)) - (200 * 6.108934) + 1311.51) = 3144.20 In SQL using round(num,4) = 3144.19 I've moded this to... round(num+0.0005,4) = 3144.20 This works most of the time but occasionally I'm still off from what Excel returns. I'm under 2% in my 342 calcs, so it's do-able and we'll explain if any client notes this that we've reduced their pmt by .01 cents. BUT, I'd still like a way to replicate Excel'...