473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Sql select question

Hello everybody,

i have the following table (agltransact), in which 2 fields are relevant:

ex_inv_ref account
15 1512
15 6040
16 1512
16 1512
16 6040
16 6040
17 1512
17 1512
17 1512
17 6040
17 6040
18 1512
18 1512
18 6040
18 6040
18 6040
18 6040

I would like to select the ext_inv_value for which there is not an
*equal* number of accounts 1512 and 6040; so this is :

ext_inv_ref
-----
17
18

I tried

select ext_inv_ref,
from agltransact
where client='MG' and account in('1512','6040') and ext_inv_type >= 15
and ext_inv_type <= 17
group by ext_inv_ref
having round(count(account)/2,0) != count(account)/2;

this select only those ext_inv_ref with an uneven number of accounts, so
ext_inv_ref
-----------
17

how would you write a query like that ?

thanks,
Andy
Jul 19 '05 #1
7 2703
this is a good application of decode -- do your GROUP BY and use DECODE
twice in the HAVING clause -- both nested in a COUNT() functions, one which
computes a count of #1512 accounts, one that computes a count of #6040
accounts -- your HAVING clause should qualify rows where these counts are
not equal

see recent references to PIVOT queries for similar examples

let me know if you need further help

-- mcs

"andy vandenberghe" <hp**************@skynet.be_removethis> wrote in message
news:hp************************************@news.s kynet.be...
| Hello everybody,
|
| i have the following table (agltransact), in which 2 fields are relevant:
|
| ex_inv_ref account
| 15 1512
| 15 6040
| 16 1512
| 16 1512
| 16 6040
| 16 6040
| 17 1512
| 17 1512
| 17 1512
| 17 6040
| 17 6040
| 18 1512
| 18 1512
| 18 6040
| 18 6040
| 18 6040
| 18 6040
|
| I would like to select the ext_inv_value for which there is not an
| *equal* number of accounts 1512 and 6040; so this is :
|
| ext_inv_ref
| -----
| 17
| 18
|
| I tried
|
| select ext_inv_ref,
| from agltransact
| where client='MG' and account in('1512','6040') and ext_inv_type >= 15
| and ext_inv_type <= 17
| group by ext_inv_ref
| having round(count(account)/2,0) != count(account)/2;
|
| this select only those ext_inv_ref with an uneven number of accounts, so
| ext_inv_ref
| -----------
| 17
|
| how would you write a query like that ?
|
| thanks,
| Andy
Jul 19 '05 #2
VC
Hello andy,

Given:

create table t1(ex_inv_ref int, account int);

the simplest way to do what you want is:

select a.ex_inv_ref, count_6040, count_1512 from
(select ex_inv_ref, count(*) count_6040 from t1 where account=6040 group
by ex_inv_ref) a,
(select ex_inv_ref, count(*) count_1512 from t1 where account=1512 group
by ex_inv_ref) b
where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;

Rgds.
"andy vandenberghe" <hp**************@skynet.be_removethis> wrote in message
news:hp************************************@news.s kynet.be...
Hello everybody,

i have the following table (agltransact), in which 2 fields are relevant:

ex_inv_ref account
15 1512
15 6040
16 1512
16 1512
16 6040
16 6040
17 1512
17 1512
17 1512
17 6040
17 6040
18 1512
18 1512
18 6040
18 6040
18 6040
18 6040

I would like to select the ext_inv_value for which there is not an
*equal* number of accounts 1512 and 6040; so this is :

ext_inv_ref
-----
17
18

I tried

select ext_inv_ref,
from agltransact
where client='MG' and account in('1512','6040') and ext_inv_type >= 15
and ext_inv_type <= 17
group by ext_inv_ref
having round(count(account)/2,0) != count(account)/2;

this select only those ext_inv_ref with an uneven number of accounts, so
ext_inv_ref
-----------
17

how would you write a query like that ?

thanks,
Andy

Jul 19 '05 #3
This is abit shorter...

select ex_inv_ref
from agltransact
where account in (1512, 6040)
having sum(decode(account, 1512, 1, -1)) <> 0
group by ex_inv_ref;

Shaun.

"VC" <bo*******@hotmail.com> wrote in message news:<C5Tub.191428$275.658505@attbi_s53>...
Hello andy,

Given:

create table t1(ex_inv_ref int, account int);

the simplest way to do what you want is:

select a.ex_inv_ref, count_6040, count_1512 from
(select ex_inv_ref, count(*) count_6040 from t1 where account=6040 group
by ex_inv_ref) a,
(select ex_inv_ref, count(*) count_1512 from t1 where account=1512 group
by ex_inv_ref) b
where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;

Rgds.
"andy vandenberghe" <hp**************@skynet.be_removethis> wrote in message
news:hp************************************@news.s kynet.be...
Hello everybody,

