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

  • Home
  • SEARCH
  • 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 8420287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:51:09+00:00 2026-06-10T02:51:09+00:00

There are 4 tables: Suppl , fields: (Code_name, Code_name_arch, Tasknum, Pki_num, Group_eng, Name, Descr,

  • 0

There are 4 tables:

  • Suppl, fields: (Code_name, Code_name_arch, Tasknum, Pki_num, Group_eng, Name, Descr, Cost, Quan, shop);

  • Maker, fields : (Code_maker, Code_maker_arch, Code_name, provider);

  • Arrival, fields: (Code_arr, Code_maker, quan_arr);

  • Accounts, fields: (Code_acc, Code_maker, num_acc, quan_acc, summ)

my query is:

ALTER Procedure [dbo].[pr_tblz] As
Set NOCOUNT ON
Declare @task VARCHAR(4000)
Select @task = coalesce(@task + ',[' + [Tasknum] + ']',
'[' + [Tasknum] + ']')
FROM [db_pki].[dbo].[Suppl]
Group BY [Tasknum]
Order BY [Tasknum]
Declare @query VARCHAR(8000)
Set @query='
Alter View Amountzz As
SELECT Shop, Name, Desc, Group_eng as GI, '+ @task +', quan_arr As specif, quan_acc As acns 
FROM 
(
select
Shop, Name, Desc, Group_eng, Tasknum, Quan, quan_arr, quan_acc
from [db_pki].[dbo].[Suppl] as deliveries
LEFT JOIN [db_pki].[dbo].[Maker] ON (deliveries.[Code_name] = [db_pki].[dbo].[Maker].[Code_name])
LEFT JOIN [db_pki].[dbo].[Arrival] ON ([db_pki].[dbo].[Maker].[Code_maker] = [db_pki].[dbo].[Arrival].[Code_maker])
LEFT JOIN [db_pki].[dbo].[Accounts] ON ([db_pki].[dbo].[Maker].[Code_maker] = [db_pki].[dbo].[Accounts].[Code_maker])
)date_to_pivot
PIVOT 
(
Max([Quan])
For [Tasknum]
IN (' + @task + ')
 )AS p'
Execute (@query)

result:

Shop      Name     Descr    GI    n1     n2      n3 ...    specif    acns
1         name1      1       5    4                           1        1
2 10      name2      2       3    8                           2        2
3         name3      3       501         11                   3        3
1 8       name1      1       5           16                   7        10
a 2 10    name2      2       3                   3            5        6
5         name1      1       2                   5            6        3

How can I get the following result?

Shop      Name     Descr    GI    n1     n2      n3 ...    specif    acns
1 8       name1      1       5    4      16                 8(1+7)     11
a 2 10    name2      2       3    8              3            7        8(2+6)
3         name3      3      501          11                   3        3
5         name1      1       2                   5            6        3

n1, n2, n3,… – Tasknum

  • 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-10T02:51:10+00:00Added an answer on June 10, 2026 at 2:51 am

    Not tested. Try aggregating the values of Shop, specif and acns in the subselect that pulls the columns before the pivoting, like this (the necessary changes are highlighted in bold):

    ALTER Procedure [dbo].[pr_tblz] As
    Set NOCOUNT ON
    Declare @task VARCHAR(4000)
    Select @task = coalesce(@task + ',[' + [Tasknum] + ']',
    '[' + [Tasknum] + ']')
    FROM [db_pki].[dbo].[Suppl]
    Group BY [Tasknum]
    Order BY [Tasknum]
    Declare @query VARCHAR(8000)
    Set @query='
    Alter View Amountzz As
    SELECT Shop, Name, Desc, Group_eng as GI, '+ @task +', quan_arr As specif, quan_acc As acns 
    FROM 
    (
    select
    MAX(Shop    ) OVER (PARTITION BY Name) AS Shop,
    Name, Desc, Group_eng, Tasknum, Quan,
    SUM(quan_arr) OVER (PARTITION BY Name) AS quan_arr,
    SUM(quan_acc) OVER (PARTITION BY Name) AS quan_acc
    from [db_pki].[dbo].[Suppl] as deliveries
    LEFT JOIN [db_pki].[dbo].[Maker] ON (deliveries.[Code_name] = [db_pki].[dbo].[Maker].[Code_name])
    LEFT JOIN [db_pki].[dbo].[Arrival] ON ([db_pki].[dbo].[Maker].[Code_maker] = [db_pki].[dbo].[Arrival].[Code_maker])
    LEFT JOIN [db_pki].[dbo].[Accounts] ON ([db_pki].[dbo].[Maker].[Code_maker] = [db_pki].[dbo].[Accounts].[Code_maker])
    )date_to_pivot
    PIVOT 
    (
    Max([Quan])
    For [Tasknum]
    IN (' + @task + ')
     )AS p'
    Execute (@query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have EF 4 implemented in the project. Within it, there are tables customer
There are two tables Table 1 select * from manage_tcp; +----+----------+--------+ | id |
There are two tables: CREATE TABLE STORE ( ID INTEGER NOT NULL, MEDICINE_ID INTEGER
There are two tables Table A id column_a 01 abc 01 abc 02 abc
there are two tables tbldoctor and tblschedule tbldoctor doc_id(PK) docMrId docfname 1 22 manish
There are two tables: A, with rows (row_id, column1, ...) and B, with rows
There are three tables in my database: apples, refrigerators, and houses. Each apple belongs
There are two tables. One is users info users, one is comments info comments.
in my database there are 3 tables CustomerType CusID EventType EventTypeID CustomerEventType CusID EventTypeID
Lets say that there are two tables: Order ID (PK) DateExecuted OrderStep ID (PK)

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.