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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:07:58+00:00 2026-05-18T02:07:58+00:00

I’m having a little problem with [PortfelID] column. I need it’s ID to be

  • 0

I’m having a little problem with [PortfelID] column. I need it’s ID to be able to use it in function which will return me name about Type of Strategy per client. However by doing this i need to put [PortfelID] in GroupBy which complicates the results a lot.

I’m looking for a way to find Type of Strategy and Sum of Money this strategy has. However if i use Group By [PortfelID] I’m getting multiple entries per each strategy. Actually over 700 rows (because there are 700 [PortfelID] values). And all I want is just 1 strategy and Sum of [WycenaWartosc] for this strategy. So in total i would get 15 rows or so

Is there a way to use that function without having to add [PortfelID] in Group By?

DECLARE @data DateTime
SET @data = '20100930'

SELECT [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty] ([PortfelID], @data)
      ,SUM([WycenaWartosc]) AS 'Wycena'   
  FROM[dbo].[Wycena]
  LEFT JOIN [KlienciPortfeleKonta]
  ON [Wycena].[KlienciPortfeleKontaID] = [KlienciPortfeleKonta].[KlienciPortfeleKontaID] 
  WHERE [WycenaData] = @data 
  GROUP BY [PortfelID]

Where [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty] is defined like this:

ALTER FUNCTION [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty]
    (
      @portfelID INT,
      @data DATETIME 
    )
RETURNS NVARCHAR(MAX)
AS BEGIN
    RETURN ( SELECT TOP 1
                    [TypyStrategiiNazwa]
             FROM   [dbo].[KlienciPortfeleUmowy]
                    INNER JOIN [dbo].[TypyStrategii]
                    ON dbo.KlienciPortfeleUmowy.TypyStrategiiID = dbo.TypyStrategii.TypyStrategiiID
             WHERE  [PortfelID] = @portfelID
                    AND ( [KlienciUmowyDataPoczatkowa] <= @data
                          AND ([KlienciUmowyDataKoncowa] >= @data
                          OR KlienciUmowyDataKoncowa IS NULL)
                        )
             ORDER BY [KlienciUmowyID] ASC
           )

   end

EDIT:

As per suggestion (Roopesh Majeti) I’ve made something like this:

  SELECT SUM(CASE WHEN [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty] ([PortfelID], @data) = 'portfel energetyka' THEN [WycenaWartosc] ELSE 0 END) AS 'Strategy 1'
 ,SUM(CASE WHEN [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty] ([PortfelID], @data) = 'banków niepublicznych' THEN [WycenaWartosc] ELSE 0 END) AS 'Strategy 2'
  FROM [dbo].[Wycena]
  LEFT JOIN [KlienciPortfeleKonta]
  ON [Wycena].[KlienciPortfeleKontaID] = [KlienciPortfeleKonta].[KlienciPortfeleKontaID] 
  WHERE [WycenaData] = @data

But this seems like a bit overkill and a bit too much of hand job is required. AlexS solution seems to do exactly what I need 🙂

  • 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-18T02:07:59+00:00Added an answer on May 18, 2026 at 2:07 am

    Here’s an idea of how you can do this.

    DECLARE @data DateTime
    SET @data = '20100930'
    
    SELECT 
       TypID,
       SUM([WycenaWartosc]) AS 'Wycena'
    FROM 
    (
      SELECT [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty] ([PortfelID], @data) as TypID
      ,[WycenaWartosc]
      FROM[dbo].[Wycena]
      LEFT JOIN [KlienciPortfeleKonta]
      ON [Wycena].[KlienciPortfeleKontaID] = [KlienciPortfeleKonta].[KlienciPortfeleKontaID] 
      WHERE [WycenaData] = @data
    ) as Q
    GROUP BY [TypID]
    

    So basically there’s no need to group by PortfelID (as soon as you need to group by output of [dbo].[ufn_TypStrategiiDlaPortfelaDlaDaty]).

    This query is not optimal, though. Join can be pushed to the outer query in case PortfelID and WycenaData are not in [KlienciPortfeleKonta] table.

    UPDATE: fixed select list and aggregation function application

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.