Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:40:07+00:00 2026-06-10T05:40:07+00:00

I am wondering if it is possible and more efficient to do something that

  • 0

I am wondering if it is possible and more efficient to do something that I am presently doing in code, to do in T-SQL instead.

I have a database with courses. Each course can have different offerings which are variations of the course at different locations and at different awards.

Here’s my (simplified) database structure and some sample data:

CREATE TABLE tblCourse (CourseId int, CourseName varchar(50))
CREATE TABLE tblOffering (OfferingId int, CourseId int, LocationId int, AwardId int)
CREATE TABLE tblLocation (LocationId int, LocationName varchar(50))
CREATE TABLE tblAward (AwardId int, AwardName varchar(50))

INSERT INTO tblCourse VALUES (1, 'Course A')
INSERT INTO tblCourse VALUES (2, 'Course B')

INSERT INTO tblOffering VALUES (1, 1, 1, 1)
INSERT INTO tblOffering VALUES (2, 1, 2, 1)
INSERT INTO tblOffering VALUES (3, 1, 3, 1)
INSERT INTO tblOffering VALUES (4, 1, 1, 2)
INSERT INTO tblOffering VALUES (5, 2, 3, 1)

INSERT INTO tblLocation VALUES (1, 'Location A')
INSERT INTO tblLocation VALUES (2, 'Location B')
INSERT INTO tblLocation VALUES (3, 'Location C')

INSERT INTO tblAward VALUES (1, 'Award A')
INSERT INTO tblAward VALUES (2, 'Award B')

What I want to retrieve from SQL is a single row for each course/award combination. Each row would have columns for each location and whether a course of that CourseId/AwardId combination was available. There would be now rows for course/award combinations that have no offerings.

The required result, from the sample data, would be a recordset like this:

CourseId | CourseName | AwardId | AwardName | LocationA | LocationB | LocationC
---------+------------+---------+-----------+-----------+-----------+----------
1        | Course A   | 1       | Award A   | True      | True      | True
1        | Course A   | 2       | Award B   | True      | NULL      | NULL
2        | Course B   | 1       | Award A   | NULL      | NULL      | True

(NULL could also be False)

At present I am doing a simple SELECT statement with various JOINS which gives me multiple rows for each course/award combination, then I loop through all rows in my code and build the required result. However, I don’t think this is so efficient as I also need to page results.

I think I could do this fairly easily in a stored procedure by creating a temporary table and a bunch of separate queries, but I don’t think that would be too efficient. Wondering if there is a better way of doing it in T-SQL???

So to clarify, what I am looking for is a T-SQL query or stored procedure that will produce the above sample recordset, and which I could adapt paging to.

NB. I am using SQL Server 2008

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:40:09+00:00Added an answer on June 10, 2026 at 5:40 am

    For Dynamic columns:

    DECLARE  @COLUMNS VARCHAR(max)
            ,@query varchar(1024)
            ,@True varchar(6)
    
    
    SELECT @COLUMNS = 
    COALESCE(
    @Columns + ',[' + L.LocationName + ']',
    '[' + L.LocationName +']'
    )
    FROM tblLocation L
    
    SELECT @True = '''True'''
    
    SELECT @QUERY = 'SELECT C.CourseName
                     ,A.AwardName
                     , pvt.*
    FROM (SELECT O.OfferingID AS OID
                ,O.AwardID AS AID
                ,O.CourseID AS CID
                ,L.LocationName AS LID
           FROM tblOffering O Inner Join tblLocation L on L.LocationID = O.LocationID) AS S
    PIVOT
    (
        count(oID) For LID IN (' +@COLUMNS+ ')
    ) As pvt
    inner join tblCourse C on C.CourseID = CID
    inner join tblAward A on A.AwardID = pvt.AID'
    
    EXEC (@QUERY)
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first time use JTree. Just wondering is it possible to have more than one
I was wondering if its possible to have one or more functions within a
I was wondering ( if possible ) if there was a program/tool/utility that when
Just wondering is it possible to build CLI app that can be run from
I am wondering if its possible to have a an (||)or or (&&)and operator
I was wondering if its possible to use the Google maps IFRAME code to
I was wondering - is it possible to change files that are stored on
I'm wondering if anyone knows if it's possible to assign more than one static
I have some code that essentially does what I want but I always like
I have something that looks like this: my $report = new ReportGenerator; #custom object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.