INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Grouping query - help requested

Grouping query - help requested

(OP)
hello to all

Here is a sample from a table... (* sorry, but I can't get the TGML to show the tables properly??? I'm using tt and /tt in square brackets)


Row DataVal
1 7
2 2
3 8
4 -2
5 -3
6 -4
7 -8
8 4
9 5
10 -6
11 -7
12 3
13 6
14 -5


I need help writing a query to a) Group DataVal by SIGN (there are no values = 0), then b) COUNT the number of records in each group.
The output should show the 1st row numberof each group, and the COUNT of each group. Here's what the output would look like...


Grp1stRow GrpCount
1 3
4 4
8 2
10 2
12 2
14 1


Thanks for any clues
Vicky


RE: Grouping query - help requested

Hi,

Okay, here's your example copied from an Excel sheet and pasted here, with Pre TGML rather than TT...

Row	DataVal
1	7
2	2
3	8
4	-2
5	-3
6	-4
7	-8
8	4
9	5
10	-6
11	-7
12	3
13	6
14	-5
 

Worked on a pure SQL solution and failed. So this is a spreadsheet/SQL solution...

1) augmented the source table in Excel with formulas...

Row	DataVal	sgn	toggle
1	7	1	1
2	2	1	1
3	8	1	1
4	-2	0	2
5	-3	0	2
6	-4	0	2
7	-8	0	2
8	4	1	3
9	5	1	3
10	-6	0	4
11	-7	0	4
12	3	1	5
13	6	1	5
14	-5	0	6
 
the sgn column formula

C2: =IF(B2>0,1,0)

the toggle column formula

D2: =IF(C2=C1,D1,IF(ISNUMBER(D1),D1,0)+1)


2) The SQL in Excel via Data > Get External Data > From Other Source > MS Query ... drill down to my workbook/worksheet named Source...

SELECT min(src.Row) as [Grp1stRow], count(src.DataVal) as  [GrpCount]

FROM `C:\Users\Skip\Documents\tt-SignGroup.xlsx`.`Source$` src

Group By src.toggle
 
...and my results

Grp1stRow	GrpCount
1       	3
4       	4
8       	2
10      	2
12      	2
14      	1
 


Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close