Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Group by columns and add sum in new columns Post 302999293 by ricky1991 on Friday 16th of June 2017 02:14:46 PM
Old 06-16-2017
Group by columns and add sum in new columns

Dear Experts,
I have input file which is comma separated, has 4 columns like below,

Code:
BRAND,COUNTRY,MODEL,COUNT
NIKE,USA,DUMMY,5
NIKE,USA,ORIGINAL,10
PUMA,FRANCE,DUMMY,20
PUMA,FRANCE,ORIGINAL,15
ADIDAS,ITALY,DUMMY,50
ADIDAS,ITALY,ORIGINAL,50
SPIKE,CHINA,DUMMY,1O

And expected output add one column TOTALCOUNT which is group by BRAND,COUNTRY column(1st and 2nd) and add a new column(5th) which is SUM of COUNT column(4th)..

Code:
BRAND,COUNTRY,MODEL,COUNT, TOTALCOUNT
NIKE,USA,DUMMY,5,15
NIKE,USA,ORIGINAL,10,15
PUMA,FRANCE,DUMMY,20,35
PUMA,FRANCE,ORIGINAL,15,35
ADIDAS,ITALY,DUMMY,50,100
ADIDAS,ITALY,ORIGINAL,50,100
SPIKE,CHINA,DUMMY,1O,100


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-16-2017 at 05:41 PM.. Reason: Added CODE tags.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of three columns - in 4N columns file

Hi All, happy new year. I have a file with 4xN columns like 0.0000e+00 0.0000e+00 7.199E+07 7.123E+07 6.976E+07 6.482E+07 5.256E+07 2.523E+07 0.0000e+00 0.0000e+00 8.641E+07 8.550E+07 8.373E+07 7.780E+07 6.309E+07 3.028E+07... (8 Replies)
Discussion started by: f_o_555
8 Replies

2. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

3. Shell Programming and Scripting

how can i group by same columns by another columns in Bash

how can i group by same columns by another columns in Bash Eq. this is a csv file Co1 Co2 Co3 Co4 A A 1,000 1,000 A B 2,000 1,250 A A 2,000 3,002 A C 2,000 3,005 how can i get the result of like this Co1 Co2 Co3 Co4 A A 3,000 ... (5 Replies)
Discussion started by: qjlongs
5 Replies

4. Shell Programming and Scripting

Sum columns

Hi All, I'm new to this forum. So please be patience with me! :) I have a file that looks like this (all rows have the same number of columns): 19 20 30 15 17 38 51 60 74 85 96 07 .... 10 20 44 59 39 88 13 77 30 10 11 12 .... . . . I want to sum the value of first field to all the... (2 Replies)
Discussion started by: Aderson Nascime
2 Replies

5. Shell Programming and Scripting

Add the values in second and third columns with group by on first column.

Hi All, I have a pipe seperated file. I need to add the values in second and third columns with group by on first column. MYFILE_28012012_1115|47|173.90 MYFILE_28012012_1115|4|0.00 MYFILE_28012012_1115|6|22.20 MYFILE_28012012_1116|47|173.90 MYFILE_28012012_1116|4|0.00... (3 Replies)
Discussion started by: angshuman
3 Replies

6. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

7. Shell Programming and Scripting

Get the SUM of TWO columns SEPARATELY by doing GROUP BY on other columns

My File looks like: "|" -> Field separator A|B|C|100|1000 D|E|F|1|2 G|H|I|0|7 D|E|F|1|2 A|B|C|10|10000 G|H|I|0|7 A|B|C|1|100 D|E|F|1|2 I need to do a SUM on Col. 5 and Col.6 by grouping on Col 1,2 & 3 My expected output is: A|B|C|111|11100 (2 Replies)
Discussion started by: machomaddy
2 Replies

8. Shell Programming and Scripting

Add sum of columns and max as new row

Hi, I am a new bie i need some help with respect to shell onliner; I have data in following format Name FromDate UntilDate Active Changed Touched Test 28-03-2013 28-03-2013 1 0.6667 100 Test2 28-03-2013 03-04-2013 ... (1 Reply)
Discussion started by: gangaraju6
1 Replies

9. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

10. Shell Programming and Scripting

Add new columns based on existing columns

Hi all, I am kind of stuck with printing my desired output. Please help me if you know how it can work. My input file(tab separated): NW_0068.1 41,16 100,900 NW_0699.1 4,2,19 200,700,80 My Output file (desired): NW_0068.1 41,16 100,900 100 - 141 NW_0068.1 41,16 100,900 ... (3 Replies)
Discussion started by: sam_2921
3 Replies
IFX_PREPARE(3)								 1							    IFX_PREPARE(3)

ifx_prepare - Prepare an SQL-statement for execution

SYNOPSIS
resource ifx_prepare (string $query, resource $link_identifier, [int $cursor_def], mixed $blobidarray) DESCRIPTION
Prepares a $query for later use with ifx_do(3). For "select-type" queries a cursor is declared and opened. Non-select queries are "execute immediate". For either query type the number of (estimated or real) affected rows is saved for retrieval by ifx_affected_rows(3). If the contents of the TEXT (or BYTE) column allow it, you can also use ifx_textasvarchar(1) and ifx_byteasvarchar(1). This allows you to treat TEXT (or BYTE) columns just as if they were ordinary (but long) VARCHAR columns for select queries, and you don't need to bother with blob id's. With ifx_textasvarchar(0) or ifx_byteasvarchar(0) (the default situation), select queries will return BLOB columns as blob id's (integer value). You can get the value of the blob as a string or file with the blob functions (see below). PARAMETERS
o $query - The query string. o $link_identifier - The link identifier. o $cursor_def - This optional parameter allows you to make this a scroll and/or hold cursor. It's a bitmask and can be either IFX_SCROLL, IFX_HOLD, or both or'ed together. o $blobidarray - If you have BLOB (BYTE or TEXT) columns in the query, you can add a $blobidarray parameter containing the corresponding "blob ids", and you should replace those columns with a "?" in the query text. RETURN VALUES
Returns a valid result identifier for use by ifx_do(3), or FALSE on errors. SEE ALSO
ifx_do(3). PHP Documentation Group IFX_PREPARE(3)
All times are GMT -4. The time now is 01:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy