SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Passing Columns to Function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

zia.ahsan
Starting Member

Indonesia
2 Posts

Posted - 05/28/2012 :  04:37:35  Show Profile  Reply with Quote
Hi All, I am using SQL Server 2005 and have the following function working properly. Now I need to pass some (but not all, bold and red) columns for select and group by at runtime. How can I do that and what is the best way to achieve it?

ALTER FUNCTION [dbo].[GET_MT_DAILY_HIST] (@STARTDATE DATETIME, @ENDDATE DATETIME)
RETURNS TABLE AS
RETURN
(
SELECT OP_CODE, SHORT_CODE, SID, CP_CODE, KEYWORD, 
CONVERT(CHAR(10), SEND_DATE_TO_OP, 101) AS REC_TERM, YEAR(SEND_DATE_TO_OP) AS REC_YEAR,
SUM(TOTAL) AS TOTAL
FROM CPGW_SUMMARY.DBO.MT_SUMMARY
WHERE SEND_DATE_TO_OP BETWEEN @STARTDATE AND @ENDDATE
GROUP BY OP_CODE, SHORT_CODE, SID, CP_CODE, KEYWORD, 
CONVERT(CHAR(10), SEND_DATE_TO_OP, 101), YEAR(SEND_DATE_TO_OP)
)


Thanks in advance.
Regards,
-Zia

Edited by - zia.ahsan on 05/28/2012 05:10:34

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/28/2012 :  13:59:13  Show Profile  Reply with Quote
you can do it using function. to determine grouping at runtime you need dynamic sql.
Whats the need of this requirement? Is it for a user interactive report? if yes, you can achieve this dynamic grouping in most of the front end reporting tools

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

zia.ahsan
Starting Member

Indonesia
2 Posts

Posted - 05/29/2012 :  02:30:09  Show Profile  Reply with Quote
Hi Visakh, thank you very much for your suggestion. Yes, this is for user interactive report where users can chose the group by elements in any order. I agree with you as I am using DynamicReports (based on JasperReports) I can group the elements dynamically.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/29/2012 :  12:10:14  Show Profile  Reply with Quote
quote:
Originally posted by zia.ahsan

Hi Visakh, thank you very much for your suggestion. Yes, this is for user interactive report where users can chose the group by elements in any order. I agree with you as I am using DynamicReports (based on JasperReports) I can group the elements dynamically.


bring data in detail to report and based on user input do required grouping and aggregation inside reporting application

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000