i have the following table (agltransact), in which 2 fields are relevant:

ex_inv_ref account
15 1512
15 6040
16 1512
16 1512
16 6040
16 6040
17 1512
17 1512
17 1512
17 6040
17 6040
18 1512
18 1512
18 6040
18 6040
18 6040
18 6040

I would like to select the ext_inv_value for which there is not an
*equal* number of accounts 1512 and 6040; so this is :

ext_inv_ref
-----
17
18

I tried

select ext_inv_ref,
from agltransact
where client='MG' and account in('1512','6040') and ext_inv_type >= 15
and ext_inv_type <= 17
group by ext_inv_ref
having round(count(account)/2,0) != count(account)/2;

this select only those ext_inv_ref with an uneven number of accounts, so
ext_inv_ref
-----------
17

how would you write a query like that ?

thanks,
Andy

Jul 19 '05 #4
clever answer -- did not look correct at first, since i was expecting a
decode for each account value

looks like your decode is incrementing the sum for #1512, and decrementing
for 'all others' -- which means #6040; so if they don't balance, you get
your rows of interest

a couple code maintenance observations
[_] i am so used to putting GROUP BY before HAVING that i have long
forgotten that HAVING can come first -- but for clarity, i would recommend
listing GROUP BY first
[_] to make the purpose of the DECODE more clear, you may want to include
6040 explicitly, or include an inline comment on the
not-immediately-apparent logic

i'm sure others will have other opinions

-- mcs

"Mr E Guest" <pi*********@virgin.net> wrote in message
news:a5**************************@posting.google.c om...
| This is abit shorter...
|
| select ex_inv_ref
| from agltransact
| where account in (1512, 6040)
| having sum(decode(account, 1512, 1, -1)) <> 0
| group by ex_inv_ref;
|
| Shaun.
|
| "VC" <bo*******@hotmail.com> wrote in message
news:<C5Tub.191428$275.658505@attbi_s53>...
| > Hello andy,
| >
| > Given:
| >
| > create table t1(ex_inv_ref int, account int);
| >
| > the simplest way to do what you want is:
| >
| > select a.ex_inv_ref, count_6040, count_1512 from
| > (select ex_inv_ref, count(*) count_6040 from t1 where account=6040
group
| > by ex_inv_ref) a,
| > (select ex_inv_ref, count(*) count_1512 from t1 where account=1512
group
| > by ex_inv_ref) b
| > where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;
| >
| > Rgds.
| >
| >
| > "andy vandenberghe" <hp**************@skynet.be_removethis> wrote in
message
| > news:hp************************************@news.s kynet.be...
| > > Hello everybody,
| > >
| > > i have the following table (agltransact), in which 2 fields are
relevant:
| > >
| > > ex_inv_ref account
| > > 15 1512
| > > 15 6040
| > > 16 1512
| > > 16 1512
| > > 16 6040
| > > 16 6040
| > > 17 1512
| > > 17 1512
| > > 17 1512
| > > 17 6040
| > > 17 6040
| > > 18 1512
| > > 18 1512
| > > 18 6040
| > > 18 6040
| > > 18 6040
| > > 18 6040
| > >
| > > I would like to select the ext_inv_value for which there is not an
| > > *equal* number of accounts 1512 and 6040; so this is :
| > >
| > > ext_inv_ref
| > > -----
| > > 17
| > > 18
| > >
| > > I tried
| > >
| > > select ext_inv_ref,
| > > from agltransact
| > > where client='MG' and account in('1512','6040') and ext_inv_type >= 15
| > > and ext_inv_type <= 17
| > > group by ext_inv_ref
| > > having round(count(account)/2,0) != count(account)/2;
| > >
| > > this select only those ext_inv_ref with an uneven number of accounts,
so
| > > ext_inv_ref
| > > -----------
| > > 17
| > >
| > > how would you write a query like that ?
| > >
| > > thanks,
| > > Andy
Jul 19 '05 #5
Hi VC,

Thanks, your code worked allright; here's the real-world translation.

/* list all accounts where the count is not equal */
select a.voucher_no, a.ext_inv_ref, a.count_700500, b.count_400
from (select voucher_no, ext_inv_ref, count(*) count_700500
from agltransact
where client='MG'
and account=700500
and voucher_type in('P4','P5')
and voucher_no >= 63000020
and voucher_no <= 63000024
group by voucher_no, ext_inv_ref) a,
(select voucher_no, ext_inv_ref, count(*) count_400
from agltransact
where client='MG'
and account in('400400','400500','400301','400300')
and voucher_type in('P4','P5')
and voucher_no >= 63000020
and voucher_no <= 63000023
group by voucher_no, ext_inv_ref) b
where a.ext_inv_ref = b.ext_inv_ref
and a.voucher_no=b.voucher_no
and a.count_700500 != b.count_400;

Hrgds,
andy

