Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Group/order by help.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-06-07 : 07:29:09
Johnny writes "I have four tables:

Customer: Customer_Name, City_ID, Credit_Limit
City: City_ID, City, Sales_District
District: Sales_District, Sales_Region
Region: Sales_Region

I need to create a query to show all the customer names grouped by sales_region, then sales_district, ordered by credit limit. I have the following query but it is generating a syntax error:

SELECT C.CUSTOMER_NAME
FROM CUSTOMER C INNER JOIN CITY C1 ON C.CITY_ID=C1.CITY_ID
INNER JOIN DISTRICT D ON D.SALES_DISTRICT=C1.SALES_DISTRICT
INNER JOIN REGION R ON R.SALES_REGION=D.SALES_REGION
ORDER BY R.SALES_REGION, D.SALES_DISTRICT, C.CREDIT_LIMIT;

I don't think I can use the GROUP BY since it requires an aggregate function which I am not using.

Thank you for any help you might be able to provide."

nr
SQLTeam MVY

12543 Posts

Posted - 2004-06-07 : 08:01:57
SELECT R.SALES_REGION, D.SALES_DISTRICT, C.CUSTOMER_NAME
FROM CUSTOMER C INNER JOIN CITY C1 ON C.CITY_ID=C1.CITY_ID
INNER JOIN DISTRICT D ON D.SALES_DISTRICT=C1.SALES_DISTRICT
INNER JOIN REGION R ON R.SALES_REGION=D.SALES_REGION
ORDER BY R.SALES_REGION, D.SALES_DISTRICT, C.CREDIT_LIMIT

Can't see why it gives error - what is the error?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-06-07 : 08:02:09
You need to put all the fields in your order by within the field list in your select statement for a start...

SELECT R.SALES_REGION, D.SALES_DISTRICT, C.CREDIT_LIMIT, C.CUSTOMER_NAME
FROM CUSTOMER C INNER JOIN CITY C1 ON C.CITY_ID=C1.CITY_ID
INNER JOIN DISTRICT D ON D.SALES_DISTRICT=C1.SALES_DISTRICT
INNER JOIN REGION R ON R.SALES_REGION=D.SALES_REGION
ORDER BY R.SALES_REGION, D.SALES_DISTRICT, C.CREDIT_LIMIT
Go to Top of Page
   

- Advertisement -