 |
|
 |
|
Next: Parameters were not supplied for the function - S..
|
| Author |
Message |
External

Since: Jun 11, 2008 Posts: 3
|
(Msg. 1) Posted: Wed Jun 11, 2008 12:46 am
Post subject: using kind of alias in sqlplus Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
hi all.
Can someone please help me on the following.
Example query:
Tabel1 contains a lot of items.
TABLE1
ITEM VARCHAR2(30),
FAM1 VARCHAR2(10),
FAM2 VARCHAR2(10),
FAM3 VARCHAR2(10),
FAM4 VARCHAR2(10)
Table contains a lot of records with the same itemnumber.
Every item has a family combination from fam1 until fam4
I need a list of items and all fam's with the following rules:
if all FAM1 are equal for that item take FAM1 otherwise get default
value 1111
If all FAM1 are equal for that item take FAM2 otherwise get default
value 2222
If all FAM1 are equal for that item take FAM3 otherwise get default
value 3333
If all FAM1 are equal for that item take FAM4 otherwise get default
value 4444
I'm using now the following query: (its just a translation of my query
in easy example)
select decode((select count(*)
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1),0,t.fam1,1111)
F1,
decode((select count(*)
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1),0,t.fam2,2222)
F2,
decode((select count(*)
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1),0,t.fam3,3333)
F3,
decode((select count(*)
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1),0,t.fam4,4444)
F4
from table1 u
Because it does the count 4 times (for every FAM) it takes long time.
Is there a way I can use the results of the first count in the 3 last
decodes?
(bad example) but kind of:
select decode((select count(*) xxxx
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1),0,t.fam1,1111)
F1,
decode(&xxxx,0,t.fam2,2222) F2,
decode(&xxxx,0,t.fam3,3333) F3,
decode(&xxxx,0,t.fam4,4444) F4
from table1 u
I've tried this one:
select decode(cnt.xxxx,0,t.fam1,1111) F1,
decode(cnt.xxxx,0,t.fam2,2222) F2,
decode(cnt.xxxx,0,t.fam3,3333) F3,
decode(cnt.xxxx,0,t.fam4,4444) F4
from table1 u,
( select count(*) xxxx
from table1 t
where t.item = u.item
and t.fam1 <> u.fam1) cnt
but the variables u.fam1 and u.item are not passed trough.
Puting this in the where clause from u.table1 doesn't help either
because the <> exclude a lot of items
select decode(cnt.xxxx,0,t.fam1,1111) F1,
decode(cnt.xxxx,0,t.fam2,2222) F2,
decode(cnt.xxxx,0,t.fam3,3333) F3,
decode(cnt.xxxx,0,t.fam4,4444) F4
from table1 u,
( select t.item itm,
t.fam1 fa1,
count(*) xxxx
from table1 t
) cnt
where u.item = cnt.itm
and u.fam1 <> cnt.fa1
Any idea/help how to solve this?
Thanks in advance.
Ronald. >> Stay informed about: using kind of alias in sqlplus |
|
| Back to top |
|
 |  |
External

