How to select a max row for each group in SQL ( with distinct code )

Dear friends, 
My table is as show in below…  for each ‘grpid’ I want get row with Max tiv value and code should be distinct. Ie in result, code & grpid will come only once.
grpid
code
Tiv
2
GB
9
2
IN
7
1
GB
11
1
US
10
Result:    ( we are selecting   IN even though for grpid 2
 ‘GB’ has max value )
grpid
code
Tiv
2
IN
7
1
GB
11
-Sajid

Thanks for reply..
please see my  DML+  DL
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
Result:
grpid      code     
tiv
1             
US          10
2             
GB          12
3             
UA          10
Thanks in Advance..
-Sajid
CREATE TABLE [dbo].[tab](
[grpid] [int] NOT NULL,
[code] [varchar](2) NOT NULL,
[Tiv] [int] NOT NULL
) ON [PRIMARY]
delete from tab
insert into tab values (2 , 'GB' ,12)
insert into tab values (2 , 'IN' ,10)
insert into tab values (1 , 'GB' ,11)
insert into tab values (1 , 'US' ,10)
insert into tab values (3 , 'GB' ,10)
insert into tab values (3 , 'US' ,9)
insert into tab values (3 , 'UA' ,10)
insert into tab values (3 , 'IN' ,8)
;with mycte as
(select grpid,code,tiv , row_number() over(Partition by code Order by Tiv DESC) rn1, row_number() over(Partition by grpid Order by Tiv DESC) rn2
from tab)
,mycte1 as (Select *
, row_number() over(Partition by grpid, rn1 Order by rn2 ASC) rn3
from mycte
WHERE rn1=1
Select grpid,code,tiv from mycte1
Where rn3=1
drop table tab

Similar Messages

  • How to select a max row for each group in SQL

    Dear Frindz,
    I want select countries with maximum value of 'Value' for a 'grpid'.  Also already selected 'Country' should not be considered for other 'grpid' while checking the maximum. ( ie Country or grpid should not be repeated in the result )
    CREATE TABLE [dbo].[test1](
    [Country] [varchar](4) NULL,
    [grpid] [int] NOT NULL,
    [Value] [float] NULL,
    [Row_number] [bigint] NULL
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 49707, 604456458, 1)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 909, 604456458, 2)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'US', 231, 604456457, 3)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 5086, 497654945, 4)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 50147, 405759433, 5)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 909, 405759433, 6)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GB', 231, 405759433, 7)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 49707, 353500201, 8)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 5086, 353500201, 9)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 909, 353500201, 10)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CA', 231, 353500201, 11)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 49707, 198291290, 12)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 5086, 198291290, 13)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 909, 198291290, 14)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'JP', 231, 198291290, 15)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 49707, 181884714, 16)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 909, 181884714, 17)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'MX', 231, 181884714, 18)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 49707, 105147054, 19)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 5086, 105147054, 20)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'BR', 909, 105147054, 21)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 49707, 94774929, 22)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 5086, 94774929, 23)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 909, 94774929, 24)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'AR', 231, 94774929, 25)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 49707, 84560514, 26)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 909, 84560514, 27)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'ZA', 231, 84560514, 28)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 49707, 68966682, 29)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 909, 68966682, 30)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CN', 231, 68966682, 31)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 49707, 65020665, 32)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 5086, 65020665, 33)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 909, 65020665, 34)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CB', 231, 65020665, 35)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 49707, 42016510, 36)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 5086, 42016510, 37)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'GT', 909, 42016510, 38)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 49707, 37116544, 39)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 5086, 37116544, 40)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 909, 37116544, 41)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'CR', 231, 37116544, 42)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 49707, 31103322, 43)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 5086, 31103322, 44)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'SV', 909, 31103322, 45)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 49707, 17415843, 46)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 5086, 17415843, 47)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'NI', 909, 17415843, 48)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 49707, 12516273, 49)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 5086, 12516273, 50)
    GO
    INSERT [dbo].[test1] ([Country], [grpid], [Value], [Row_number]) VALUES (N'HN', 909, 12516273, 51)
    GO
    Result:
    Country
    grpid
    Value
    Row_number
    US
    49707
    604456458
    1
    GB
    5086
    497654945
    4
    CA
    909
    353500201
    10
    JP
    231
    198291290
    15
    -Sajid

    Hi Pituach, thanks for your reply.
    in result  Country or grpid should not be repeated... in your result 'GB' and 'US' is  repeating.
      1) select countries with maximum value of 'Value' for a 'grpid'
      2) 'grpid' & country should not be repeated.
      3)  we need to take next maximum 
    value if 'grpid'  not  there a for 
    highest value.
    Result:
    Country
    grpid
    Value
    Row_number
    US
    49707
    604456458
    1
    GB
    5086
    497654945
    4
    CA
    909
    353500201
    10
    JP
    231
    198291290
    15
    -Sajid
    -Sajid

  • How to select the first row for each pair of values ?

    Hi guys, am hoping someone can help me here.
    If I have 4 tables - Building, Dept, Emp, Text
    B D E T
    A 1 Z blah1
    A 1 X blah2
    A 1 W blah3
    A 2 V blah4
    A 2 G blah5
    A 2 H blah6
    B 1 K blah7
    B 1 L blah8
    B 2 E blah9
    B 2 F blah0
    I need a query that will bring back the first bit of text for each pair of building and dept records.
    So the results I would get would be
    A 1 blah1
    A 2 blah4
    B 1 blah7
    B 2 blah9
    blah2 wouldnt be returned, because we have already had a record from the Building A, dept 1 pairing.
    Likewise we wouldnt get blah5 because we have already had a record from the Building A, dept 2 pairing.
    Can this be done in 1 query??
    Thanks all.
    Message was edited by:
    Scott Hillier

    here you go
    SQL> drop table t;
    Table dropped.
    SQL> create table t(B char(1), D integer, E char(1), T varchar2(5));
    Table created.
    SQL> insert into t values('A', 1, 'Z', 'blah1');
    1 row created.
    SQL> insert into t values('A', 1, 'X', 'blah2');
    1 row created.
    SQL> insert into t values('A', 1, 'W', 'blah3');
    1 row created.
    SQL> insert into t values('A', 2, 'V', 'blah4');
    1 row created.
    SQL> insert into t values('A', 2, 'G', 'blah5');
    1 row created.
    SQL> insert into t values('A', 2, 'H', 'blah6');
    1 row created.
    SQL> insert into t values('B', 1, 'K', 'blah7');
    1 row created.
    SQL> insert into t values('B', 1, 'L', 'blah8');
    1 row created.
    SQL> insert into t values('B', 2, 'E', 'blah9');
    1 row created.
    SQL> insert into t values('B', 2, 'F', 'blah0');
    1 row created.
    SQL> select b,d,t
      2    from (
      3  select row_number() over(partition by b, d order by b,d) rno,b,d,t
      4    from t)
      5   where rno = 1;
    B          D T
    A          1 blah1
    A          2 blah4
    B          1 blah7
    B          2 blah9karthick
    http://karthickarp.blogspot.com/

  • Getting the first row for each group

    Hi Everyone,
    I have a query which returns a number of rows, all of which are valid. What I need to do is to get the first row for each group and work with those records.
    For example ...
    client flight startairport destairport stops
    A fl123 LGW BKK 2
    A fl124 LHR BKK 5
    B fl432 LGW XYZ 7
    B fl432 MAN ABC 8
    .... etc.
    I would need to return one row for Client A and one row for Client B (etc.) but find that I can't use the MIN function because it would return the MIN value for each column (i.e. mix up the rows). I also can use the rownum=1 because this would only return one row rather than one row per group (i.e. per client).
    I have been investigating and most postings seem to say that it needs a second query to look up the first row for each grouping. This is a solution which would not really be practical because my query is already quite complex and incorporating duplicate subqueries would just make the whole thing much to cumbersome.
    So what I really new is a "MIN by group" or a "TOP by group" or a "ROWNUM=1 by group" function.
    Can anyone help me with this? I'm sure that there must be a command to handle this.
    Regards and any thanks,
    Alan Searle
    Cologne, Germany

    Something like this:
    select *
    from (
       select table1.*
       row_number() over (partition by col1, col2 order by col3, col4) rn
       from table1
    where rn = 1In the "partition by" clause you place what you normally would "group by".
    In the "order by" clause you define which will have row_number = 1.
    Edit:
    PS. The [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#i81407]docs have more examples on using analytical functions ;-)
    Edited by: Kim Berg Hansen on Sep 16, 2011 10:46 AM

  • How can I have one row for each object?

    Post Author: farnaz
    CA Forum: WebIntelligence Reporting
    I have 3 objcts that one of them is ID and second one is internal code and the third one is quantity.There is more than one value for each ID (becouse of internal code).I want to create a report based on ID and quantity (not based on internal code).I make a break on ID but the result shows me more than one row for each ID.so how can i have just one row for each ID?
    another question:
    how can I post screen shot of the results to my messag in forum?

    Post Author: jsanzone
    CA Forum: WebIntelligence Reporting
    farnaz:
    Have you tried to omit "internal code" from your grid?  In normal circumstances, if you have "ID" and "Quantity" on the grid, then WebI will on its own summarize Quantity based on ID, however, this is provided that Quantity has been set up as a measure (or metric).  The quickest way to tell if Quantity is a metric or not is to observe the color of its icon when you look at it in the data panel.  If it is a purple (or sometimes appears as pink or fuscia) dot, then it's a measure, but if it is a blue box then it is a dimension.  If you need Quantity as a measure then this will have to be modified at the universe (make it look like "sum(Quantity)" in the select phase of the object, and insure the object is defined as a number....

  • Selecting top x rows for each of the groupby clause

    Hi Gurus (again).
    I have this query to select the top 24 rows:
    select * from
    +(+
    select proid, to_char((next_day(trunc(request_date, 'iw'), 'mon')-7), 'YYYY') as YEAR, to_char(request_date, 'IW') as WEEK,
    next_day(trunc(request_date, 'iw'), 'mon')-7 as weekdate,
    trunc(avg(case when report_date > request_date then ((report_date - request_date) * 24) end),1) as avg_ERT_hr,  '24' as ERT_goal,
    trunc(avg(case when response_date > request_date then ((response_date - request_date) * 24) end),1) as avg_IRT_hr,  '48' as IRT_goal,
    trunc(avg(case when deliver_date > request_date then ((deliver_date - request_date)) end),1) as avg_IDT_day,  '0' as IDT_goal,
    trunc(avg(case when complete_date > request_date then ((complete_date - request_date)) end),1) as avg_ORT_day,  '0' as ORT_goal
    from biw_hda_event
    where request_date is not null
    group by proid, to_char((next_day(trunc(request_date, 'iw'), 'mon')-7), 'YYYY'), to_char(request_date, 'IW'), next_day(trunc(request_date, 'IW'), 'MON')-7
    order by 1 desc, 2 desc
    +) where rownum <=24+
    The problem here is that I have a group by "proid" clause in addition to the dates. My purpose is to be able to select the top 24 rows for each of the "proid" that is already grouped by the date. Essentially, the top 24 is the last 24 weeks pertaining to that "proid". How can I do this effectively without creating heaps of UNION clause for each of the "proid"?
    thanks in advance

    I meant to delete this thread, but I do not know how to. Sorry.

  • How To Restrict Number Of Rows For Multiple Group In Report Output

    Hi ,
    I have a requirement to restrict number of rows in report output.I have three different group , if i use same no of rows to restrict then output is as expected but if i want Deduction group should have 7 rows , earning should have 5 rows and Tax group have 3 rows in report output then XML tag is not working.
    Below is the XML tag i am using -
    First i have declare the variable to restrict the rows -
    <xsl:variable name="lpp" select="number(7)"/>
    <xsl:variable name="lpp1" select="number(5)"/>
    <xsl:variable name="lpp2" select="number(3)"/>
    For Each -
    <?for-each:PAYSLIP?>
    <xsl:variable xdofo:ctx="incontext" name="DedLines" select=".//AC_DEDUCTIONS"/>
    <xsl:variable xdofo:ctx="incontext" name="EarLines" select=".//AC_EARNINGS[ELEMENT_CLASSIFICATION!='Taxable Benefits']"/>
    <xsl:variable xdofo:ctx="incontext" name="EarTaxLines" select=".//AC_EARNINGS[ELEMENT_CLASSIFICATION='Taxable Benefits']>
    <?for-each:$DedLines?><?if:(position()-1) mod $lpp=0?> <xsl:variable name="start" xdofo:ctx="incontext" select="position()"/>
    <?if:(position()-1) mod $lpp1=0?><xsl:variable name="start1" xdofo:ctx="incontext" select="position()"/
    <?if:(position()-1) mod $lpp2=0?><xsl:variable name="start2" xdofo:ctx="incontext" select="position()"/>
    Report output is tabular form (one page has two column - Earning and Deduction ) . Tax group comes below earning group.
    Deduction Group -
    <?for-each-group:$DedLines;./REPORTING_NAME?><?if:position()>=$start and position()<$start+$lpp?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Earning Group -
    <?for-each-group:$EarLines;./REPORTING_NAME?><?if:position()>=$start1 and position()<$start1+$lpp1?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Tax Group -
    <?for-each-group:$EarTaxLines;./REPORTING_NAME?><?if:position()>=$start2 and position()<$start2+$lpp2?>
    <?REPORTING_NAME?>
    <?end if?><?end for-each-group?>
    Please let me know in case additional detail is require.
    Thanks in Advance.
    Thanks,
    Harsh
    Edited by: Harsh.rkg on Jan 14, 2013 9:43 PM

    variable lpp2 is declare to restrict EarTaxLines -
    <xsl:variable name="lpp2" select="number(2)"/>
    This will help to restrict the no of rows on one page , if we have more then two tax benefits line then layout will roll over to continuation page.
    As part of report output my expectation is if i restrict Earning , Deduction and Tax benefits to same no of line for example - variable lpp ,lpp1 and lpp2 have same value "number(2)" , we can see the layout is continue on next page (restrict every group can have max two lines) .This is the reason we have 4 header grid , deduction and Tax Benefit lines are rolled over to continuation page .But if we restrict different value for each variable then continuation page layout is missing .
    When we tried for <xsl:variable name="lpp2" select="number(3)"/> value continuation page layout is not getting generate for both employee number .

  • How to handle goup sub total for each group in oracle query ?

    hi,
    i want to handle one complex thing in oracle query.
    i have a grouping in my oracle query.
    i want to do sub total for each group in query and after that i want to subtract one group sub total from previous group subtotal ?
    so how can i handle this in oracle query ?
    any help is greatly appreciated.
    thanks.

    Hello
    Interesting requirement.
    I wonder why are you not using these calculation during the display time. If it is acceptable then you can try using the custom formula with the running total combination in crystral report to achieve to get the required results.
    Best regards
    Ali Hadi

  • Select top 10 rows for each product

    Hi,
    I have to build a query in the following way:
    I have a query like,
    select dept, product, product_sold
    from products
    order by 1, 2 desc
    But I have a requirement, not to show all the products in dept . I need to show only first 10 rows of each 'dept' column.
    Please help me in acheiving this.

    You would do something like this (untested):
    select dept
    , product
    , product_sold
    from (
       select dept
       , product
       , product_sold
       , row_number() over (
          partition by dept
          order by product
          , product_sold
       ) rn
       from products
    where rn <= 10;Assuming that I have correctly guessed the ordering you require...
    cheers,
    Anthony

  • Getting the max count for each group

    Hi, I am a newbie and working on a project for school. I have been working this problem for a couple of days and made a bit of progress.
    I have two tables, student and enroll for a college database. I need to show the students who have the most enrollments in each major. So far, I have:
    WITH COUNT_Q AS
    SELECT B.SSN, B.MAJOR, COUNT (A.CLASS_NO) AS COUNTCLASS
    FROM ENROLL A, STUDENT B
    WHERE A.SSN = B.SSN
    GROUP BY B.SSN, B.MAJOR
    SELECT MAX(COUNTCLASS), MAJOR
    FROM COUNT_Q
    GROUP BY MAJOR
    I am getting the correct results but need to show the SSN and Major of the studens. When I add in the SSN and Major, like below, I get back too many records.
    WITH COUNT_Q AS
    SELECT B.NAME, B.SSN, B.MAJOR, COUNT(A.SSN) AS COUNTCLASS
    FROM ENROLL A, STUDENT B
    WHERE A.SSN = B.SSN
    GROUP BY B.NAME, B.SSN, B.MAJOR
    SELECT *
    FROM COUNT_Q
    WHERE (COUNTCLASS) IN (SELECT MAX(COUNTCLASS)
    FROM COUNT_Q
    GROUP BY MAJOR
    Can someone help point me in the right direction?

    SELECT  B.NAME,
            B.SSN,
            B.MAJOR
      FROM  (
             SELECT  B.NAME,
                     B.SSN,
                     B.MAJOR,
                     COUNT(A.CLASS_NO) SSN_MAJOR_COUNTCLASS
                     DENSE_RANK() OVER(PARTITION BY MAJOR ORDER BY COUNT(A.CLASS_NO) DESC) RNK
               FROM  ENROLL A,
                     STUDENT B
               WHERE A.SSN = B.SSN
               GROUP BY B.NAME,
                        B.SSN,
                        B.MAJOR
      WHERE RNK = 1
    /SY.
    Edited by: Solomon Yakobson on Mar 27, 2011 6:53 PM

  • How to create a dynamic menu for each group of responsibility and  user

    Hi
    I am using Jdev 11.1.1.6 and new to ADF, my application require to populate menu which would be different for different groups of user, and the user in each group would have different submenu depends on their responsibilities assigned to each one of them.
    so on the top level would be menu_top_item1, menue_top_item2
    |-->submenu1_1 |-->submenu2_1
    |-->submenu1_2 |-->submenu2_2
    also the menu would need to be on_mouse_over to show the submenu items or collapsed back.
    any idea what would be the best approch? or is there any example around?
    thanks a lot

    You can use ADF Security to control access to menu options.
    More about ADF Security here:
    http://download.oracle.com/otn_hosted_doc/jdeveloper/11gdemos/AdfSecurity/AdfSecurity.html
    If you also use WebCenter you might use their menuing option:
    https://blogs.oracle.com/shay/entry/webcenter_portal_intro_for_adf

  • How to display different subtotal text for each group in alv report

    Hi,
    i need to display subtotal text as "totals of  Mat1"  for material numbers 1 to 10 as group1 and again i need to display subtotal text as " totals of Mat2" for material number 11 to 20.
    group 2.
    Please help me asap.

    You can create subtotals by columns.
    You would need extra column to group the materials( 1 to 10 GROUP1 20 to 30 GROUP2 by example)  and use this column for subtotals

  • FillBy always fills in the same row in data grid view. How to make it fill in a new row for each click of the Fillby Button? VB 2010 EXPRESS?

    Hi there, 
    I am a beginner in Visual Basic Express 2010. I have a Point of Sale program that uses DataGridView to display records from an external microsoft access
    database using the fillby query. 
    It works, but it repopulates the same row each time, but i want to be able to display multiple records at the same time, a new row should be filled for
    each click of the fillby button. 
    also I want to be able to delete any records if the customer suddenly decides to not buy an item after it has already been entered. 
    so actually 2 questions here: 
    1. how to populate a new row for each click of the fillby button 
    2. how to delete records from data grid view after an item has been entered 
    Thanks 
    Vishwas

    Hello,
    The FillBy method loads data according to what the results are from the SELECT statement, so if there is one row then you get one row in the DataGridView, have two rows then two rows show up.
    Some examples
    Form load populates our dataset with all data as it was defined with a plain SELECT statement. Button1 loads via a query I created after the fact to filter on a column, the next button adds a new row to the existing data. When adding a new row it is appended
    to the current data displayed and the primary key is a negative value but the new key is shown after pressing the save button on the BindingNavigator or there are other ways to get the new key by manually adding the row to the backend table bypassing the Adapter.
    The following article with code shows this but does not address adapters.
    Conceptually speaking the code in the second code block shows how to get the new key
    Public Class Form1
    Private Sub StudentsBindingNavigatorSaveItem_Click(
    sender As Object, e As EventArgs) Handles StudentsBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.StudentsBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.MyDataSet)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'MyDataSet.Students' table. You can move, or remove it, as needed.
    Me.StudentsTableAdapter.Fill(Me.MyDataSet.Students)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.StudentsTableAdapter.FillBy(Me.MyDataSet.Students, ComboBox1.Text)
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Me.MyDataSet.Students.AddStudentsRow("Jane", "Adams", "Female")
    End Sub
    End Class
    Get new key taken from
    this article.
    Public Function AddNewRow(ByVal sender As Customer, ByRef Identfier As Integer) As Boolean
    Dim Success As Boolean = True
    Try
    Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
    Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
    cmd.CommandText = InsertStatement
    cmd.Parameters.AddWithValue("@CompanyName", sender.CompanyName)
    cmd.Parameters.AddWithValue("@ContactName", sender.ContactName)
    cmd.Parameters.AddWithValue("@ContactTitle", sender.ContactTitle)
    cn.Open()
    cmd.ExecuteNonQuery()
    cmd.CommandText = "Select @@Identity"
    Identfier = CInt(cmd.ExecuteScalar)
    End Using
    End Using
    Catch ex As Exception
    Success = False
    End Try
    Return Success
    End Function
    In closing I have not given you a solution but hopefully given you some stuff/logic to assist with this issue, if not perhaps I missed what you want conceptually speaking.
    Additional resources
    http://msdn.microsoft.com/en-us/library/fxsa23t6.aspx
    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

  • How to retrieve Min(startDate) and Max(endDate) for different groups of data? (sql server 2000)

    My sample dataset (below) contains 3 groups -- 'a', 'b', 'c'.  I need to retrieve the Min(startDate) and Max(EndDate) for each group so that the output looks something like this (date format not an issue):
    fk   minStart       maxEnd
    a    1/13/1985    12/31/2003
    b    2/14/1986    12/31/2003
    c    4/26/1987    12/31/2002
    What is the Tsql to perform this type of operation?  Note:  the actual data resides in a sql server 2000 DB.  If the Tsql is different between version 2000 and the later versions -- I would be grateful for both versions of the Tsql
    --I noticed that multiple lines of Insert values doesn't work in Sql Server 2000 -- this sample is in Sql Server 2008
    create table #tmp2(rowID int Identity(1,1), fk varchar(1), startDate datetime, endDate datetime)
    insert into #tmp2
    values
    ('a', '1/13/1985', '12/31/1999'),
    ('a', '3/17/1992', '12/31/1997'),
    ('a', '4/21/1987', '12/31/2003'),
    ('b', '2/14/1986', '12/31/2003'),
    ('b', '5/30/1993', '12/31/2001'),
    ('b', '6/15/1994', '12/31/2003'),
    ('b', '7/7/2001', '12/31/2003'),
    ('c', '4/26/1987', '12/31/1991'),
    ('c', '8/14/1992', '12/31/1998'),
    ('c', '9/10/1995', '12/31/2002'),
    ('c', '10/9/1996', '12/31/2000')
    Thanks
    Rich P

    Rich
    It is unclear what you are trying to achieve, you said that it is SQL Server 2000 but provide a sample data with SQL Server 2008 syntax
    Is it possible to use UNION ALL for your queries to make its one 
    select * from
    select * from #tmp2 t1 where exists
    (select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.startdate) x where x.rowID = t1.rowID)
    UNION ALL
    select * from #tmp2 t1 where exists
    (select * from (select top 1 * from #tmp2 t2 where t2.fk = t1.fk order by t2.Enddate desc) x where x.rowID = t1.rowID)
     as  der order by fk
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to make RECORD for saving multiple rows for each employee..

    Hi,
    How can I create a record for saving multiple rows for each employee... (parent child relationship)
    I have created all required fields, then created a record, and then created a page. in that page i dragged-n-dropped a scroll area on that page and dropped all required fields from record into that scroll-area.
    whats is happening that its saving ONE (the first) record fine, but for the second record its not storing EMPLID for that row..
    Will I have to make some change at RECORD level ?
    OR Will i have to make some configuration in parent component ?

    Hi, the problem is solved. The record will be created in same way. just create more then one keys (allowing to make composite key in DB table)

Maybe you are looking for