Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Help 4

Status
Not open for further replies.
Mar 17, 2005
147
US
I want this query to show the unique orders per persons firstname, which in this case happens to be the full name
I have 3 tables Users, Orders, OrderDetails.

SELECT DISTINCT DetailOrderID, UserID, OrderUserID, DetailProductName, DetailQuantity, DetailPrice, OrderShipping, OrderTotal, OrderDate, Firstname
FROM Orders o, OrderDetails d, Users u
WHERE o_OrderUserID = u.UserID

Problem with this query is it shows everything like multiple times.
 
Looks like you are missing a join on the Orders and OrderDetails table.

Code:
SELECT DISTINCT DetailOrderID, UserID,  OrderUserID, DetailProductName, DetailQuantity, DetailPrice, OrderShipping, OrderTotal, OrderDate, Firstname
FROM Orders o, OrderDetails d, Users u
WHERE o.OrderUserID = u.UserID and [red]O.IdField = d.IdField[/red]

I don't know what the field names are, but you should.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Is that your whole query? If yes then you are missing a join condition between orders and orderdetails hence you see multiple values.

If it was just a cut and paste problem, then what are you criteria to pick up one orderID per firstname??

Regards,
AA
 
Getting closer, but now when I test this I get type mismatch in expression???

SELECT DISTINCT DetailOrderID, OrderReferenceID, UserID, OrderUserID, DetailProductName, DetailQuantity, DetailPrice, OrderShipping, OrderTotal, OrderDate, Firstname
FROM Orders o, OrderDetails d, Users u
WHERE o_OrderUserID = u.UserID and O.OrderReferenceID = d.DetailOrderID
 
you may need to use Cast() or Convert() functions in your where conditions...

-DNG
 
You need to make sure the datatypes on these two columns are the same.

> O.OrderReferenceID = d.DetailOrderID

If not use the cast/convert functions.
 
something like

Convert(Varchar,o_OrderUserID) = u.UserID

thats is to make sure you have same data types on either side of the = sign

-DNG
 
Try running this query.

Code:
Select table_name, column_name, data_type From Information_Schema.Columns
Where (TABLE_NAME = 'Orders' And COLUMN_NAME = 'OrderUserId')
       Or (TABLE_NAME = 'Orders' And COLUMN_NAME = 'OrderReferenceId')
       Or (TABLE_NAME = 'Users' And COLUMN_NAME = 'UserId')
       Or (TABLE_NAME = 'OrderDetails' And COLUMN_NAME = 'DetailOrderId')

paste the results in to a post here. It should highlight the data type issue for us.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Ok your right OrderReferenceID is text and DetailOrderID is number so now how would I need to write this:

SELECT DISTINCT DetailOrderID, OrderReferenceID, UserID, OrderUserID, DetailProductName, DetailQuantity, DetailPrice, OrderShipping, OrderTotal, OrderDate, Firstname
FROM Orders o, OrderDetails d, Users u
WHERE o_OrderUserID = u.UserID and O.OrderReferenceID = d.DetailOrderID
 
WHERE o_OrderUserID = u.UserID and Convert(int, O.OrderReferenceID) = d.DetailOrderID
 
jbenson001,

You cannot convert a text field to an int field. You get the following error: Explicit conversion from data type text to int is not allowed.

VonFranzken,
I strongly urge you to change the data type of the field. If this 'text' field is only used to store numbers, then you should change it to an integer field. It may not be fun right now, but in the long run, you'll be glad you did.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
IF the text is a valid numeric value it will convert. I tried it with no errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top