Results 1 to 7 of 7

Thread: [RESOLVED] SQL statement pls help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Resolved [RESOLVED] SQL statement pls help

    I have a problem on how to filter this table and have the output based on their ID. Please help thanks


    Name:  sql.bmp
Views: 116
Size:  146.1 KB
    Last edited by hoobas20; Jul 16th, 2008 at 02:12 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL statement pls help

    Thread Moved
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: SQL statement pls help

    To get the output to match on their ID's, pass the ID in the WHERE Clause. (Bases on T-SQL)

    Code:
    SELECT [Name]
    FROM TableA
    WHERE [id] = 1 
    -- Change the number in the where clause to get different names
    If you are running this SQL from a program e.g. VB6, then you can pass the id as a parameter to the SQL so that you can return different results.

    ps. Not sure if that was what you where after... if it isnt, could you explain more what you want.
    Last edited by kevchadders; Jul 15th, 2008 at 02:57 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: SQL statement pls help

    all i want is to display all that 3 id in 3 columns inonly 1 table..exactly what my example is.. Any idea? thanks

  5. #5
    Hyperactive Member
    Join Date
    Jan 2008
    Location
    Merseyside
    Posts
    456

    Re: SQL statement pls help

    This code will split them into 3 columns, but NULL's will be present. There are other ways i could of done it will NULLs showing, but i though giving you the example with a temp table as it might be usefull for you.

    It is very difficult to return the columns the way you want (without the NULL's appearing) as they vary in length.

    Code:
    CREATE TABLE #name_temps
    	(
    		field1	Varchar ( 50 ),
    		field2	Varchar ( 50 ),
    		field3	Varchar ( 50 )                
    	)
    
    -- Insert for ID = 1
    INSERT INTO #name_temps (field1)
    	SELECT name
    	FROM TableA WITH (NOLOCK) 
    	WHERE [id] = 1
    
    -- Insert for ID = 2
    INSERT INTO #name_temps (field2)
    	SELECT name
    	FROM TableA WITH (NOLOCK) 
    	WHERE [id] = 2
    
    -- Insert for ID = 3
    INSERT INTO #name_temps (field3)
    	SELECT name
    	FROM TableA WITH (NOLOCK) 
    	WHERE [id] = 3
    
    -- Select the data
    SELECT DISTINCT * FROM #name_temps
    
    -- Drop table after you have finished with it
    DROP TABLE #name_temps

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: SQL statement pls help

    What you're trying to do is create a pivot table. The bad news is that there's no standard SQL to do this. The good news is that most implementations of SQL have their own syntax for doing it. If you let us know what database you're using (including the version) the chances are someone will be able to tell you how to do it.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    166

    Re: SQL statement pls help

    thanks guys! i already solved it.. using case statement :

    heres the code :

    Code:
    select case when id = 1 then name end name1, case when id = 2 then name end name2, case when id = 3 then name end name3  from TableA
    Thanks guys!

Posting Permissions

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



Click Here to Expand Forum to Full Width