Results 1 to 6 of 6

Thread: Simple SQL question

  1. #1
    Pixel Abuser Spunkey's Avatar
    Join Date
    Nov 2003
    Location
    Milton Keynes
    Posts
    1,523
    Thanks
    0
    Thanked
    0 times in 0 posts

    Simple SQL question

    Simple question:
    In the query
    Code:
    SELECT Value, 'DummyValue' = IncrementingVariable FROM Table
    How can you order by 'DummyValue'?

    Same question, but with the way I need it...
    In the query
    Code:
    DECLARE @Client_Code varchar(30)
    DECLARE @Campaign_Code varchar(30)
    DECLARE @Catalogue_ID int
    DECLARE @Section_ID int
    DECLARE @Points int
    DECLARE @Country_Code int
    DECLARE @SortOrder int
    SET @Client_Code = 'XQX'
    SET @Campaign_Code = 'DEM'
    SET @Catalogue_ID = '151'
    SET @Section_ID = '177'
    SET @Points = '999999'	
    SET @Country_Code = '1'
    SET @SortOrder = '1'
    	
    
    	SELECT Products.ID, Products.Name, 'Points' = 
    			CASE 
    				WHEN EXISTS (
    					SELECT cPoints_Value FROM tblAward_Value WITH (NOLOCK) 
    					WHERE cAward_Code = Products.ID 
    						AND cClient_Code = @Client_Code 
    						AND cCampaign_Code = @Campaign_Code 
    						AND cCountry_Code = @Country_Code
    						AND cPoints_Value = @Points) 
    					THEN (SELECT cPoints_Value FROM tblAward_Value WITH (NOLOCK) 
    						WHERE cAward_Code = Products.ID 
    							AND cClient_Code = @Client_Code 
    							AND cCampaign_Code = @Campaign_Code 
    							AND cCountry_Code = @Country_Code
    							AND cPoints_Value = @Points)
    				ELSE (SELECT cPoints_Value FROM tblAward_Value WITH (NOLOCK) 
    					WHERE cAward_Code = Products.ID 
    						AND cClient_Code = 'PMM' 
    						AND cCampaign_Code = 'PMM' 
    						AND cCountry_Code = @Country_Code
    						AND cPoints_Value = @Points)
    			END
    	FROM Products 
    		INNER JOIN Product_Display ON Products.ID = Product_Display.Product_ID 
    	WHERE Product_Display.Client_Code LIKE @Client_Code
    		AND Product_Display.Campaign_Code LIKE @Campaign_Code
    		AND Product_Display.Catalogue_ID = @Catalogue_ID
    		AND Product_Display.Section_ID = @Section_ID
    	ORDER BY CASE @SortOrder -- varchar fields
    			WHEN 1 THEN Products.Name	
    			ELSE Products.Name
    		END
    How can you order by 'Points'?

    TIA

  2. #2
    Sublime HEXUS.net
    Join Date
    Jul 2003
    Location
    The Void.. Floating
    Posts
    11,819
    Thanks
    213
    Thanked
    233 times in 160 posts
    • Stoo's system
      • Motherboard:
      • Mac Pro
      • CPU:
      • 2*Xeon 5450 @ 2.8GHz, 12MB Cache
      • Memory:
      • 32GB 1600MHz FBDIMM
      • Storage:
      • ~ 2.5TB + 4TB external array
      • Graphics card(s):
      • ATI Radeon HD 4870
      • Case:
      • Mac Pro
      • Operating System:
      • OS X 10.7
      • Monitor(s):
      • 24" Samsung 244T Black
      • Internet:
      • Zen Max Pro
    Order by 3

    isn't it?

    3rd item selected?
    (\__/)
    (='.'=)
    (")_(")

  3. #3
    Pixel Abuser Spunkey's Avatar
    Join Date
    Nov 2003
    Location
    Milton Keynes
    Posts
    1,523
    Thanks
    0
    Thanked
    0 times in 0 posts
    tried that one

    dynamic order by's are really naff. They really restrict the useablity of the order by. I have been looking for a function to pass the column ID into but no joy.

    I think I'm just going to have to do
    If Sortorder = 2
    SELECT.... ORDER BY Points
    Else
    SELECT.... ORDER BY Products.Name

    Crappy I know, but it works. And it avoids having to use GetRows in the ASP code this is built on, which is a result as far as I'm concerned

    Cheers Stoo

  4. #4
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    43
    Thanked
    47 times in 42 posts
    As you say cross tab's reports cause you to have restrictions imposed on the order by.

    You may consider breaking the query down into sub parts and doing multiple queries to get the data set and orders you want.

    Its blooming difficult to help you as this data set is complicated, CrossTab's are difficult to imagine too without some underlying data to back up the query.

    TiG
    -- Hexus Meets Rock! --

  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    95
    Thanks
    0
    Thanked
    1 time in 1 post
    SELECT Value, 'Points' = Big sub query natyness AS foo FROM Table ORDER BY foo

    adding a column alias to your 'Points' should do it I think

    HT

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    The main approach that springs to mind is to build a view around selecting with the derived value, and then build simple order by clauses in the final select statement. This also can help optimize the code a little by storing the definition files on the server for this view, rather than recreating them when called.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 13-05-2005, 01:01 PM
  2. Simple question
    By steve threlfall in forum Help! Quick Relief From Tech Headaches
    Replies: 4
    Last Post: 12-12-2004, 07:22 PM
  3. simple html question
    By Jimmy Little in forum Software
    Replies: 9
    Last Post: 20-09-2004, 10:27 AM
  4. Simple question
    By MAS in forum PC Hardware and Components
    Replies: 10
    Last Post: 22-08-2004, 08:31 PM
  5. Simple network question...(Do not remove from GD plz)
    By TomWilko in forum Networking and Broadband
    Replies: 14
    Last Post: 25-11-2003, 02:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •