Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old August 4th, 2003, 04:41 PM
jmennen jmennen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jmennen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Case / Order By

How would I change this query that I am running from my servlet so that it takes out the duplicates of B and R just like it is currently doing, but then also combines E and V and removes their duplicates and orders them by E then V? I can add them together with the following code, but cannot order them separately. Any ideas? The order should be B, R, E, V with B and R unioned and E and V unioned but seperated in the order. Currently it is B and R bunched together followed by E and V bunched together. E needs to come before V.

SELECT DISTINCT
a.itemtype
, c.textid
, c.min
, c.max
, rtrim(c.header) as header
, CASE a.itmlabel
WHEN 'B' THEN 1
WHEN 'R' THEN 1
WHEN 'E' THEN 2
WHEN 'V' THEN 2
END ORDERING_COLUMN FROM
RTC_global.productLibrary.vdepts d
, RTC_global.productLibrary.item a
, RTC_global.productLibrary.text c
WHERE
a.priceapproved = 'Y'
AND d.deptcode = -- tempInt.parseInt(dept[0])
AND d.classcode = -- tempInt.parseInt(classCode[0])
AND d.subcode = -- tempInt.parseInt(subCode[0])
AND a.dept = d.dept
AND a.subdept = d.subdept
AND a.class = d.class
AND a.subclass = d.subclass
AND a.textid = c.textid
AND c.status = 'A' ORDER BY ORDERING_COLUMN

Reply With Quote
  #2  
Old August 4th, 2003, 09:48 PM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 5 m 34 sec
Reputation Power: 20
Please format your SQL!

I think a simple change is all you need:

Code:
SELECT DISTINCT a.itemtype, c.textid, c.min,
    c.max, rtrim(c.header) as header,
    CASE a.itmlabel 
        WHEN 'B' THEN 1 
        WHEN 'R' THEN 2 
        WHEN 'E' THEN 3 
        WHEN 'V' THEN 4 
    END ORDERING_COLUMN
FROM RTC_global.productLibrary.vdepts d,
    RTC_global.productLibrary.item a,
    RTC_global.productLibrary.text c 
WHERE a.priceapproved = 'Y' 
    AND d.deptcode = -- tempInt.parseInt(dept[0]) 
    AND d.classcode = -- tempInt.parseInt(classCode[0]) 
    AND d.subcode = -- tempInt.parseInt(subCode[0]) 
    AND a.dept = d.dept 
    AND a.subdept = d.subdept 
    AND a.class = d.class 
    AND a.subclass = d.subclass 
    AND a.textid = c.textid 
    AND c.status = 'A'
ORDER BY ORDERING_COLUMN
__________________

Left DevShed May 28, 2005. Reason: Unresponsive administrators.
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old August 4th, 2003, 10:31 PM
jmennen jmennen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jmennen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
No, I tried that but it does not work. There are four possible values for itmlabel, 'R', 'B', 'E', and 'V'. There is another field called textid that needs to be distinct, but in some cases there are multiple instances of a textid because it will match up with more than one itmlabel. The textids with itmlabel B and R need to be combined and the textids with itmlabel E and V need to be combined. They then need to be ordered B, R, E, V.


Thanks in advance for the help.

Reply With Quote
  #4  
Old August 5th, 2003, 04:54 AM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 5 m 34 sec
Reputation Power: 20
I will need to see your data. Can you put it on a web site or is it too much?

Reply With Quote
  #5  
Old August 5th, 2003, 08:05 AM
jmennen jmennen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jmennen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For simplicity, lets say I have a table with these columns:

textid, itmlabel

Then here is some sample data:

111222,B
111222,R
111223,E
111224,V
111233,E
111233,V
111255,R
111266,V
111300,E
111301,E
111301,V
111655,B
111666,B
111666,R

I then need to combine / remove duplicates between B and R and then E and V. I then need to order B, R, E, V.

Reply With Quote
  #6  
Old August 5th, 2003, 09:15 AM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 5 m 34 sec
Reputation Power: 20
do a two step

I don't know what database you are working in, but they should all support temporary tables. I would select from your sample data something like this:

select distinct colA, colB
into #tmpTable <-- this works for MS SQLServer and Sybase
from sampleTable

Then do your join on the temp table with the modified query I sent earlier (you can create indexes on temp tables if you have enough data that it is worth while). Temporary tables often make for much more maintanable code as someone comming after you can clearly see what you intend. It also can produce dramatic improvements in performance, I have seen as much as two orders of magnitude speedup. Temp tables, in case you were not aware, exist only as long as the connection is open (database connection, not recordset connection) so you can reuse them if you like. If you put your query into a stored procedure (my favorite for all but the most simple queries), the temp table disappears as soon as the proc terminates.

Reply With Quote
  #7  
Old August 5th, 2003, 09:57 AM
jmennen jmennen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 5 jmennen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am using DB2 and due to the requirements, will be unable to work with a temporary table. I need one SQL statement that can accomplish the task.

Reply With Quote
  #8  
Old August 5th, 2003, 10:07 AM
mitakeet's Avatar
mitakeet mitakeet is offline
Last Day: May 28, 2005
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 4,575 mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level)mitakeet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 5 m 34 sec
Reputation Power: 20
I don't know anything about db2, but in Sybase and MSSqlServer you can create temp tables in regular SQL statments submitted to the server.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Case / Order By


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway