Sunday, March 11, 2012
Another Newb SQL Question
SELECT tblCustomers.companyName, tblOrders.orderID,
tblOrders.freightCharge, tblOrderDetails.unitPriceOnOrderDate
FROM tblCustomers
JOIN tblOrders
ON tblCustomers.customerID = tblOrders.orderID
JOIN tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
WHERE EXISTS
(
SELECT tblOrders.orderID
FROM tblOrders
JOIN tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
GROUP BY tblOrders.orderID, tblOrderDetails.orderID,
tblOrders.freightCharge
HAVING tblOrders.orderID = tblOrderDetails.orderID and
tblOrders.freightCharge > MIN(tblOrderDetails.unitPriceOnOrderDate)
)
idea is to show all orders whose freight charge (contained in
tblOrders) exceeds the unit price (contained in tblOrderDetails) of any
product in that order.
unfortunately results from my query contain many orders where the
freight charge is nowhere near as high as the prices of the products.
thxPlease post DDL and sample data.
ML|||<A_StClaire_@.hotmail.com> wrote in message
news:1130611630.835990.38500@.g49g2000cwa.googlegroups.com...
> me again. been staring at this and can't see what's wrong.
>
<snip>
> idea is to show all orders whose freight charge (contained in
> tblOrders) exceeds the unit price (contained in tblOrderDetails) of
any
> product in that order.
> unfortunately results from my query contain many orders where the
> freight charge is nowhere near as high as the prices of the
products.
> thx
>
A_StClaire_@.hotmail.com,
Original Query:
SELECT tblCustomers.companyName
,tblOrders.orderID
,tblOrders.freightCharge
,tblOrderDetails.unitPriceOnOrderDate
FROM tblCustomers
JOIN
tblOrders
ON tblCustomers.customerID = tblOrders.orderID
JOIN
tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
WHERE EXISTS
(SELECT tblOrders.orderID
FROM tblOrders
JOIN
tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
GROUP BY tblOrders.orderID
,tblOrderDetails.orderID
,tblOrders.freightCharge
HAVING tblOrders.orderID = tblOrderDetails.orderID
and tblOrders.freightCharge >
MIN(tblOrderDetails.unitPriceOnOrderDate)
)
The outer query has:
tblOrders
tblCustomers
tblOrderDetails
The subquery has:
tblOrders
tblOrderDetails
Note: In the outer query, tblOrder and tblCustomers are JOINed on
customerID = orderID. Is this correct (I can't imagine it would be)?
I changed it in example below in the hopes I was interpreting things
correctly.
--
In order for an EXISTS predicate to function properly (AFAIK, anyway),
the subquery must be correlated with the outer query.
I cannot spot the correlation in the above SQL. Without Table
Aliases, the query optimizer has no way of linking the subquery to the
outer query.
The following is the original Query now reformatted with Table Aliases
and a WHERE clause correlating the subquery and the outer query.
SELECT C1.companyName
,O1.orderID
,O1.freightCharge
,OD1.unitPriceOnOrderDate
FROM tblCustomers AS C1
INNER JOIN
tblOrders AS O1
ON C1.customerID = O1.customerID
INNER JOIN
tblOrderDetails AS OD1
ON O1.orderID = OD1.orderID
WHERE EXISTS
(SELECT O2.orderID
FROM tblOrders AS O2
INNER JOIN
tblOrderDetails AS OD2
ON O2.orderID = OD2.orderID
WHERE O1.orderID = O2.orderID
GROUP BY O2.orderID
,OD2.orderID
,O2.freightCharge
HAVING O2.orderID = OD2.orderID
and O2.freightCharge >
MIN(OD2.unitPriceOnOrderDate)
)
Note: Notice how the WHERE clause refers to a table in the outer query
by using the alias of the table in the outer query.
Note: I am making a fairly huge guess on the proper correlation in the
WHERE clause of the subquery. Pleaes take it for what it is meant to
be, a hint directing you toward a solution. IOW, this is definitely
not tested.
Sincerely,
Chris O.
PS Although meant for microsoft.pulic.sqlserver.programming, the
following link is still applicable for microsoft.pulic.access.queries:
http://www.aspfaq.com/etiquette.asp?id=5006, when it comes to
providing the information that will best enable others to answer your
question.|||<A_StClaire_@.hotmail.com> wrote in message
news:1130611630.835990.38500@.g49g2000cwa.googlegroups.com...
> me again. been staring at this and can't see what's wrong.
>
<snip>
> idea is to show all orders whose freight charge (contained in
> tblOrders) exceeds the unit price (contained in tblOrderDetails) of
any
> product in that order.
> unfortunately results from my query contain many orders where the
> freight charge is nowhere near as high as the prices of the
products.
> thx
>
A_StClaire_@.hotmail.com,
Original Query:
SELECT tblCustomers.companyName
,tblOrders.orderID
,tblOrders.freightCharge
,tblOrderDetails.unitPriceOnOrderDate
FROM tblCustomers
JOIN
tblOrders
ON tblCustomers.customerID = tblOrders.orderID
JOIN
tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
WHERE EXISTS
(SELECT tblOrders.orderID
FROM tblOrders
JOIN
tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
GROUP BY tblOrders.orderID
,tblOrderDetails.orderID
,tblOrders.freightCharge
HAVING tblOrders.orderID = tblOrderDetails.orderID
and tblOrders.freightCharge >
MIN(tblOrderDetails.unitPriceOnOrderDate)
)
The outer query has:
tblOrders
tblCustomers
tblOrderDetails
The subquery has:
tblOrders
tblOrderDetails
Note: In the outer query, tblOrder and tblCustomers are JOINed on
customerID = orderID. Is this correct (I can't imagine it would be)?
I changed it in example below in the hopes I was interpreting things
correctly.
--
In order for an EXISTS predicate to function properly (AFAIK, anyway),
the subquery must be correlated with the outer query.
I cannot spot the correlation in the above SQL. Without Table
Aliases, the query optimizer has no way of linking the subquery to the
outer query.
The following is the original Query now reformatted with Table Aliases
and a WHERE clause correlating the subquery and the outer query.
SELECT C1.companyName
,O1.orderID
,O1.freightCharge
,OD1.unitPriceOnOrderDate
FROM tblCustomers AS C1
INNER JOIN
tblOrders AS O1
ON C1.customerID = O1.customerID
INNER JOIN
tblOrderDetails AS OD1
ON O1.orderID = OD1.orderID
WHERE EXISTS
(SELECT O2.orderID
FROM tblOrders AS O2
INNER JOIN
tblOrderDetails AS OD2
ON O2.orderID = OD2.orderID
WHERE O1.orderID = O2.orderID
GROUP BY O2.orderID
,OD2.orderID
,O2.freightCharge
HAVING O2.orderID = OD2.orderID
and O2.freightCharge >
MIN(OD2.unitPriceOnOrderDate)
)
Note: Notice how the WHERE clause refers to a table in the outer query
by using the alias of the table in the outer query.
Note: I am making a fairly huge guess on the proper correlation in the
WHERE clause of the subquery. Pleaes take it for what it is meant to
be, a hint directing you toward a solution. IOW, this is definitely
not tested.
Sincerely,
Chris O.
PS The link http://www.aspfaq.com/etiquette.asp?id=5006, is excellent
when it comes to detailing how to provide the information that will
best enable others to answer your questions.|||Did it occur to you that "unit_price_on_order_date" is a query and
not a data element? WHERE IS THE IMPLIED HISTORY PRICE TABLE?
Did it occur to you that a customer_id should NEVER be equal to an
order_id?
What you posted is a screwed up mess.
Then you to spit on the people that ate helping you for free, you never
posted DDL.
Would you like to try again? With DDL? With specs that can be
programmed from?|||On 29 Oct 2005 11:47:10 -0700, A_StClaire_@.hotmail.com wrote:
(snip)
>idea is to show all orders whose freight charge (contained in
>tblOrders) exceeds the unit price (contained in tblOrderDetails) of any
>product in that order.
Hi A_StClaire_,
Try if the query below works:
SELECT tblCustomers.companyName, tblOrders.orderID,
tblOrders.freightCharge, tblOrderDetails.unitPriceOnOrderDate
FROM tblCustomers
JOIN tblOrders
ON tblCustomers.customerID = tblOrders.orderID
JOIN tblOrderDetails
ON tblOrders.orderID = tblOrderDetails.orderID
WHERE tblOrders.freightCharge > unitPriceOnOrderDate
(Untested - see www.aspfaq.com/5006 if you prefer a tested solution, or
if the query above doesn't meet your expectations)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||--CELKO-- (jcelko212@.earthlink.net) writes:
> Did it occur to you that "unit_price_on_order_date" is a query and
> not a data element? WHERE IS THE IMPLIED HISTORY PRICE TABLE?
> Did it occur to you that a customer_id should NEVER be equal to an
> order_id?
> What you posted is a screwed up mess.
No, what you posted is a screwed up mess. For crying out load, the
guys says that he is a begeinner. Beginners are permitted to make mistakes.
And they should be permitted to make mistakes, without having to be
insulted by morons like you.
> Then you to spit on the people that ate helping you for free, you never
> posted DDL.
>
How can he post DDL when you never explain what it is? And how the hell
can you accuse someone for spitting when he is asking politely?
> Would you like to try again?
The risk is that he will never try again, because he was turned off by
your reply. Is that what you want?
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:f1t7m192si2fd8h6ftv78lvotge2mg4h2i@.
4ax.com...
> On 29 Oct 2005 11:47:10 -0700, A_StClaire_@.hotmail.com wrote:
> (snip)
any
> Hi A_StClaire_,
> Try if the query below works:
> SELECT tblCustomers.companyName, tblOrders.orderID,
> tblOrders.freightCharge, tblOrderDetails.unitPriceOnOrderDate
> FROM tblCustomers
> JOIN tblOrders
> ON tblCustomers.customerID = tblOrders.orderID
> JOIN tblOrderDetails
> ON tblOrders.orderID = tblOrderDetails.orderID
> WHERE tblOrders.freightCharge > unitPriceOnOrderDate
> (Untested - see www.aspfaq.com/5006 if you prefer a tested solution,
or
> if the query above doesn't meet your expectations)
> Best, Hugo
> --
>
Hugo,
The above has:
> ON tblCustomers.customerID = tblOrders.orderID
I am still thinking that customerID cannot equal orderID.
Sincerely,
Chris O.|||
> The outer query has:
> tblOrders
> tblCustomers
> tblOrderDetails
> The subquery has:
> tblOrders
> tblOrderDetails
> --
> Note: In the outer query, tblOrder and tblCustomers are JOINed on
> customerID = orderID. Is this correct (I can't imagine it would be)?
> I changed it in example below in the hopes I was interpreting things
> correctly.
> --
you are absolutely right, Chris.
> In order for an EXISTS predicate to function properly (AFAIK, anyway),
> the subquery must be correlated with the outer query.
> I cannot spot the correlation in the above SQL. Without Table
> Aliases, the query optimizer has no way of linking the subquery to the
> outer query.
>
> The following is the original Query now reformatted with Table Aliases
> and a WHERE clause correlating the subquery and the outer query.
>
> SELECT C1.companyName
> ,O1.orderID
> ,O1.freightCharge
> ,OD1.unitPriceOnOrderDate
> FROM tblCustomers AS C1
> INNER JOIN
> tblOrders AS O1
> ON C1.customerID = O1.customerID
> INNER JOIN
> tblOrderDetails AS OD1
> ON O1.orderID = OD1.orderID
> WHERE EXISTS
> (SELECT O2.orderID
> FROM tblOrders AS O2
> INNER JOIN
> tblOrderDetails AS OD2
> ON O2.orderID = OD2.orderID
> WHERE O1.orderID = O2.orderID
> GROUP BY O2.orderID
> ,OD2.orderID
> ,O2.freightCharge
> HAVING O2.orderID = OD2.orderID
> and O2.freightCharge >
> MIN(OD2.unitPriceOnOrderDate)
> )
> Note: Notice how the WHERE clause refers to a table in the outer query
> by using the alias of the table in the outer query.
> Note: I am making a fairly huge guess on the proper correlation in the
> WHERE clause of the subquery. Pleaes take it for what it is meant to
> be, a hint directing you toward a solution. IOW, this is definitely
> not tested.
you derived exactly the intended solution. I am not very clear on many
aspects of SQL including aliases and it's obvious I have a lot to
learn.
> Sincerely,
> Chris O.
> PS Although meant for microsoft.pulic.sqlserver.programming, the
> following link is still applicable for microsoft.pulic.access.queries:
> http://www.aspfaq.com/etiquette.asp?id=5006, when it comes to
> providing the information that will best enable others to answer your
> question.
thx again for your help. I will definitely get up to speed from an
etiquette point of view.|||
> Hugo,
> The above has:
>
> I am still thinking that customerID cannot equal orderID.
>
> Sincerely,
> Chris O.
yes. it would be a very interesting query that could operate in the
manner specified by myself initially.
Thursday, March 8, 2012
Another GUID problem... What is wrong with this code ?
I have the following code:
Dim RowLoopIndex As Integer
DsFuncties.Clear()
A_Functies.Fill(DsFuncties)
For RowLoopIndex = 0 To (DsFuncties.Tables("Functies").Rows.Count - 1)
If DsFuncties.Tables("Functies").Rows(RowLoopIndex).Item("FunctieID") = Func_ID Then
Func_naam = (DsFuncties.Tables("Functies").Rows(RowLoopIndex).Item("Functienaam"))
DDL_Functie.SelectedIndex = RowLoopIndex + 1
Exit For
End If
Next
Func_ID has been declared as follows:
Public Property Func_ID() As Guid
Get
If Not viewstate("Func_ID") Is Nothing Then
Return viewstate("Func_ID")
End If
End Get
Set(ByVal Value As Guid)
viewstate("Func_ID") = Value
End Set
End Property
On the red line I got the following tooltip (error):
Operator '=' is not defined for types 'System.Object' and 'System.Guid'.
How can I define '=' and eg. '&' for type System.Guid ?? Or another solution ??
Help is appreciated, Ger.This is the solution:
If DsFuncties.Tables("Functies").Rows(RowLoopIndex).Item("FunctieID").ToString() = Func_ID.ToString() Then
Hope this helps others too...
regards, Ger.
|||The underlying issue with that exception is you're validating two separate types.By converting the type System.Object to System.GUID, it would be easier to work with.
Func_ID().Equals(CType(DsFuncties.Tables("Functies").Rows(RowLoopIndex).Item("FunctieID"), GUID))
That would be a more appropriate method than to cast both into strings then evaluate.
|||Hey KraGiE, thanks for the advice.
With your help I become a rather good programmer, (I hope).
regards from the North Sea.
Sunday, February 19, 2012
Anaylsis Server 2005 and wrong results in dimensions/cube
My cubes are somehow incorrect and I can't find out why:
I created a very simple table, because of wrong results in my project cubes and inserted 102 rows:
-
USE EA_DWH
GO
CREATE TABLE [dbo].[TEST_DWH]
(
[DWSTOREDATE_INT] INT NOT NULL,
[USER_NAME] VARCHAR(20) NULL
)
GO
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021216','USER_99')
INSERT INTO TEST_DWH VALUES('20021217','USER_03')
INSERT INTO TEST_DWH VALUES('20021218','USER_04')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021219','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_01')
INSERT INTO TEST_DWH VALUES('20021220','USER_03')
INSERT INTO TEST_DWH VALUES('20021220','USER_02')
INSERT INTO TEST_DWH VALUES('20021220','USER_02')
INSERT INTO TEST_DWH VALUES('20021220','USER_02')
INSERT INTO TEST_DWH VALUES('20021220','USER_03')
INSERT INTO TEST_DWH VALUES('20021220','USER_02')
INSERT INTO TEST_DWH VALUES('20021220','USER_02')
INSERT INTO TEST_DWH VALUES('20021220','USER_03')
GO
-
The first value of this table represents the date value as integer and the second value is the user name.
Now when building a cube (I) you will find out that the values differ to a simple group by on the sql database (II):
I count all user and the dimension is build on DWSTOREDATE_INT (logical key) as key attribute and USER_Name as regular attribute.
(I):
-
-
(II):
-
SQL query (SQL Server):
SELECT DWSTOREDATE_INT, USER_NAME, COUNT(*) AS Count_StoreDate_User
FROM TEST_DWH
GROUP BY DWSTOREDATE_INT, USER_NAME
ORDER BY DWSTOREDATE_INT
RESULT:
DWSTOREDATE_INT USER_NAME Count_StoreDate_User
-- --
20021216 USER_99 22
20021217 USER_03 1
20021218 USER_04 1
20021219 USER_01 62
20021220 USER_01 8
20021220 USER_02 5
20021220 USER_03 3
(7 row(s) affected)
-
I tried this example on three different computers and can't find out why this happens. On Analysis Server 2000 it works correctly.
My computer:
W2K3 (SP1), SQL Server 2005 EE (SP1 CTP)
I had the same results without SP1 CTP and installed it because I thought this issue may be corrected.
Please help me solve this problem.
Best regards
You are correct. This is one of the cases where AS2005 is different from AS2000.
In your example you have a case of dimension that is based on the non-unique key attribute.
In AS 2000 Analysis Server automatically makes a dimension key unique by concatenating all levels to the key level ( lowest level in the dimension). In AS 2005 this is no longer the case. Although in simple cases (like you have here) you might be little confused at first, the real gain comes in AS2005 being able to implement better performing and scalable dimensions.
To fix the problem in your dimension, you need to define your composite key for your dimension key attribute. Add USER_NAME column to the KeyColumn of your dimension key attribute.
Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.