<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: SQL create a group column? in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820838#M4851681</link>
    <description>&lt;P&gt;I can't understand what you intend is. Provide more details&lt;/P&gt;</description>
    <pubDate>Tue, 12 May 2020 06:23:08 GMT</pubDate>
    <dc:creator>thomas_duemesnil</dc:creator>
    <dc:date>2020-05-12T06:23:08Z</dc:date>
    <item>
      <title>SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaq-p/13820835</link>
      <description>&lt;P&gt;I have a field with Pos in a table. Now I want to create a group value that I will use later in a new column with sql like this:
I've tested functions for last_value and first_value, but I couldn't solve it.&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;Pos GroupValue
--- ----------
30      1
30      1
100     0
100     0
100     0
110     1
110     1
110     1
120     0
120     0
130     1
130     1
130     1
130     1
140     0
140     0
150     1
150     1
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 11 May 2020 05:36:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaq-p/13820835</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-11T05:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820838#M4851681</link>
      <description>&lt;P&gt;I can't understand what you intend is. Provide more details&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 06:23:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820838#M4851681</guid>
      <dc:creator>thomas_duemesnil</dc:creator>
      <dc:date>2020-05-12T06:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820836#M4851679</link>
      <description>&lt;P&gt;Your group value could be a case when MOD((select count(distinct(y.pos)) from yourtable y where y.pos&amp;lt;pos)/2) = 0 then 1 else 0 end&lt;/P&gt;
&lt;P&gt;the count on 30 is 0 so 1
the count on 100 is 1 so 0&lt;/P&gt;
&lt;P&gt;Basically an even/odd position gives you the group, so you could also do a cte with row number and distinct pos order by pos and modulus that on != 0. &lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 09:20:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820836#M4851679</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-12T09:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820837#M4851680</link>
      <description>&lt;P&gt;A nice use case for OLAP, methinks:&lt;/P&gt;
&lt;PRE&gt;create table TestPos( 
    -- add a PK column just to show how to conserve original order
   PK  int default autoincrement primary key,
   Pos int not null
);

-- Sample date
insert TestPos(Pos)
values
(30), (30),
(100), (100), (100),
(110), (110), (110),
(120), (120),
(130), (130), (130), (130),
(140), (140),
(150), (150);

-- Use the DENSE_RANK window function to "number" the groups with identical values
-- and apply MOD 2 to the result
select Pos, mod(PosRank, 2) as GroupValue
from
   (select PK, Pos, dense_rank() over (order by Pos) as PosRank
   from TestPos
   order by 1) DT
order by PK, Pos;

-- You can also directly apply MOD to the window function
select Pos, mod(dense_rank() over (order by Pos), 2) as GroupValue
from TestPos
order by Pos;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 May 2020 10:06:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820837#M4851680</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2020-05-12T10:06:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820839#M4851682</link>
      <description>&lt;P&gt;I've never used dense_rank - I like it!&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 10:22:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820839#M4851682</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-12T10:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820840#M4851683</link>
      <description>&lt;P&gt;Excellent solution. Exactly what I needed. A small problem. If Pos doesn't come in sorted order, can it be solved the same way then? If it looks like this:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;Pos GroupValue
--- ----------
30      1
30      1
150     0
150     0
100     1
100     1
100     1
130     0
130     0
130     0
110     1
110     1
110     1
120     0
120     0
130     1
140     0
140     0
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 12 May 2020 11:35:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820840#M4851683</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-12T11:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820841#M4851684</link>
      <description>&lt;P&gt;Hm, I don't fully understand your question, and do not know how to specify a sort order that sorts 150 between 30 and 100 - but well, you have to apply your required sort order both in the DENSE_RANKE OVER (ORDER BY ...) and the final ORDER BY.&lt;/P&gt;</description>
      <pubDate>Tue, 12 May 2020 12:11:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820841#M4851684</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2020-05-12T12:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: SQL create a group column?</title>
      <link>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820842#M4851685</link>
      <description>&lt;P&gt;I solved it. Thanks for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 13 May 2020 13:14:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sql-create-a-group-column/qaa-p/13820842#M4851685</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-13T13:14:19Z</dc:date>
    </item>
  </channel>
</rss>

