473,396 Members | 2,037 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.

Can someone explain me something about my SQL query...

I have this:
SELECT UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName,
Sum(SoldItems.Commision) AS TotalCommission, Sum(TIP.TipAmt) AS
TotalTip,(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable)
FROM
(SELECT * FROM ((SoldItems INNER JOIN UserInfo ON
SoldItems.EmpID=UserInfo.EmpID) INNER JOIN [Transaction] ON
SoldItems.TransID=Transaction.TransID) INNER JOIN TIP ON
(Transaction.TransID=TIP.TransID) AND (UserInfo.EmpID=TIP.EmpID)
WHERE ((DateValue(Transaction.Time)) Between #8/2/2007# And
#8/30/2007#) ) AS mytable
GROUP BY UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName;

and Access "cannot see" 'my table' in the query (SELECT
Sum(SoldItems.Commision) AS MyCommission FROM mytable). I already
reference it in the the subquery below it!!
Thanks

(the query may look a little odd as I just try out the query)

Aug 17 '07 #1
8 1463
daicamad wrote:
I have this:
SELECT UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName,
Sum(SoldItems.Commision) AS TotalCommission, Sum(TIP.TipAmt) AS
TotalTip,(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable)
FROM
(SELECT * FROM ((SoldItems INNER JOIN UserInfo ON
SoldItems.EmpID=UserInfo.EmpID) INNER JOIN [Transaction] ON
SoldItems.TransID=Transaction.TransID) INNER JOIN TIP ON
(Transaction.TransID=TIP.TransID) AND (UserInfo.EmpID=TIP.EmpID)
WHERE ((DateValue(Transaction.Time)) Between #8/2/2007# And
#8/30/2007#) ) AS mytable
GROUP BY UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName;

and Access "cannot see" 'my table' in the query (SELECT
Sum(SoldItems.Commision) AS MyCommission FROM mytable). I already
reference it in the the subquery below it!!
Thanks

(the query may look a little odd as I just try out the query)
You seem to be selecting fields from UserInfo but if there's any table
available for referencing I'd think it would be MyTable as you are
Froming from MyTable and referencing UserInfo.
Aug 17 '07 #2
Ben
On Aug 17, 8:28 am, daicamad <daica...@gmail.comwrote:
I have this:
SELECT UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName,
Sum(SoldItems.Commision) AS TotalCommission, Sum(TIP.TipAmt) AS
TotalTip,(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable)
FROM
(SELECT * FROM ((SoldItems INNER JOIN UserInfo ON
SoldItems.EmpID=UserInfo.EmpID) INNER JOIN [Transaction] ON
SoldItems.TransID=Transaction.TransID) INNER JOIN TIP ON
(Transaction.TransID=TIP.TransID) AND (UserInfo.EmpID=TIP.EmpID)
WHERE ((DateValue(Transaction.Time)) Between #8/2/2007# And
#8/30/2007#) ) AS mytable
GROUP BY UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName;

and Access "cannot see" 'my table' in the query (SELECT
Sum(SoldItems.Commision) AS MyCommission FROM mytable). I already
reference it in the the subquery below it!!

Thanks

(the query may look a little odd as I just try out the query)
Salad is right, you can't select userinfo.EmpID because by the time
you select it the field has become mytable.EmpID.

This would be easier to see if you specified fields instead of * in
your subquery.
Aug 17 '07 #3
You guys are correct. I read about this on standard SQL, but if I do
this it work fine (may be something Access does not follow standard)
SELECT UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName,
Sum(SoldItems.Commision) AS TotalCommission, Sum(TIP.TipAmt) AS
TotalTip FROM
(SELECT * FROM ((SoldItems INNER JOIN UserInfo ON
SoldItems.EmpID=UserInfo.EmpID) INNER JOIN [Transaction] ON
SoldItems.TransID=Transaction.TransID) INNER JOIN TIP ON
(Transaction.TransID=TIP.TransID) AND (UserInfo.EmpID=TIP.EmpID)
WHERE ((DateValue(Transaction.Time)) Between #8/2/2007# And
#8/30/2007#) ) AS mytable
GROUP BY UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName;

But when I put ,(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable) as a field, it does not see mytable at all.
But if I use mytable.UserInfo.EmpID, it's ok

I am now even more baffle!!
Aug 17 '07 #4
I think I understand this now. Correct me if I am wrong. The field
(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable) was calculate first; therefore, at that time, there is no
'mytable' has been referenced yet! It was computed first because it's
an subquery. The mytable.UserInfo.EmpID field refer to an actual field
that will computed after the query, thus it's ok.

And why User.EmpID alone still work then I just guess Access assume it
from 'mytable' as there is nothing else there beside it.

If I am right, it still does not solve my problem. There is another
field called IsRetailItem in SoldItems table. I want to include 2
fields in my report that sum up the commision if the IsRetailItem =
TRUE and IsRetailItem = FALSE . So I intend to include fields (SELECT
Sum(SoldItems.Commision) AS RetailCommission FROM mytable WHERE
SoldItems.IsRetailItem =TRUE), (SELECT Sum(SoldItems.Commision) AS
NonretailCommission FROM mytable WHERE SoldItems.IsRetailItem =FALSE)
Any thoughts? :^)

Aug 17 '07 #5
daicamad wrote:
You guys are correct. I read about this on standard SQL, but if I do
this it work fine (may be something Access does not follow standard)

AFAIK even in "Standard" SQL you cannot reference a materialized table
from within a sub-select of the outer query.
Aug 17 '07 #6
Post your simplified table structure with some sample data and the
output you need

daicamad wrote:
I think I understand this now. Correct me if I am wrong. The field
(SELECT Sum(SoldItems.Commision) AS MyCommission FROM
mytable) was calculate first; therefore, at that time, there is no
'mytable' has been referenced yet! It was computed first because it's
an subquery. The mytable.UserInfo.EmpID field refer to an actual field
that will computed after the query, thus it's ok.

And why User.EmpID alone still work then I just guess Access assume it
from 'mytable' as there is nothing else there beside it.

If I am right, it still does not solve my problem. There is another
field called IsRetailItem in SoldItems table. I want to include 2
fields in my report that sum up the commision if the IsRetailItem =
TRUE and IsRetailItem = FALSE . So I intend to include fields (SELECT
Sum(SoldItems.Commision) AS RetailCommission FROM mytable WHERE
SoldItems.IsRetailItem =TRUE), (SELECT Sum(SoldItems.Commision) AS
NonretailCommission FROM mytable WHERE SoldItems.IsRetailItem =FALSE)
Any thoughts? :^)
Aug 17 '07 #7
On Aug 17, 12:46 pm, John Winterbottom <john...@rogers.cawrote:
Post your simplified table structure with some sample data and the
output you need
SoldItems Table structure:

SoldItemTableIndex
UPC
TransID
EmpID
Commision
IsRetailItem

Data:

154, 56789, 07220104211-01, 1000, $9.00, FALSE
155, 56789, 07220113322-01, 4627, $1.00, TRUE
156, 74145, 07220113322-01, 4627, $2.00, FALSE
-----------------------------
UserInfo Table structure

EmpID
FirstName
LastName

Data:
1000, Terri, G
4627, Suzi, W
-----------------------------
Transaction table structure

TransID
[Time]

Data:
07220104211-01, 8/8/2007 10:42:11 AM
07220113322-01, 8/8/2007 11:33:22 AM

(Let ignore the TIP for now to simplify the database)
The output I need would be

FirstN LastN TotalCommision RetailCommision NonRetailCommission
------------------------------------------------------------------------------------
Terri G $9
$0 $9
Suzi W $3
$1 $2

Aug 17 '07 #8
I think I found the solution. It's not elegant, so if anyone come with
a better, more SQL related solution, I still like to see it

This only work with Access

SELECT UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName,
Sum(SoldItems.Commision) AS TotalCommission,
Sum(IIF(SoldItems.IsRetailItem=TRUE,SoldItems.Comm ision,0))
FROM
(SELECT * FROM ( (SoldItems INNER JOIN UserInfo ON
SoldItems.EmpID=UserInfo.EmpID) INNER JOIN [Transaction] ON
SoldItems.TransID=Transaction.TransID)
WHERE ((DateValue(Transaction.Time)) Between #8/2/2007# And
#8/30/2007#) ) AS mytable
GROUP BY UserInfo.EmpID, UserInfo.FirstName, UserInfo.LastName;

I used IFF function to filter out whether the sold items are retail
items or not. Probbaly now I don't need mytable anymore.


Aug 17 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce D | last post by:
I'm trying to understand MySQL better. I can't seem to figure out why the second query takes 2 minutes and the first query takes 12 seconds. It should be noted that the table, KBM, has 250...
10
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a...
3
by: Mark Harrison | last post by:
I have indexed two columns in a table. Can somebody explain to me why the first query below uses an Index Scan while the second uses a Seq Scan? Many TIA! Mark planb=# \d abcs Table...
1
by: Simon Windsor | last post by:
Hi I have just recevived this error could not write to hash-join temporary file: No space left on device Can someone please explain how I can stop this occuring. Whereis the hash-join...
3
by: chucher | last post by:
I´m developing a database in access about invoices that could use to different kinds of money (US Dollar and Pesos). I have a form where you can insert the invoices and de items of de invoice. I...
2
by: blueyonder | last post by:
The statament below does exactly what I want it to do but I don't understand why? In my mind the subquery produces a result set which is a subset of the handset table which the initial part of...
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
4
by: stiki | last post by:
How and what does this mean in php: ($this->varname) I can't seem to find this by searching Google, because the "->" are removed from my search query. Cheers, Igor Terzic www.stikimedia.com
7
by: skaushik | last post by:
Hi all, I was working on a SQL query where a history table is joined with a small table to get some information. There is an index on the history table column but the explain plan tells that there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
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,...
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
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.