Coding Thread, SQL Magic Needed in Coding and Web Development; Hi All,
I am in need of some sql magic,
i have a table with the follow columns
teacher , ...
-
11th June 2009, 03:43 PM #1 SQL Magic Needed
Hi All,
I am in need of some sql magic,
i have a table with the follow columns
teacher , subject ,english, maths, science
mr smith, science ,27 , 28 , 30
i would like to return the following
mr smith, science,30
is this possible with sql in access?
i would prefer doing this in sql and not vba.
-
-
IDG Tech News
-
11th June 2009, 03:53 PM #2 Code:
SELECT teacher, subject, CASE subject
WHEN 'science' THEN science
WHEN 'maths' THEN maths
WHEN 'english' THEN english
END
FROM table
That might do it. I assumed you didn't want a simple 'SELECT teacher, subject, science FROM table' query.
Last edited by jamesb; 11th June 2009 at 03:55 PM.
-
-
11th June 2009, 04:15 PM #3 thanks but there will be upto 20 subjects and i would like only one query.
-
-
11th June 2009, 04:18 PM #4 You need to normalise your database, storing the reference to a column within the same table as that column is not good practice from what I know.
Would it not be better to store the column with science/maths etc in it with an id linking it to the original table not be better? Then you can do a simple nested SELECT?
-
-
11th June 2009, 04:26 PM #5 Well, doing it that way would give you a list of teacher, subject, subject_mark for each teacher quite happily, but it would involve typing out each subject in the CASE statement. Can't find anything to simply pull the CASE statement out of the table itself but I'll keep looking.
(Note: Started typing this before reading localzuk's reply, so it has no bearing on that)
Last edited by jamesb; 11th June 2009 at 04:29 PM.
-
-
11th June 2009, 04:44 PM #6 hi thanks for your ideas.
it wont be possible to normalise the database as im pulling most of the data from anthor database which i dont have control and trying to make the best of a crap database.
i thought about a case statement but would be hard to keep upto date.
I think i will have to do it via vba.
-
-
11th June 2009, 07:04 PM #7
- Rep Power
- 14
I'll start by saying I don't know the answer (!)
However, I wonder if it's possible to get the list of current subjects in the database using a SELECT DISTINCT query, then loop through the results of that running a second query for each subject returned. If so, that wouldn't need updating if the subjects changed.
I've never ever used them, but would stored procedures be able to handle this?
Article here that has the basics on stored procedures.
Just a thought.
-
-
11th June 2009, 07:55 PM #8 I have been putting of sorting out something like this in teh Serco Facility DB.
Anyhow this great article gives some pointers to how you might do something like this
Arrays and Lists in SQL Server 2005
-
-
14th June 2009, 07:22 AM #9 Is your source table stored in Access or MS-SQL? Probably the best way is to have a bit of VB code that will read in the un-normalised data and just populate a more normalised table (i.e. with the output in the form you're after) in code.
-
SHARE: 
Similar Threads
-
By laserblazer in forum Jokes/Interweb Things
Replies: 0
Last Post: 28th March 2009, 04:36 PM
-
By ZeroHour in forum General Chat
Replies: 3
Last Post: 28th March 2008, 07:18 PM
-
By Grommit in forum Windows
Replies: 12
Last Post: 22nd October 2007, 07:15 PM
-
By tech_guy in forum General Chat
Replies: 19
Last Post: 11th October 2007, 09:20 PM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
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