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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:39:51+00:00 2026-06-14T10:39:51+00:00

I’m trying to construct a select statement that will give me a percentage value

  • 0

I’m trying to construct a select statement that will give me a percentage value for each month and user.

I have a table with rows that looks like this;
Table name: pytrans

tTransDate  tArtCode  tTime  tSignature
20120801    DKK       1      30JK
20120801    AD        1.5    30JK
20120802    DKB       3      40AK
20120903    MI        2      45TR
20120904    DKK       2      30JK
20120904    DKB       2      30JK

The select(s) should give me the percentage of how much time spent on tArtCode DKB or DKK for each signature and month of all time spent on all tArtCode. So from the above example the select should give me 40% for signature 30JK in month 8 and 100% for signature 30JK in month 9.

I have tried different kind of sql select statements but It doesn’t seem to get 100% right.
This is what I’ve got;

select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignatur,
   (select SUM(tAntal) AS SumPart from pytrans where tSignatur = '30JK' AND (tArtikelkod = 'DKK' OR tArtikelkod = 'DKB'))
    /(select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, SUM(tAntal) AS SumTot from pytrans where tSignatur = '30JK' GROUP BY Year, Month, SumTot) * 100
From pytrans where tSignatur = '30JK' GROUP BY DATEPART(mm, tTransdatum), DATEPART(YY, tTransdatum), tSignatur

This SQL don’t work the error is that I can’t group Year, Month, SumTot. Also i get error; Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

This next sql select give me what i want but I’m to specific what I’m asking for… so to speak. If I use this sql i then need to have a sql query for every signature and every month, and that’s not really creative.

select TOP 1 DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignatur, 
SumProc = (select SUM(tAntal) from pytrans
          where tSignatur = '30JK' AND (tArtikelkod NOT LIKE 'FL' AND tArtikelkod NOT LIKE 'SEM' AND tArtikelkod NOT LIKE 'KOMPIN') AND (tArtikelkod = 'DKK' OR tArtikelkod = 'DKB') AND tTransdatum BETWEEN '20121101' AND '20121131')
          / (select SUM(tAntal) from pytrans 
          where tSignatur = '30JK' AND (tArtikelkod NOT LIKE 'FL' AND tArtikelkod NOT LIKE 'SEM' AND tArtikelkod NOT LIKE 'KOMPIN') AND tTransdatum BETWEEN '20121101' AND '20121131') * 100
from pytrans
Where tSignatur = '30JK' AND tTransdatum BETWEEN '20121101' AND '20121131'
group by tTransdatum, tSignatur

I’ve also tried Union but that doesn’t seem to work for me.

I really need help with this! Thanks!

  • 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-14T10:39:53+00:00Added an answer on June 14, 2026 at 10:39 am
    Create Table #pytrans(tTransdatum datetime,  tArtCode varchar(10), tAntal float,  tSignature varchar(10))
    
    insert into #pytrans Values('20120801',    'DKK',       1      ,'30JK')
    insert into #pytrans Values('20120801',    'AD',        1.5    ,'30JK')
    insert into #pytrans Values('20120802',    'DKB',       3      ,'40AK')
    insert into #pytrans Values('20120903',    'MI',        2      ,'45TR')
    insert into #pytrans Values('20120904',    'DKK',       2      ,'30JK')
    insert into #pytrans Values('20120904',    'DKB',       2      ,'30JK')
    
    Select a.Year,a.Month,a.tSignature,a.SumPart as aAll,b.SumPart as bAll,Case when b.SumPart =0 then NULL else a.SumPart/b.SumPart * 100 end as [Percent]
     from
    (select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignature
    ,SUM(tAntal) AS SumPart
    from #pytrans
    where tSignature = '30JK' AND tArtCode in( 'DKK' ,'DKB')
    Group by DATEPART(YY, tTransdatum) , DATEPART(mm, tTransdatum) , tSignature) a
    Left Join
    (select DATEPART(YY, tTransdatum) AS Year, DATEPART(mm, tTransdatum) AS Month, tSignature
    ,SUM(tAntal) AS SumPart
    from #pytrans
    where tSignature = '30JK' 
    Group by DATEPART(YY, tTransdatum) , DATEPART(mm, tTransdatum) , tSignature) b
    on a.Month=b.Month and a.Year =b.Year and a.tSignature = b.tSignature
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put

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.