|
-
Jul 15th, 2008, 01:28 AM
#1
Thread Starter
Addicted Member
[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
Last edited by hoobas20; Jul 16th, 2008 at 02:12 AM.
-
Jul 15th, 2008, 02:30 AM
#2
Re: SQL statement pls help
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 15th, 2008, 02:52 AM
#3
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.
-
Jul 15th, 2008, 03:19 AM
#4
Thread Starter
Addicted Member
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
-
Jul 15th, 2008, 03:52 AM
#5
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
-
Jul 15th, 2008, 06:52 AM
#6
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
-
Jul 16th, 2008, 02:10 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|