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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:48:08+00:00 2026-06-09T16:48:08+00:00

I have a table similar to below Date |field1 | qty1 | qty2 |

  • 0

I have a table similar to below

Date   |field1  | qty1 | qty2 | qty3
1 Aug  | xyz    | 0    | 0    | 3
1 Aug  | xyz    | 3    | 0    | 0
1 Aug  | abc    | 0    | 5    | 0
2 Aug  | abc    | 0    | 15   | 0
2 Aug  | xyz    | 0    | 12   | 0
2 Aug  | xyz    | 5    | 0    | 0

I have written three stored procedures to display each quantity separately as below.

This is my first procedure

create procedure firstprocedure 
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty1) as XYZ,
sum (case when field1 = 'abc', qty1) as ABC 
from table1
where date between @startdate and @enddate
group by date

This is my second procedure

Create procedure secondprocedure
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty2) as XYZ,
sum (case when field1 = 'abc', qty2) as ABC 
from table1
where date between @startdate and @enddate
group by date

This is my third procedure

Create procedure thirdprocedure 
@startdate datetime, @enddate datetime
As
select date, sum (case when field1 = 'xyz', qty3) as XYZ,
sum (case when field1 = 'abc', qty3) as ABC 
from table1
where date between @startdate and @enddate
group by date

I wonder is there any chance I just place the columns(whether qty1, qty2, or qty3) in parameters and while executing just mention if I want qty1 or qty2 or qty3. It should produce output accordingly.

  • 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-09T16:48:10+00:00Added an answer on June 9, 2026 at 4:48 pm

    try this:

    create a procedure like this:

    create procedure my_procedure(@startdate datetime, @enddate datetime,@qty_type varchar(50))
    as
    begin 
        with cte as( 
        select 'qty1' [col_name],date, sum (case when field1 = 'xyz' then qty1 end) as XYZ,
        sum (case when field1 = 'abc' then qty1 end) as ABC 
        from table1
        where date between @startdate and @enddate
        group by date
        union all 
        select 'qty2' [col_name],date, sum (case when field1 = 'xyz' then qty2 end) as XYZ,
        sum (case when field1 = 'abc' then qty2 end) as ABC 
        from table1
        where date between @startdate and @enddate
        group by date
        union all
        select 'qty3' [col_name],date, sum (case when field1 = 'xyz' then qty3 end) as XYZ,
        sum (case when field1 = 'abc' then qty3 end) as ABC 
        from table1
        where date between @startdate and @enddate
        group by date
        )select * from cte where [col_name]=@qty_type
    end
    

    then execute it with:

    exec '2012-04-01','2012-05-01','qty1'
    

    or

    exec '2012-04-01','2012-05-01','qty2'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table similar to below Date | institution | qty1 | qty2
I have a table similar to below in sql server 2005 date_column | field1
I have a table similar to the example shown below. I would like to
Basically I have a table that has similar format to the below table. What
I have a table very similar to the one below. p1 and p2 on
I have a table similar to the one below: ID Vote_Up Vote_Down Vote_Total 1
I have a table that is similar to the following below: id | cat
I have a sql table similar to the one below. And I want to
I have a table with data similar to below: Group TimePoint Value 1 0
I have a sql stored procedure similar to below : create procedure procedurename declare

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.