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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need some advices on SQL INTERSECT Function

Need some advices on SQL INTERSECT Function

Scheduled Pinned Locked Moved Database
databasequestion
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.
  • J Offline
    J Offline
    James Shao
    wrote on last edited by
    #1

    Hi guys, I am currently trying to create a powerful stock market scanner, like the one Yahoo or Google has.   The scanner will allow users to choose among some 50 parameters to scan through several thousand stocks. I am not sure how Yahoo or Google does it, but the only way I know is by using a SQL INTERSECT query.   The query statement probably needs to be reconstructed each time an user adds additional parameter.   So when a parameter is added, I'll simply append "INTERSECT [new sql query goes here]" into one big long query string.   ;) Conceptually, it'll be like this: SQL Query 1 (parameter 1) INTERSECT SQL Query 2 (parameter 2) INTERSECT SQL Query 3 (parameter 3) INTERSECT .. .. Is this method the best approach to create a scanner?   I thought all other methods (INNER JOIN, Views, CTEs, nested query) would result in over-complicated query strings. Or is there a better method (the scanner in Yahoo/Google seems to run really fast)?   :rolleyes: I'd greatly appreciate any suggestions!!   Thanks as always!

    D M 2 Replies Last reply
    0
    • J James Shao

      Hi guys, I am currently trying to create a powerful stock market scanner, like the one Yahoo or Google has.   The scanner will allow users to choose among some 50 parameters to scan through several thousand stocks. I am not sure how Yahoo or Google does it, but the only way I know is by using a SQL INTERSECT query.   The query statement probably needs to be reconstructed each time an user adds additional parameter.   So when a parameter is added, I'll simply append "INTERSECT [new sql query goes here]" into one big long query string.   ;) Conceptually, it'll be like this: SQL Query 1 (parameter 1) INTERSECT SQL Query 2 (parameter 2) INTERSECT SQL Query 3 (parameter 3) INTERSECT .. .. Is this method the best approach to create a scanner?   I thought all other methods (INNER JOIN, Views, CTEs, nested query) would result in over-complicated query strings. Or is there a better method (the scanner in Yahoo/Google seems to run really fast)?   :rolleyes: I'd greatly appreciate any suggestions!!   Thanks as always!

      D Offline
      D Offline
      Dimitri Witkowski
      wrote on last edited by
      #2

      Have you tried this: SELECT ... WHERE (parameter 1) AND (parameter 2) AND (parameter 3) ... To speed up this, you need build an index on the fields you want to filter by.

      See my article about Windows 7 Taskbar timer here on CodeProject

      1 Reply Last reply
      0
      • J James Shao

        Hi guys, I am currently trying to create a powerful stock market scanner, like the one Yahoo or Google has.   The scanner will allow users to choose among some 50 parameters to scan through several thousand stocks. I am not sure how Yahoo or Google does it, but the only way I know is by using a SQL INTERSECT query.   The query statement probably needs to be reconstructed each time an user adds additional parameter.   So when a parameter is added, I'll simply append "INTERSECT [new sql query goes here]" into one big long query string.   ;) Conceptually, it'll be like this: SQL Query 1 (parameter 1) INTERSECT SQL Query 2 (parameter 2) INTERSECT SQL Query 3 (parameter 3) INTERSECT .. .. Is this method the best approach to create a scanner?   I thought all other methods (INNER JOIN, Views, CTEs, nested query) would result in over-complicated query strings. Or is there a better method (the scanner in Yahoo/Google seems to run really fast)?   :rolleyes: I'd greatly appreciate any suggestions!!   Thanks as always!

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        I rarely use intersect, so this may not be the best advice but. The above would result in 3 select statements against the same data structure, the results would then be compared and the intersection returned. As Dmitry suggested stick it in a where clause. You can almost certainly define the set of allowed parameters so some thing like this will do the trick:

        ALTER PROC [DocumentsForEntity]
        --DECLARE
        @FleetID int,
        @RoomID INT,
        @ShipID INT,
        @AutomationID int,
        @CostumeID INT,
        @ShowID INT
        AS
        --SET @FleetID = 1
        SELECT
        *
        FROM dbo.vwDocument
        CROSS JOIN dbo.Settings s

        WHERE s.SettingCode = 'DocumentURL'
        AND (ISNULL(@FleetID,0) = 0 OR FleetID = @FleetID)
        AND (ISNULL(@RoomID,0) = 0 or RoomID = @Roomid)
        AND (ISNULL(@ShipID,0) = 0 or ShipID = @ShipID)
        AND (ISNULL(@AutomationID,0) = 0 or AutomationID = @AutomationID)
        AND (ISNULL(@CostumeID,0) = 0 or CostumeID = @CostumeID)
        AND (ISNULL(@ShowID,0) = 0 or ShowID = @ShowID)

        ORDER BY DocumentType,DocName

        The user can then select 1 or more of the parameters.

        Never underestimate the power of human stupidity RAH

        J 1 Reply Last reply
        0
        • M Mycroft Holmes

          I rarely use intersect, so this may not be the best advice but. The above would result in 3 select statements against the same data structure, the results would then be compared and the intersection returned. As Dmitry suggested stick it in a where clause. You can almost certainly define the set of allowed parameters so some thing like this will do the trick:

          ALTER PROC [DocumentsForEntity]
          --DECLARE
          @FleetID int,
          @RoomID INT,
          @ShipID INT,
          @AutomationID int,
          @CostumeID INT,
          @ShowID INT
          AS
          --SET @FleetID = 1
          SELECT
          *
          FROM dbo.vwDocument
          CROSS JOIN dbo.Settings s

          WHERE s.SettingCode = 'DocumentURL'
          AND (ISNULL(@FleetID,0) = 0 OR FleetID = @FleetID)
          AND (ISNULL(@RoomID,0) = 0 or RoomID = @Roomid)
          AND (ISNULL(@ShipID,0) = 0 or ShipID = @ShipID)
          AND (ISNULL(@AutomationID,0) = 0 or AutomationID = @AutomationID)
          AND (ISNULL(@CostumeID,0) = 0 or CostumeID = @CostumeID)
          AND (ISNULL(@ShowID,0) = 0 or ShowID = @ShowID)

          ORDER BY DocumentType,DocName

          The user can then select 1 or more of the parameters.

          Never underestimate the power of human stupidity RAH

          J Offline
          J Offline
          James Shao
          wrote on last edited by
          #4

          Thank you guys, I've considered the WHERE clause, but each filter criteria already looks very complex (with 3 to 4 nested Select, subquery statements, a lot of nested WHERE statements too); so I thought using WHERE would make it too complicated for SQL to handle.

          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