In article <C5Tub.191428$275.658505@attbi_s53>,
"VC" <bo*******@hotmail.com> wrote:
Hello andy,

Given:

create table t1(ex_inv_ref int, account int);

the simplest way to do what you want is:

select a.ex_inv_ref, count_6040, count_1512 from
(select ex_inv_ref, count(*) count_6040 from t1 where account=6040 group
by ex_inv_ref) a,
(select ex_inv_ref, count(*) count_1512 from t1 where account=1512 group
by ex_inv_ref) b
where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;

Rgds.

Jul 19 '05 #6
mcs,

It's a fair cop, guv - I'm always guilty of leaving comments out -
totally agree with explicitly including account 6040 (here I blame the
days when our sole server was very short on space!). The solution is
more cunning than logical, I guess - desperate to make it as short as
possible - doesn't help on the clarity front, but it makes me feel
better. ;-)

Shaun.

"mcstock" <mc*************@spamdamenquery.com> wrote in message news:<oI********************@comcast.com>...
clever answer -- did not look correct at first, since i was expecting a
decode for each account value

looks like your decode is incrementing the sum for #1512, and decrementing
for 'all others' -- which means #6040; so if they don't balance, you get
your rows of interest

a couple code maintenance observations
[_] i am so used to putting GROUP BY before HAVING that i have long
forgotten that HAVING can come first -- but for clarity, i would recommend
listing GROUP BY first
[_] to make the purpose of the DECODE more clear, you may want to include
6040 explicitly, or include an inline comment on the
not-immediately-apparent logic

i'm sure others will have other opinions

-- mcs

"Mr E Guest" <pi*********@virgin.net> wrote in message
news:a5**************************@posting.google.c om...
| This is abit shorter...
|
| select ex_inv_ref
| from agltransact
| where account in (1512, 6040)
| having sum(decode(account, 1512, 1, -1)) <> 0
| group by ex_inv_ref;
|
| Shaun.
|
| "VC" <bo*******@hotmail.com> wrote in message
news:<C5Tub.191428$275.658505@attbi_s53>...
| > Hello andy,
| >
| > Given:
| >
| > create table t1(ex_inv_ref int, account int);
| >
| > the simplest way to do what you want is:
| >
| > select a.ex_inv_ref, count_6040, count_1512 from
| > (select ex_inv_ref, count(*) count_6040 from t1 where account=6040
group
| > by ex_inv_ref) a,
| > (select ex_inv_ref, count(*) count_1512 from t1 where account=1512
group
| > by ex_inv_ref) b
| > where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;
| >
| > Rgds.
| >
| >
| > "andy vandenberghe" <hp**************@skynet.be_removethis> wrote in
message
| > news:hp************************************@news.s kynet.be...
| > > Hello everybody,
| > >
| > > i have the following table (agltransact), in which 2 fields are
relevant:
| > >
| > > ex_inv_ref account
| > > 15 1512
| > > 15 6040
| > > 16 1512
| > > 16 1512
| > > 16 6040
| > > 16 6040
| > > 17 1512
| > > 17 1512
| > > 17 1512
| > > 17 6040
| > > 17 6040
| > > 18 1512
| > > 18 1512
| > > 18 6040
| > > 18 6040
| > > 18 6040
| > > 18 6040
| > >
| > > I would like to select the ext_inv_value for which there is not an
| > > *equal* number of accounts 1512 and 6040; so this is :
| > >
| > > ext_inv_ref
| > > -----
| > > 17
| > > 18
| > >
| > > I tried
| > >
| > > select ext_inv_ref,
| > > from agltransact
| > > where client='MG' and account in('1512','6040') and ext_inv_type >= 15
| > > and ext_inv_type <= 17
| > > group by ext_inv_ref
| > > having round(count(account)/2,0) != count(account)/2;
| > >
| > > this select only those ext_inv_ref with an uneven number of accounts,
so
| > > ext_inv_ref
| > > -----------
| > > 17
| > >
| > > how would you write a query like that ?
| > >
| > > thanks,
| > > Andy

Jul 19 '05 #7
short on storage? my first 'real' project had to be implemented in
interpreted BASIC on a TRaSh-80 running XENIX (BASIC because the company
president felt he understood it) -- to conserve space we were limited to
2-character variable names, and no (as in absolutely no) space between
keywords. so, all comments had to be, well, shall we say, under my breath

