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. Web Development
  3. ASP.NET
  4. Subqueries

Subqueries

Scheduled Pinned Locked Moved ASP.NET
databasesql-serversysadminhelp
3 Posts 2 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.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi guys, I have three different queries and i would like to put them as one huge query. This will reduce the number of times i make a connection to Sql Server and hence load time on my page. I have already managed to put two queries like:

    SELECT COUNT(Cellsmart_ussd.dbo.ussd_log.[msisdn]) AS totentries,
    (
    SELECT COUNT(ussd_clean.dbo.ster_vouchers.sent) FROM ussd_clean.dbo.ster_vouchers
    WHERE [sent] = '1' AND [sent_date] BETWEEN @start AND @end
    ) AS [totsent]
    FROM [Cellsmart_ussd].[dbo].[ussd_log]
    WHERE [request] = '*120*789#'

    But i also want the query below to be part of the two subqueries above^:

    SELECT COUNT([ussd_clean].[dbo].[unique_codes].[msisdn_pair]) AS tota FROM [ussd_clean].[dbo].[unique_codes]
    WHERE [msisdn_pair] != '' AND [used] = '1'

    Unfortunatly I can't use INNER JOINS because all these single queries refer to different databases and tables. Please help me :doh: Thanks, Morgs

    L 1 Reply Last reply
    0
    • M Morgs Morgan

      Hi guys, I have three different queries and i would like to put them as one huge query. This will reduce the number of times i make a connection to Sql Server and hence load time on my page. I have already managed to put two queries like:

      SELECT COUNT(Cellsmart_ussd.dbo.ussd_log.[msisdn]) AS totentries,
      (
      SELECT COUNT(ussd_clean.dbo.ster_vouchers.sent) FROM ussd_clean.dbo.ster_vouchers
      WHERE [sent] = '1' AND [sent_date] BETWEEN @start AND @end
      ) AS [totsent]
      FROM [Cellsmart_ussd].[dbo].[ussd_log]
      WHERE [request] = '*120*789#'

      But i also want the query below to be part of the two subqueries above^:

      SELECT COUNT([ussd_clean].[dbo].[unique_codes].[msisdn_pair]) AS tota FROM [ussd_clean].[dbo].[unique_codes]
      WHERE [msisdn_pair] != '' AND [used] = '1'

      Unfortunatly I can't use INNER JOINS because all these single queries refer to different databases and tables. Please help me :doh: Thanks, Morgs

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can join all the three queries as following.

      SELECT COUNT(Cellsmart_ussd.dbo.ussd_log.[msisdn]) AS totentries,

      (
      SELECT COUNT(ussd_clean.dbo.ster_vouchers.sent) FROM ussd_clean.dbo.ster_vouchers
      WHERE [sent] = '1' AND [sent_date] BETWEEN @start AND @end
      ) AS [totsent],

      (
      SELECT COUNT([ussd_clean].[dbo].[unique_codes].[msisdn_pair]) FROM [ussd_clean].[dbo].[unique_codes]
      WHERE [msisdn_pair] != '' AND [used] = '1'
      ) AS tota

      FROM [Cellsmart_ussd].[dbo].[ussd_log]
      WHERE [request] = '*120*789#'

      It is in the same manner as you joined the first two query. Also, please post database related question in General Database[^] section. HTH

      Jinal Desai - LIVE Experience is mother of sage....

      M 1 Reply Last reply
      0
      • L Lost User

        You can join all the three queries as following.

        SELECT COUNT(Cellsmart_ussd.dbo.ussd_log.[msisdn]) AS totentries,

        (
        SELECT COUNT(ussd_clean.dbo.ster_vouchers.sent) FROM ussd_clean.dbo.ster_vouchers
        WHERE [sent] = '1' AND [sent_date] BETWEEN @start AND @end
        ) AS [totsent],

        (
        SELECT COUNT([ussd_clean].[dbo].[unique_codes].[msisdn_pair]) FROM [ussd_clean].[dbo].[unique_codes]
        WHERE [msisdn_pair] != '' AND [used] = '1'
        ) AS tota

        FROM [Cellsmart_ussd].[dbo].[ussd_log]
        WHERE [request] = '*120*789#'

        It is in the same manner as you joined the first two query. Also, please post database related question in General Database[^] section. HTH

        Jinal Desai - LIVE Experience is mother of sage....

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        hey thanks Jinal, that code worked and am so impressed now...thanks again

        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