Since: Jun 01, 2004 Posts: 652
|
(Msg. 2) Posted: Wed Jun 11, 2008 2:08 pm
Post subject: Re: using kind of alias in sqlplus [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Ronald" wrote in message
> hi all.
>
> Can someone please help me on the following.
>
> Example query:
>
> Tabel1 contains a lot of items.
>
> TABLE1
> ITEM VARCHAR2(30),
> FAM1 VARCHAR2(10),
> FAM2 VARCHAR2(10),
> FAM3 VARCHAR2(10),
> FAM4 VARCHAR2(10)
>
> Table contains a lot of records with the same itemnumber.
> Every item has a family combination from fam1 until fam4
>
> I need a list of items and all fam's with the following rules:
> if all FAM1 are equal for that item take FAM1 otherwise get default
> value 1111
> If all FAM1 are equal for that item take FAM2 otherwise get default
> value 2222
> If all FAM1 are equal for that item take FAM3 otherwise get default
> value 3333
> If all FAM1 are equal for that item take FAM4 otherwise get default
> value 4444
>
> I'm using now the following query: (its just a translation of my query
> in easy example)
>
> select decode((select count(*)
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1),0,t.fam1,1111)
> F1,
> decode((select count(*)
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1),0,t.fam2,2222)
> F2,
> decode((select count(*)
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1),0,t.fam3,3333)
> F3,
> decode((select count(*)
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1),0,t.fam4,4444)
> F4
> from table1 u
>
>
> Because it does the count 4 times (for every FAM) it takes long time.
> Is there a way I can use the results of the first count in the 3 last
> decodes?
>
> (bad example) but kind of:
>
>
> select decode((select count(*) xxxx
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1),0,t.fam1,1111)
> F1,
> decode(&xxxx,0,t.fam2,2222) F2,
> decode(&xxxx,0,t.fam3,3333) F3,
> decode(&xxxx,0,t.fam4,4444) F4
> from table1 u
>
>
> I've tried this one:
>
>
> select decode(cnt.xxxx,0,t.fam1,1111) F1,
> decode(cnt.xxxx,0,t.fam2,2222) F2,
> decode(cnt.xxxx,0,t.fam3,3333) F3,
> decode(cnt.xxxx,0,t.fam4,4444) F4
> from table1 u,
> ( select count(*) xxxx
> from table1 t
> where t.item = u.item
> and t.fam1 <> u.fam1) cnt
>
>
> but the variables u.fam1 and u.item are not passed trough.
> Puting this in the where clause from u.table1 doesn't help either
> because the <> exclude a lot of items
>
> select decode(cnt.xxxx,0,t.fam1,1111) F1,
> decode(cnt.xxxx,0,t.fam2,2222) F2,
> decode(cnt.xxxx,0,t.fam3,3333) F3,
> decode(cnt.xxxx,0,t.fam4,4444) F4
> from table1 u,
> ( select t.item itm,
> t.fam1 fa1,
> count(*) xxxx
> from table1 t
> ) cnt
> where u.item = cnt.itm
> and u.fam1 <> cnt.fa1
>
>
> Any idea/help how to solve this?
>
> Thanks in advance.
>
> Ronald.
Hi Ronald
This is a SQL Server newsgroup, so knowledge of Oracle is limited!
Its not clear to me exactly you are trying to do posting DDL and example
data with the expected output would help considerably
I think your last example is probably near to what you want, hopefully you
can do this in oracle.
select t.item item,
COUNT ( DISTINCT t.fam1) fam1,
COUNT ( DISTINCT t.fam2) fam2,
COUNT ( DISTINCT t.fam3) fam3
COUNT ( DISTINCT t.fam4) fam4
from table1 t
group by t.item
Will tell you if for each item the number of distinct values for the columns
are unique.
SELECT u.item,
DECODE(cnt.fam1,1,t.fam1,1111) F1,
DECODE(cnt.fam2,1,t.fam2,2222) F2,
DECODE(cnt.fam3,1,t.fam3,3333) F3,
DECODE(cnt.fam4,1,t.fam4,4444) F4
from table1 u,
( select t.item item,
COUNT ( DISTINCT t.fam1) fam1,
COUNT ( DISTINCT t.fam2) fam2,
COUNT ( DISTINCT t.fam3) fam3
COUNT ( DISTINCT t.fam4) fam4
from table1 t
group by t.item ) cnt
WHERE u.item = cnt.item
HTH
John >> Stay informed about: using kind of alias in sqlplus |
|
| Back to top |
|
 |  |
External

Since: Jun 11, 2008 Posts: 3
|
(Msg. 3) Posted: Thu Jun 12, 2008 1:17 am
Post subject: Re: using kind of alias in sqlplus [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Jun 11, 3:08 pm, "John Bell" wrote:
> "Ronald" wrote in message
>
>
>
>
>
> > hi all.
>
> > Can someone please help me on the following.
>
> > Example query:
>
> > Tabel1 contains a lot of items.
>
> > TABLE1
> > ITEM VARCHAR2(30),
> > FAM1 VARCHAR2(10),
> > FAM2 VARCHAR2(10),
> > FAM3 VARCHAR2(10),
> > FAM4 VARCHAR2(10)
>
> > Table contains a lot of records with the same itemnumber.
> > Every item has a family combination from fam1 until fam4
>
> > I need a list of items and all fam's with the following rules:
> > if all FAM1 are equal for that item take FAM1 otherwise get default
> > value 1111
> > If all FAM1 are equal for that item take FAM2 otherwise get default
> > value 2222
> > If all FAM1 are equal for that item take FAM3 otherwise get default
> > value 3333
> > If all FAM1 are equal for that item take FAM4 otherwise get default
> > value 4444
>
> > I'm using now the following query: (its just a translation of my query
> > in easy example)
>
> > select decode((select count(*)
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1),0,t.fam1,1111)
> > F1,
> > decode((select count(*)
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1),0,t.fam2,2222)
> > F2,
> > decode((select count(*)
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1),0,t.fam3,3333)
> > F3,
> > decode((select count(*)
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1),0,t.fam4,4444)
> > F4
> > from table1 u
>
> > Because it does the count 4 times (for every FAM) it takes long time.
> > Is there a way I can use the results of the first count in the 3 last
> > decodes?
>
> > (bad example) but kind of:
>
> > select decode((select count(*) xxxx
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1),0,t.fam1,1111)
> > F1,
> > decode(&xxxx,0,t.fam2,2222) F2,
> > decode(&xxxx,0,t.fam3,3333) F3,
> > decode(&xxxx,0,t.fam4,4444) F4
> > from table1 u
>
> > I've tried this one:
>
> > select decode(cnt.xxxx,0,t.fam1,1111) F1,
> > decode(cnt.xxxx,0,t.fam2,2222) F2,
> > decode(cnt.xxxx,0,t.fam3,3333) F3,
> > decode(cnt.xxxx,0,t.fam4,4444) F4
> > from table1 u,
> > ( select count(*) xxxx
> > from table1 t
> > where t.item = u.item
> > and t.fam1 <> u.fam1) cnt
>
> > but the variables u.fam1 and u.item are not passed trough.
> > Puting this in the where clause from u.table1 doesn't help either
> > because the <> exclude a lot of items
>
> > select decode(cnt.xxxx,0,t.fam1,1111) F1,
> > decode(cnt.xxxx,0,t.fam2,2222) F2,
> > decode(cnt.xxxx,0,t.fam3,3333) F3,
> > decode(cnt.xxxx,0,t.fam4,4444) F4
> > from table1 u,
> > ( select t.item itm,
> > t.fam1 fa1,
> > count(*) xxxx
> > from table1 t
> > ) cnt
> > where u.item = cnt.itm
> > and u.fam1 <> cnt.fa1
>
> > Any idea/help how to solve this?
>
> > Thanks in advance.
>
> > Ronald.
>
> Hi Ronald
>
> This is a SQL Server newsgroup, so knowledge of Oracle is limited!
>
> Its not clear to me exactly you are trying to do posting DDL and example
> data with the expected output would help considerably
>
> I think your last example is probably near to what you want, hopefully you
> can do this in oracle.
>
> select t.item item,
> COUNT ( DISTINCT t.fam1) fam1,
> COUNT ( DISTINCT t.fam2) fam2,
> COUNT ( DISTINCT t.fam3) fam3
> COUNT ( DISTINCT t.fam4) fam4
> from table1 t
> group by t.item
>
> Will tell you if for each item the number of distinct values for the columns
> are unique.
>
> SELECT u.item,
> DECODE(cnt.fam1,1,t.fam1,1111) F1,
> DECODE(cnt.fam2,1,t.fam2,2222) F2,
> DECODE(cnt.fam3,1,t.fam3,3333) F3,
> DECODE(cnt.fam4,1,t.fam4,4444) F4
> from table1 u,
> ( select t.item item,
> COUNT ( DISTINCT t.fam1) fam1,
> COUNT ( DISTINCT t.fam2) fam2,
> COUNT ( DISTINCT t.fam3) fam3
> COUNT ( DISTINCT t.fam4) fam4
> from table1 t
> group by t.item ) cnt
> WHERE u.item = cnt.item
>
> HTH
>
> John
Many thanks John !
Will try it.
Best regards,
Ronald. >> Stay informed about: using kind of alias in sqlplus |
|
| Back to top |
|
 |  |
| Related Topics: | Column for storing any kind of text? - I'd like suggestions about the security of storing any type of text in a varchar(MAX) column. I have web browser based application with one section that is basically Notepad. People can paste what every kind of text they like. There isn't any..
Comparing execution plans - is there any kind of DIFF for .. - Hi, Let's say I have two SQL queries (SELECT statements) and I need to compare their execution plans. Can anybody recommend any tool or technique that would work better than doing visual comparison in Management Studio? Of course, I am most concerned..
using a VALUE + a STRING as an ALIAS? - Hi, I'd like to translate this from Access SQL to 'true' SQL: SELECT "http:/mydomain/" & [Image] AS image_url I've tried several things but SQL Server doesn't accept it. How can I add this string to the value of that field? Thank you v...
Way to alias a database? - Hi, I'm using MS Sql 2005. When I run a query from within "MyDatabase" that accesses another database, "OtherDatabase", is there a way I can create an alias, "OtherDatabaseOtherName", to access "OtherDatabase" so...
Table alias with UPDATE ? - Is there a way to use a table alias with an UPDATE that has no FROM clause? I'm using SQL 2000. For example: UPDATE TableLongName SET myfield3 = 789 WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456 works, but isn't as easy to read... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|