+ Reply to Thread
Results 1 to 12 of 12

Thread: SQL select where case statement!

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    SQL select where case statement!

    Hi

    I need to SQL select a number from a table when a different table holds a certain value

    e.g.

    select number1 from table (table1) when value1 in table2 = true
    select number2 from table (table1) when value2 in table2 = true
    select number3 from table (table1) when value3 in table2 = true

    thanks

  2. #2
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    Is this even possible?


    i know it is for the same table but is it possible for a case to be in a separate one?

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: SQL select where case statement!

    Your question is unclear to me. Where/how are you attempting to do this? What, if any, link is there between the two tables?

  4. #4
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    well i have an access database that i connect to using

    sNWind = _
          "M:\Television and Broadband\CHANNEL ECONOMICS\Database\Database Actual\ChannelEconomics_Q3_2010.accdb"
          
       conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
          sNWind & ";"
    
    
    and the the sql statement would be something like
    
    rst.Open "SELECT [VALUE A] from TABLE A when [VALUE B] from TABLE B = 'TRUE'
    all i want is some sql that gets a certain value from a table when a value in a seperate table is true..

    for example
    when value A from Table A is true
    get Value B from Table B


    the tables are not physically connected!

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: SQL select where case statement!

    So you only have one record in table B?

  6. #6
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    no, i have a load of different information in table B, but for this instance i'm only interested in one value! I see why this is confusing now, i have missed out a bit of information

    i should say something like
    [code}
    IF (FOR ID x in TABLE B), VALUE B = True, THEN GET VALUE 1 From TABLE A where ID = Y
    IF (FOR ID x in TABLE B), VALUE C = True, THEN GET VALUE 2 From TABLE A where ID = Y
    IF (FOR ID x in TABLE B), VALUE A = True, THEN GET VALUE 3 From TABLE A where ID = Y
    [/code]

  7. #7
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: SQL select where case statement!

    So the two tables are in fact linked (by ID)

    You want something like:

    Select (IIf(B.[ValueB],A.[Value1],IIf(B.[ValueC],A.[Value2],IIf(B.[ValueA],A.[Value3],""))) As MyData FROM [Table A] A, [Table B] B WHERE A.ID = B.ID

    Not that this has anything at all to do with Excel...

  8. #8
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    Hmm not quite, the ID'S are different, but defined buy a user input, the query is then run lots of times with one the id's varied to extract a bunch of data on it. Thanks for your help, i may be able to work the rest out

  9. #9
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    as far as i can tell this is the right code:

    rst.Open "Select (Iif(3D from ChannelBrand = 'true') then ([3D Subscribers] from OperatorDetail FROM OperatorDetail WHERE [Operator Name] = 'WARNER' AND Country = '" & cntry & "'), (Iif(HD from ChannelBrand = 'true') then ([HD Subscribers] from OperatorDetail FROM OperatorDetail WHERE [Operator Name] = 'WARNER' AND Country = '" & cntry & "'),(Iif(3D from ChannelBrand = 'true') then ([3D Subscribers] from OperatorDetail FROM OperatorDetail WHERE [Operator Name] = 'WARNER' AND Country = '" & cntry & "'),(Iif([Digtal/Analogue] from package = 'digital') then ([Digital Subscribers] from OperatorDetail FROM OperatorDetail WHERE [Operator Name] = 'WARNER' AND Country = '" & cntry & "'),(Iif([Digtal/Analogue] from package = 'analogue') then ([Analogue Subscribers] from OperatorDetail FROM OperatorDetail WHERE [Operator Name] = 'WARNER' AND Country = '" & cntry & "')", conn
    oh god

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: SQL select where case statement!

    I doubt it because that isn't valid SQL.

  11. #11
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: SQL select where case statement!

    okeydokey.. could you point me towards the right code please

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: SQL select where case statement!

    No, since I have no more idea what you are doing than I had when I posted my last attempt. I also still don't know why you are posting this in an Excel forum?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0