SQL help needed with sql statement please

Get help on programming - C++, Java, Delphi, etc.
Post Reply
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

SQL help needed with sql statement please

Post by SilverBack »

Hey all,

I have the following sql statement running off a sql 2000 db. Its for ordering stationery. The code is pretty much self explanatory. It needs to get the top 5 items ordered by a user, and must exclude duplicate items. Only problem is I cant seem to exclude the the duplicate items. I added a having clause in there, but it does not seem to work. Any ideas? Want to do this select statement in one sql query...thanks!

Code: Select all

SELECT TOP 5
SI.Item_Code, 
SI.Item_ID, 
SO.Order_ID, 
SOL.Quantity AS Quantity, 
SI.Description, 
SI.Item_Value, 
SI.Quantity AS Stock_Quantity, 
SO.Order_Date 

FROM 
Stationery_Orders SO

INNER JOIN Stationery_Order_Lines SOL ON
SOL.Order_ID = SO.Order_ID 
INNER JOIN Stationery_Items SI ON
SI.Item_ID = SOL.Item_ID 

WHERE 
SO.Employee_Number='1245' 

GROUP BY
SI.Item_Code, 
SI.Item_ID, 
SO.Order_ID, 
SOL.Quantity, 
SI.Description, 
SI.Item_Value, 
SI.Quantity, 
SO.Order_Date 

HAVING
COUNT(SI.Item_ID) = 1

ORDER BY SO.Order_Date DESC
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
Judas
Registered User
Posts: 2118
Joined: 17 Oct 2006, 02:00
Location: Stellenbosch
Contact:

Post by Judas »

If I remember correctly you can ignore duplicates by using the DISTINCT keyword.
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

I tried adding distinct, and it did nothing to help...so dont know what else to try... :?
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

Why are you grouping? Just to have the having clause? Get rid of it!

What exactly is the purpose of Stationary_Order_Lines?

While distinct is a useful statement to you, it can be dangerous.

I've found that when there are dulicates, it is to do with the data...make sure the data is correct, and try determine why you're getting duplicates before trying distinct

ps. Please also provide all columns of all the tables...I'm pretty sure that some field you're not specifying is causing the duplication
Image
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

viceroy wrote:Why are you grouping? Just to have the having clause? Get rid of it
For the having clause...will try remove it and see what I get
viceroy wrote: What exactly is the purpose of Stationary_Order_Lines?
It contains the lines (ie items per order). 1 order can have many lines. A single item can be found in many orders from 1 user. Thats why there are "duplicate" items. From all the orders, it is getting the top 5 items...and a "duplicate" items could be in there. eg. item 1 can be in order number 2 and order number 3, so it finds "duplicate" items, but I dont want the item "duplicated" as it is meant to show the top 5 items...so an item should not be repeated.
viceroy wrote: While distinct is a useful statement to you, it can be dangerous.


I've found that when there are dulicates, it is to do with the data...make sure the data is correct, and try determine why you're getting duplicates before trying distinct
Yep, know why they are there...as explained above, they are duplicated, but they are not "duplicates" in he true sense of the word...
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

The reason they are duplicated is that you're displaying the orderid and order date. Remove those and the dupes will fall away (you may need to use distinct)
Image
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

Thanks...have seen now that I have no choice but to do a lopp ans split it over 2 sql queries unfortunately. Thought I would be able to use just 1. Oh well... thanks though. Removing those does sort it out, had to remove other fields too...
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
SBSP
Registered User
Posts: 3124
Joined: 09 May 2006, 02:00
Location: Centurion

Post by SBSP »

SilverBack wrote:Thanks...have seen now that I have no choice but to do a lopp ans split it over 2 sql queries unfortunately. Thought I would be able to use just 1. Oh well... thanks though. Removing those does sort it out, had to remove other fields too...
Why dont you or your company invest in a reporting tool ?

We use crystal reports and its very very handy,
SilverBack
Registered User
Posts: 1387
Joined: 26 Jan 2006, 02:00
Location: JHB
Contact:

Post by SilverBack »

That is an idea, thanks :wink:
Deja Moo: The feeling that you've heard this bull before.
Hire A Programmer -|- Just Source Code Blog
viceroy
Registered User
Posts: 3565
Joined: 27 Mar 2006, 02:00
Location: I forget

Post by viceroy »

SBSP wrote:
SilverBack wrote:Thanks...have seen now that I have no choice but to do a lopp ans split it over 2 sql queries unfortunately. Thought I would be able to use just 1. Oh well... thanks though. Removing those does sort it out, had to remove other fields too...
Why dont you or your company invest in a reporting tool ?

We use crystal reports and its very very handy,
Have you seen the price of Crystal licenses these days.

If you're using MSSQL, maybe investigate Reporting Services....comes free and is very powerful, esp the 2005 version
Image
Vektor
Registered User
Posts: 48
Joined: 24 Apr 2007, 02:00
Contact:

Post by Vektor »

Not so sure about this but try performing a Top-N Analysis and include the Distinct Keyword as well.
Post Reply