Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Complicated query issue: need to specify strict return

Complicated query issue: need to specify strict return

Scheduled Pinned Locked Moved Database
cssdatabasehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vodstok
    wrote on last edited by
    #1

    I have a table i am querying, and a prety odd set i need to return. the columns i am interested in are the following types: int, nvarchar, datetime,datetime. the int has a pretty normal progression, 1,2,3,4,5. the nvarchar contains only 2 possible values, we'll say red and blue. now, the data lines up something like this: 1 blue 2 blue 1 red 3 blue 2 red 3 red ie, no particular order, but every number listed in the int coulmn has a red and a blue row, so a normal query would return 1 blue 2 blue 3 blue 1 red 2 red 3 red if ordering by the colors now, the two date times are a strat date and end date, and they line up weird. the blue and red of each int do not have the same range, but are considered related. however, all of the dates for a color tie together, so if the enddate for 1 blue is 4/25/2008, then the startdate for 2 blue is 4/26/2008. same goes for red, except what i end up with is something like this 1 blue 2/23/2008 3/12/2008 1 red 2/25/2008 3/20/2008 what i need to do is write a query that returns only 2 values, where they are both less than todays date, one is blue and one is red, and they both have the same int value. now, the query i am using looks like this: select top 2 * from [table] where enddate < cast('4/15/2008 12:59:59.999' as datetime) order by enddate desc this particular one is right on the cusp of one of the transition dates, and the result i am getting is: 1 red 2 blue when i need 1 red 1 blue or 2 red 2 blue. the int must always be the same and the nvarchar must always be different. is there a way to force this in a query? what i end up needing is a query that returns

    ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

    P A 2 Replies Last reply
    0
    • V Vodstok

      I have a table i am querying, and a prety odd set i need to return. the columns i am interested in are the following types: int, nvarchar, datetime,datetime. the int has a pretty normal progression, 1,2,3,4,5. the nvarchar contains only 2 possible values, we'll say red and blue. now, the data lines up something like this: 1 blue 2 blue 1 red 3 blue 2 red 3 red ie, no particular order, but every number listed in the int coulmn has a red and a blue row, so a normal query would return 1 blue 2 blue 3 blue 1 red 2 red 3 red if ordering by the colors now, the two date times are a strat date and end date, and they line up weird. the blue and red of each int do not have the same range, but are considered related. however, all of the dates for a color tie together, so if the enddate for 1 blue is 4/25/2008, then the startdate for 2 blue is 4/26/2008. same goes for red, except what i end up with is something like this 1 blue 2/23/2008 3/12/2008 1 red 2/25/2008 3/20/2008 what i need to do is write a query that returns only 2 values, where they are both less than todays date, one is blue and one is red, and they both have the same int value. now, the query i am using looks like this: select top 2 * from [table] where enddate < cast('4/15/2008 12:59:59.999' as datetime) order by enddate desc this particular one is right on the cusp of one of the transition dates, and the result i am getting is: 1 red 2 blue when i need 1 red 1 blue or 2 red 2 blue. the int must always be the same and the nvarchar must always be different. is there a way to force this in a query? what i end up needing is a query that returns

      ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Change your order by to include the number field as the first portion before the enddate, e.g. order by id, enddate.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      • V Vodstok

        I have a table i am querying, and a prety odd set i need to return. the columns i am interested in are the following types: int, nvarchar, datetime,datetime. the int has a pretty normal progression, 1,2,3,4,5. the nvarchar contains only 2 possible values, we'll say red and blue. now, the data lines up something like this: 1 blue 2 blue 1 red 3 blue 2 red 3 red ie, no particular order, but every number listed in the int coulmn has a red and a blue row, so a normal query would return 1 blue 2 blue 3 blue 1 red 2 red 3 red if ordering by the colors now, the two date times are a strat date and end date, and they line up weird. the blue and red of each int do not have the same range, but are considered related. however, all of the dates for a color tie together, so if the enddate for 1 blue is 4/25/2008, then the startdate for 2 blue is 4/26/2008. same goes for red, except what i end up with is something like this 1 blue 2/23/2008 3/12/2008 1 red 2/25/2008 3/20/2008 what i need to do is write a query that returns only 2 values, where they are both less than todays date, one is blue and one is red, and they both have the same int value. now, the query i am using looks like this: select top 2 * from [table] where enddate < cast('4/15/2008 12:59:59.999' as datetime) order by enddate desc this particular one is right on the cusp of one of the transition dates, and the result i am getting is: 1 red 2 blue when i need 1 red 1 blue or 2 red 2 blue. the int must always be the same and the nvarchar must always be different. is there a way to force this in a query? what i end up needing is a query that returns

        ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

        A Offline
        A Offline
        A Wong
        wrote on last edited by
        #3

        Didn't actually try running this, but you can give it a whirl: select top 2 a.* from [table] a INNER JOIN [table] b ON a.int = b.int AND a.nvarchar <> b.nvarchar AND b.enddate < cast('4/15/2008 12:59:59.999' as datetime) where a.enddate < cast('4/15/2008 12:59:59.999' as datetime) order by enddate desc

        V 1 Reply Last reply
        0
        • A A Wong

          Didn't actually try running this, but you can give it a whirl: select top 2 a.* from [table] a INNER JOIN [table] b ON a.int = b.int AND a.nvarchar <> b.nvarchar AND b.enddate < cast('4/15/2008 12:59:59.999' as datetime) where a.enddate < cast('4/15/2008 12:59:59.999' as datetime) order by enddate desc

          V Offline
          V Offline
          Vodstok
          wrote on last edited by
          #4

          your code is a thing of beauty :) i modified it to run against the table i was using and was getting the same results i was getting before, whihc confused me because looking at it, it definatley said exaclty what i needed. so i opened a new view and plugged it in and started playing with it. the first thing i did was change it to show the results from a and b, and bingo, there it was. with this, i dont need the top 2 rows, just the top 1 with the nd date set to order by desc, and tada, i had the exact results i was looking for. so here is what i used: SELECT TOP (1) a.nvarchar, b.nvarchar AS nvarchar2, b.int, a.int AS int2, a.StartDate, b.StartDate AS StartDate2, a.EndDate, b.EndDate AS EndDate2 FROM [table] AS a INNER JOIN [table] AS b ON a.int = b.int AND a.nvarchar <> b.nvarchar AND b.EndDate < getdate() AND a.EndDate < GETDATE() ORDER BY int2 DESC Thank you very much for this, you saved me a massive headache. And thank you to everyone that replied, i appreciate any input :D

          ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups