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 6591397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:25:05+00:00 2026-05-25T17:25:05+00:00

I have two tables PackageDetail and PackageDuration PackageDuration have the PackageID as Foreign Key

  • 0

I have two tables PackageDetail and PackageDuration
PackageDuration have the PackageID as Foreign Key i.e. can have multiple records with respect to PackageID

The Schema of the PackageDetail is:

PackageID  INT PK
PackageName Nvarchar(50)

Schema of the PackageDuration Table is:

DurationID INT PK
Price   Money
Duration Nvarchar(50)
PackageID INT FPK

PackageDetail tables have follwoing records:

PackageID        PackageName
  1              TestPackage
  2              MySecondPackage

PackageDuration table have following records:

DurationID      PackageID    Price        Duration
  1                 1        100          6
  2                 1        200          12
  3                 1        300          24
  4                 2        500          6

PackageDuration table can have max 3 records with one PackageID not more than this(if have ignore that)
Now I want to select the Records as in following Way:

PackageId  PackageNAme     Price1 Price2 Price3 Duration1  Duration2    Duration3
1          TestPackage        100    200    300         6         12           24
2          MySecondPackage    500   null   null         6       null         null

Please suggest me how can I achive this.

  • 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-05-25T17:25:06+00:00Added an answer on May 25, 2026 at 5:25 pm

    Another approach:

    WITH Durations AS
    (
        SELECT *, ROW_NUMBER() OVER(PARTITION BY PackageId ORDER BY DurationId) Sequence
        FROM PackageDuration 
    )
    
    SELECT  A.PackageId, B.PackageName, 
            MIN(CASE WHEN Sequence = 1 THEN Price ELSE NULL END) Price1,
            MIN(CASE WHEN Sequence = 2 THEN Price ELSE NULL END) Price2,
            MIN(CASE WHEN Sequence = 3 THEN Price ELSE NULL END) Price3,
            MIN(CASE WHEN Sequence = 1 THEN Duration ELSE NULL END) Duration1,
            MIN(CASE WHEN Sequence = 2 THEN Duration ELSE NULL END) Duration2,
            MIN(CASE WHEN Sequence = 3 THEN Duration ELSE NULL END) Duration3
    FROM Durations A
    INNER JOIN PackageDetail B
    ON A.PackageId = B.PackageId
    GROUP BY A.PackageId, B.PackageName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with a foreign key constraint how can I delete rows
I have two tables CREATE TABLE table1 (id int primary key auto_increment,....); CREATE TABLE
I have two tables: Transactions and TransactionAgents. TransactionAgents has a foreign key to Transactions
I have two tables: A (col1,col2,col3,col4) B (col1,col2,col3,col4) Table A has 4 records(rows) and
Have two sets of data (two tables) for patient records, one 1999-2003, the other
I have two tables as below: Student: oneID (primary key), col1, col2, col3, subId,
I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables: product and sale . How can I write a SQL
I have two tables with the exact same schema in SQL Server 2008 Standard
I have two tables named MEMBER - columns id(primary key), name, email & TOPICS

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.