"Mr E Guest" <pi*********@virgin.net> wrote in message
news:a5**************************@posting.google.c om...
| mcs,
|
| It's a fair cop, guv - I'm always guilty of leaving comments out -
| totally agree with explicitly including account 6040 (here I blame the
| days when our sole server was very short on space!). The solution is
| more cunning than logical, I guess - desperate to make it as short as
| possible - doesn't help on the clarity front, but it makes me feel
| better. ;-)
|
| Shaun.
|
| "mcstock" <mc*************@spamdamenquery.com> wrote in message
news:<oI********************@comcast.com>...
| > clever answer -- did not look correct at first, since i was expecting a
| > decode for each account value
| >
| > looks like your decode is incrementing the sum for #1512, and
decrementing
| > for 'all others' -- which means #6040; so if they don't balance, you get
| > your rows of interest
| >
| > a couple code maintenance observations
| > [_] i am so used to putting GROUP BY before HAVING that i have long
| > forgotten that HAVING can come first -- but for clarity, i would
recommend
| > listing GROUP BY first
| > [_] to make the purpose of the DECODE more clear, you may want to
include
| > 6040 explicitly, or include an inline comment on the
| > not-immediately-apparent logic
| >
| > i'm sure others will have other opinions
| >
| > -- mcs
| >
| > "Mr E Guest" <pi*********@virgin.net> wrote in message
| > news:a5**************************@posting.google.c om...
| > | This is abit shorter...
| > |
| > | select ex_inv_ref
| > | from agltransact
| > | where account in (1512, 6040)
| > | having sum(decode(account, 1512, 1, -1)) <> 0
| > | group by ex_inv_ref;
| > |
| > | Shaun.
| > |
| > | "VC" <bo*******@hotmail.com> wrote in message
| > news:<C5Tub.191428$275.658505@attbi_s53>...
| > | > Hello andy,
| > | >
| > | > Given:
| > | >
| > | > create table t1(ex_inv_ref int, account int);
| > | >
| > | > the simplest way to do what you want is:
| > | >
| > | > select a.ex_inv_ref, count_6040, count_1512 from
| > | > (select ex_inv_ref, count(*) count_6040 from t1 where account=6040
| > group
| > | > by ex_inv_ref) a,
| > | > (select ex_inv_ref, count(*) count_1512 from t1 where account=1512
| > group
| > | > by ex_inv_ref) b
| > | > where a.ex_inv_ref=b.ex_inv_ref and count_6040 != count_1512;
| > | >
| > | > Rgds.
| > | >
| > | >
| > | > "andy vandenberghe" <hp**************@skynet.be_removethis> wrote in
| > message
| > | > news:hp************************************@news.s kynet.be...
| > | > > Hello everybody,
| > | > >
| > | > > i have the following table (agltransact), in which 2 fields are
| > relevant:
| > | > >
| > | > > ex_inv_ref account
| > | > > 15 1512
| > | > > 15 6040
| > | > > 16 1512
| > | > > 16 1512
| > | > > 16 6040
| > | > > 16 6040
| > | > > 17 1512
| > | > > 17 1512
| > | > > 17 1512
| > | > > 17 6040
| > | > > 17 6040
| > | > > 18 1512
| > | > > 18 1512
| > | > > 18 6040
| > | > > 18 6040
| > | > > 18 6040
| > | > > 18 6040
| > | > >
| > | > > I would like to select the ext_inv_value for which there is not an
| > | > > *equal* number of accounts 1512 and 6040; so this is :
| > | > >
| > | > > ext_inv_ref
| > | > > -----
| > | > > 17
| > | > > 18
| > | > >
| > | > > I tried
| > | > >
| > | > > select ext_inv_ref,
| > | > > from agltransact
| > | > > where client='MG' and account in('1512','6040') and ext_inv_type
= 15

| > | > > and ext_inv_type <= 17
| > | > > group by ext_inv_ref
| > | > > having round(count(account)/2,0) != count(account)/2;
| > | > >
| > | > > this select only those ext_inv_ref with an uneven number of
accounts,
| > so
| > | > > ext_inv_ref
| > | > > -----------
| > | > > 17
| > | > >
| > | > > how would you write a query like that ?
| > | > >
| > | > > thanks,
| > | > > Andy
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified)...
6
by: @sh | last post by:
Need help again! Here's the head script... ---------------------------------------------------------------------------------------- function Ash_ChangePicFromSelect(TheSource,TheDestination) {...
1
by: Christopher DeMarco | last post by:
Hi all... I've written a class to provide an interface to popen; I've included the actual select() loop below. I'm finding that "sometimes" popen'd processes take "a really long time" to...
5
by: GTi | last post by:
Whats wring with this code? <select class=EditField size="1" name="PlantUnitID" title="Select line"> <option value="0" >Standalone Unit</option> <option value="1" selected >Connected Unit...
2
by: Eitan | last post by:
Hello, I want a solutions for a compicateds sql select statments. The selects can use anything : views, stored procedures, analytic functions, etc... (not use materialized view, unless it is...
21
beacon
by: beacon | last post by:
Hello to everybody, I have a section on a form that has 10 questions, numbered 1-10, with 3 option buttons per question. Each of the option buttons have the same response (Yes, No, Don't know),...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.