Solved

SyBase SQL Query

Posted on 2014-11-13
7
143 Views
Last Modified: 2014-11-24
Hello Experts,

I have a unique situation where I need to group by a column but the column has the data stored in a way where I cant figure out how to group by. The DB table in question contains employees and which store they have access to (S1 = Store1, S2 = Store 2, S17 = Store17, etc). I want to see all employees grouped by stores they have access to. As an example, employee "KRISHNA C." would have be apart of stores: 1, 4, 5, 6, AND 7. (see attached)

This is SyBase SQL Anywhere v10
table.png
0
Comment
Question by:triphen
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40442290
this is how you want to see them? or this is the main table??
0
 

Author Comment

by:triphen
ID: 40443385
This is the main table.....

I want to see (as a crude example)


S1
KRISHNA C.
ELNA
JUNE

S4
KRISHNA C.

S5
KRISHNA C.

S6
KRISHNA C.

S7
KRISHNA C.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40444064
The main table contains the stores as a single combined string?  That does make it hard.

If there are a reasonably small number of different stores, you could do something like:
select 'S1' store, name from main_table where stores like '%,S1,%'
union all
select 'S2' store, name from main_table where stores like '%,S2,%'
-- etc

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:triphen
ID: 40444083
The main table contains the stores as a single combined string?  That does make it hard.


Yes Exactly!

This worked, but I imagine there has to be a better way. Currently there are 20 stores, but there will be more. Any other ideas?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40444099
I don't have a better idea in pure sql.  Personally, I'd write a perl script to process this data and generate the output you want (any language with decent string handling should work).

This should work except for the $connect_string and real table name:
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect($connect_string) or die $DBI::errstr;
my $sth = $dbh->prepare("select name, stores from main_table");
$sth->execute;
my %stores;
while (my $row = $sth->fetchrow_arrayref) {
    foreach my $store (split /,/, $row->[1]) {
        next if ($store =~ /^\s*$/);
        $stores{$store} = [] unless $stores{$store};
        push @{$stores{$store}}, $name;
    }
}
$sth->finish;
foreach my $store (sort keys %stores) {
    print $store, ":\n", join("\n", @{$stores{$store}}), "\n\n";
}

Open in new window

0
 

Author Comment

by:triphen
ID: 40454051
I dont know perl.

I know VB.net is VS2010...any ideas with that?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40454274
It's been something like 15 years since I coded in VB (pre-.Net).  I've thought about learning C# but haven't gotten around to it yet...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now