Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

SQLQueriesNoCode

The Yahoo! Groups Product Blog

Check it out!

Group Information

  • Members: 3234
  • Category: SQL
  • Founded: Feb 17, 2003
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Messages

Advanced
Messages Help
Messages 844 - 873 of 4871   Oldest  |  < Older  |  Newer >  |  Newest
Messages: Show Message Summaries Sort by Date ^  
#844 From: "rajafaisal09" <rajafaisal09@...>
Date: Sun Jul 4, 2004 12:32 pm
Subject: sql query ?
rajafaisal09
Send Email Send Email
 
Hello ,
well i have a problem in SQL n tht is
... i have a column cantainning  names of students in any
institute ...now i have to write a query tht should give me numeric
answer for the characters present in a students name
suppose i would like a charcter 'A'

Alen = 1 should be displayed
AAbias = 2
Micheal = 1
Missy = 0

any ideas how can i do this in a query

Bye

#845 From: "Michael Gerholdt" <gerholdt@...>
Date: Sun Jul 4, 2004 4:28 pm
Subject: Re: sql query ?
gerholdt@...
Send Email Send Email
 
The solution to this might vary significantly depending upon which vendor
product you are using.

Oracle?
Sybase?
MS SQL Server?
etc


> Hello ,
> well i have a problem in SQL n tht is
> ... i have a column cantainning  names of students in any
> institute ...now i have to write a query tht should give me numeric
> answer for the characters present in a students name
> suppose i would like a charcter 'A'
>
> Alen = 1 should be displayed
> AAbias = 2
> Micheal = 1
> Missy = 0
>
> any ideas how can i do this in a query
>
> Bye

#846 From: Faisal Raja <rajafaisal09@...>
Date: Sun Jul 4, 2004 6:48 pm
Subject: Re: sql query ?
rajafaisal09
Send Email Send Email
 
Well I was looking the solution in Oracle Actually
But thanks for replying i have got the solution now Thanks a lot
it will be like
            Select length(name) - length( replace(name,'A',' ' ))  Name
             from student ;
Well i have got solution like this , If any one know its solution other way round to then plz share it
Thanks to all

Michael Gerholdt <gerholdt@...> wrote:
The solution to this might vary significantly depending upon which vendor
product you are using.

Oracle?
Sybase?
MS SQL Server?
etc


> Hello ,
> well i have a problem in SQL n tht is
> ... i have a column cantainning  names of students in any
> institute ...now i have to write a query tht should give me numeric
> answer for the characters present in a students name
> suppose i would like a charcter 'A'
>
> Alen = 1 should be displayed
> AAbias = 2
> Micheal = 1
> Missy = 0
>
> any ideas how can i do this in a query
>
> Bye





Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!

#847 From: "Michael Gerholdt" <gerholdt@...>
Date: Mon Jul 5, 2004 4:46 am
Subject: Re: sql query ?
gerholdt@...
Send Email Send Email
 
Faisal,

This is a very clever solution!

Remember to upper the name prior to replace since Oracle is by default case
sensitive.

select length(name) - length(replace(upper(name),'A','')) numChars
from student

Regards,
Michael Gerholdt
SUNY College at Fredonia
----- Original Message -----
From: "Faisal Raja" <rajafaisal09@...>

> Well I was looking the solution in Oracle Actually
> But thanks for replying i have got the solution now Thanks a lot
> it will be like
>             Select length(name) - length( replace(name,'A',' ' ))  Name
>              from student ;
> Well i have got solution like this , If any one know its solution other
way round to then plz share it
> Thanks to all

>
> > Hello ,
> > well i have a problem in SQL n tht is
> > ... i have a column cantainning  names of students in any
> > institute ...now i have to write a query tht should give me numeric
> > answer for the characters present in a students name
> > suppose i would like a charcter 'A'
> >
> > Alen = 1 should be displayed
> > AAbias = 2
> > Micheal = 1
> > Missy = 0
> >
> > any ideas how can i do this in a query

#848 From: Michael Gerholdt <gerholdt@...>
Date: Tue Jul 6, 2004 3:08 pm
Subject: ansi joins in Oracle subqueries
gerholdt@...
Send Email Send Email
 
I've been just switching over to using ansi joins when I can. I have an
instance in a subquery that I think should work but it does not.

    SELECT a.spriden_id,
           a.spriden_first_name
      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
       AND a.spriden_pidm = 3344334
       AND b.sgbstdn_term_code_eff = (SELECT max(z.sgbstdn_term_code_eff)
                                        FROM sgbstdn z
                                       WHERE z.sgbstdn_term_code_eff <=
'200420'
                                         AND z.sgbstdn_pidm = b.sgbstdn_pidm)

The above works fine but when I replace the subquery with an ansi join (as
below) I get no results.


    SELECT a.spriden_id,
           a.spriden_first_name
      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
       AND a.spriden_pidm = 3344334
       AND b.sgbstdn_term_code_eff = (SELECT max(z.sgbstdn_term_code_eff)
                                        FROM sgbstdn z join sgbstdn b on
b.sgbstdn_pidm = z.sgbstdn_pidm
                                       WHERE z.sgbstdn_term_code_eff <=
'200420')


Oracle is new to ansi joins as of 9i I believe. Is there a known problem
with ansi joins in subqueries, or am I doing something wrong and just not
seeing it?
--
Michael Gerholdt
Senior Programmer/Analyst
Web Development Manager
706 Maytum Hall
SUNY Fredonia
716.673.3388

#849 From: "John Warner" <john@...>
Date: Tue Jul 6, 2004 5:03 pm
Subject: RE: ansi joins in Oracle subqueries
john@...
Send Email Send Email
 
I think, but I don't use Oracle 9, you have the alias b defined twice. I
would switch one of the two places you mention this table to another
alias say c.

SELECT a.spriden_id,
           a.spriden_first_name
      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
       AND a.spriden_pidm = 3344334
       AND b.sgbstdn_term_code_eff = (SELECT max(z.sgbstdn_term_code_eff)
                                        FROM sgbstdn z join sgbstdn c on
c.sgbstdn_pidm = z.sgbstdn_pidm
                                       WHERE z.sgbstdn_term_code_eff <=
'200420')


You might be confusing the SQL engine. Note just a wild guess.

John Warner
mailto:john@...

> -----Original Message-----
> From: Michael Gerholdt [mailto:gerholdt@...]
> Sent: Tuesday, July 06, 2004 11:08 AM
> To: SQLQueriesNoCode@yahoogroups.com
> Subject: [SQLQueriesNoCode] ansi joins in Oracle subqueries
>
>
> I've been just switching over to using ansi joins when I can.
> I have an instance in a subquery that I think should work but
> it does not.
>
>    SELECT a.spriden_id,
>           a.spriden_first_name
>      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
>       AND a.spriden_pidm = 3344334
>       AND b.sgbstdn_term_code_eff = (SELECT
> max(z.sgbstdn_term_code_eff)
>                                        FROM sgbstdn z
>                                       WHERE
> z.sgbstdn_term_code_eff <= '200420'
>                                         AND z.sgbstdn_pidm =
> b.sgbstdn_pidm)
>
> The above works fine but when I replace the subquery with an
> ansi join (as
> below) I get no results.
>
>
>    SELECT a.spriden_id,
>           a.spriden_first_name
>      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
>       AND a.spriden_pidm = 3344334
>       AND b.sgbstdn_term_code_eff = (SELECT
> max(z.sgbstdn_term_code_eff)
>                                        FROM sgbstdn z join
> sgbstdn b on b.sgbstdn_pidm = z.sgbstdn_pidm
>                                       WHERE z.sgbstdn_term_code_eff <=
> '200420')
>
>
> Oracle is new to ansi joins as of 9i I believe. Is there a
> known problem with ansi joins in subqueries, or am I doing
> something wrong and just not seeing it?
> --
> Michael Gerholdt
> Senior Programmer/Analyst
> Web Development Manager
> 706 Maytum Hall
> SUNY Fredonia
> 716.673.3388
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~-->
> Yahoo! Domains - Claim yours for only $14.70
> http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/m7folB/T> M
>
>
> --------------------------------------------------------------
> ------~->
>
>
> Yahoo! Groups Links
>
>
>
>
>
>

#850 From: Michael Gerholdt <gerholdt@...>
Date: Tue Jul 6, 2004 5:20 pm
Subject: Re: ansi joins in Oracle subqueries
gerholdt@...
Send Email Send Email
 
Ah ... that could be it. I'd still have to join that inner table back to b
as well ...

I'll play with your suggestion.

Thanks

> I think, but I don't use Oracle 9, you have the alias b defined twice. I
> would switch one of the two places you mention this table to another
> alias say c.
>
> SELECT a.spriden_id,
>           a.spriden_first_name
>      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
>       AND a.spriden_pidm = 3344334
>       AND b.sgbstdn_term_code_eff = (SELECT max(z.sgbstdn_term_code_eff)
>                                        FROM sgbstdn z join sgbstdn c on
> c.sgbstdn_pidm = z.sgbstdn_pidm
>                                       WHERE z.sgbstdn_term_code_eff <=
> '200420')
>
>
> You might be confusing the SQL engine. Note just a wild guess.
>
> John Warner
> mailto:john@...
>
> > -----Original Message-----
> > From: Michael Gerholdt [mailto:gerholdt@...]
> > Sent: Tuesday, July 06, 2004 11:08 AM
> > To: SQLQueriesNoCode@yahoogroups.com
> > Subject: [SQLQueriesNoCode] ansi joins in Oracle subqueries
> >
> >
> > I've been just switching over to using ansi joins when I can.
> > I have an instance in a subquery that I think should work but
> > it does not.
> >
> >    SELECT a.spriden_id,
> >           a.spriden_first_name
> >      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
> >       AND a.spriden_pidm = 3344334
> >       AND b.sgbstdn_term_code_eff = (SELECT
> > max(z.sgbstdn_term_code_eff)
> >                                        FROM sgbstdn z
> >                                       WHERE
> > z.sgbstdn_term_code_eff <= '200420'
> >                                         AND z.sgbstdn_pidm =
> > b.sgbstdn_pidm)
> >
> > The above works fine but when I replace the subquery with an
> > ansi join (as
> > below) I get no results.
> >
> >
> >    SELECT a.spriden_id,
> >           a.spriden_first_name
> >      FROM spriden a join sgbstdn b on a.spriden_pidm = b.sgbstdn_pidm
> >       AND a.spriden_pidm = 3344334
> >       AND b.sgbstdn_term_code_eff = (SELECT
> > max(z.sgbstdn_term_code_eff)
> >                                        FROM sgbstdn z join
> > sgbstdn b on b.sgbstdn_pidm = z.sgbstdn_pidm
> >                                       WHERE z.sgbstdn_term_code_eff <=
> > '200420')
> >
> >
> > Oracle is new to ansi joins as of 9i I believe. Is there a
> > known problem with ansi joins in subqueries, or am I doing
> > something wrong and just not seeing it?
> > --
> > Michael Gerholdt
> > Senior Programmer/Analyst
> > Web Development Manager
> > 706 Maytum Hall
> > SUNY Fredonia
> > 716.673.3388
> >
> >
> >
> >
> > ------------------------ Yahoo! Groups Sponsor
> > --------------------~-->
> > Yahoo! Domains - Claim yours for only $14.70
> > http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/m7folB/T> M
> >
> >
> > --------------------------------------------------------------
> > ------~->
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>

#851 From: jephmat@...
Date: Wed Jul 7, 2004 2:07 am
Subject: Substring queries?
ubercat
Send Email Send Email
 
Hello there!  My first post.

I am working on a database that uses first and last names in its
search parameters.  I am using traditional queries such as SELECT *
FROM TABLE WHERE FIRST_NAME = 'JEFFREY';

But is there a way you can fashion the SQL query itself so that it
looks for a substring?  For example, is there a way to have the query
look up the string 'JEFF' and also include in its results the
names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
with J-E-F-F?

#852 From: "pdhakeph" <pdhakeph@...>
Date: Wed Jul 7, 2004 3:05 pm
Subject: Get specifc records from a table
pdhakeph
Send Email Send Email
 
Hi,

I have table as follows

Row Id, fld_1, Date, fld_2, fld_3

fld_1 will be used to join to other table. Multiple records
can have same value for fld_1. The 'Date' field will always have
different value.

What I need is collection of records (all fields) for each unique
value of fld_1, where 'Date' is maximum.

e.g.

Row Id, fld_1, Date, fld_2, fld_3
1, A, 2/2/04, p, q
2, A, 2/3/04, z, v
3, A, 2/4/04, t, y
4, B, 1/3/04, s, d
5, C, 3/3/04, T, h
6, C, 5/6/04, y, u

I should get following set
3, A, 2/4/04, t, y
4, B, 1/3/04, s, d
6, C, 5/6/04, y, u

What would be query ?

Thanks and regards,

Prasanna Dhakephalkar

#853 From: "John Warner" <john@...>
Date: Wed Jul 7, 2004 7:37 pm
Subject: RE: Substring queries?
john@...
Send Email Send Email
 
SELECT First_Name
FROM Table
WHERE First_Name Like 'Jeff%';

John Warner
mailto:john@...

> -----Original Message-----
> From: jephmat@... [mailto:jephmat@...]
> Sent: Tuesday, July 06, 2004 10:08 PM
> To: SQLQueriesNoCode@yahoogroups.com
> Subject: [SQLQueriesNoCode] Substring queries?
>
>
>  Hello there!  My first post.
>
> I am working on a database that uses first and last names in its
> search parameters.  I am using traditional queries such as SELECT *
> FROM TABLE WHERE FIRST_NAME = 'JEFFREY';
>
> But is there a way you can fashion the SQL query itself so that it
> looks for a substring?  For example, is there a way to have the query
> look up the string 'JEFF' and also include in its results the
> names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
> with J-E-F-F?
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~-->
> Yahoo! Domains - Claim yours for only $14.70
> http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/m7folB/T> M
>
>
> --------------------------------------------------------------
> ------~->
>
>
> Yahoo! Groups Links
>
>
>
>
>
>

#854 From: Michael Gerholdt <gerholdt@...>
Date: Wed Jul 7, 2004 7:39 pm
Subject: Re: Substring queries?
gerholdt@...
Send Email Send Email
 
>
> I am working on a database that uses first and last names in its
> search parameters.  I am using traditional queries such as SELECT *
> FROM TABLE WHERE FIRST_NAME = 'JEFFREY';
>
> But is there a way you can fashion the SQL query itself so that it
> looks for a substring?  For example, is there a way to have the query
> look up the string 'JEFF' and also include in its results the
> names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
> with J-E-F-F?
>
>

this is quite simple because you need not actually treat it as a substr and
use any functions to work on the data. Instead, just use the SQL operator
"LIKE" instead of "=" and then make sure you use the percent sign as a wild
card (unless you are in Access where I think it is an asterisk)

Do a search on "LIKE" operator in SQL and you'll be set


where first_name like 'JEFF%'

does what you want above

where first_name like '%na'

will get 'Anna' and 'banana' and anything that ends in 'na'

where first name like 'J%y'

Joy, Jimmy, Jeffrey, etc

where first_name like 'J_y'

Joy, Jay
(the underscore limits to one character)

#855 From: Michael Gerholdt <gerholdt@...>
Date: Wed Jul 7, 2004 7:46 pm
Subject: Re: Get specifc records from a table
gerholdt@...
Send Email Send Email
 
select a.field1, a.field2, b.field3
from tableA a join tableB b on b.fld_1 = a.fld_1
where a.date = (select max(c.Date)
                              from tableA c
                            where c.fld_1 = a.fld_1)




> Hi,
>
> I have table as follows
>
> Row Id, fld_1, Date, fld_2, fld_3
>
> fld_1 will be used to join to other table. Multiple records
> can have same value for fld_1. The 'Date' field will always have
> different value.
>
> What I need is collection of records (all fields) for each unique
> value of fld_1, where 'Date' is maximum.
>
> e.g.
>
> Row Id, fld_1, Date, fld_2, fld_3
> 1, A, 2/2/04, p, q
> 2, A, 2/3/04, z, v
> 3, A, 2/4/04, t, y
> 4, B, 1/3/04, s, d
> 5, C, 3/3/04, T, h
> 6, C, 5/6/04, y, u
>
> I should get following set
> 3, A, 2/4/04, t, y
> 4, B, 1/3/04, s, d
> 6, C, 5/6/04, y, u
>
> What would be query ?
>

#856 From: "(DMR) Mark Gorelik" <MGorelik@...>
Date: Wed Jul 7, 2004 5:36 pm
Subject: RE: Substring queries?
MGorelik@...
Send Email Send Email
 
use  : first_name like '%JEF%'

Mark Gorelik
Fujitsu Consulting
416-969-7141



-----Original Message-----
From: jephmat@... [mailto:jephmat@...]
Sent: Tuesday, July 06, 2004 10:08 PM
To: SQLQueriesNoCode@yahoogroups.com
Subject: [SQLQueriesNoCode] Substring queries?


  Hello there!  My first post.

I am working on a database that uses first and last names in its
search parameters.  I am using traditional queries such as SELECT *
FROM TABLE WHERE FIRST_NAME = 'JEFFREY';

But is there a way you can fashion the SQL query itself so that it
looks for a substring?  For example, is there a way to have the query
look up the string 'JEFF' and also include in its results the
names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
with J-E-F-F?






Yahoo! Groups Links






*** This communication is strictly private and contains confidential information
intended solely for the addressee(s).  Access to this communication by parties
other than the intended recipient(s) is unauthorized.  If you have received this
communication in error and are not the intended recipient, any disclosure,
copying, distribution or action taken is strictly prohibited and may be
unlawful. ***

*** Ce message est strictement prive et contient des renseignements
confidentiels destines uniquement au(x) destinataire(s).  L'acces a ce message
par des parties autres que le(s) destinataire(s) prevu(s) n'est pas autorise. 
Si vous avez reçu ce message par erreur et n'etes pas le destinataire prevu,
toute divulgation, copie, distribution ou action prise est strictement interdite
et peut etre consideree comme illegale. ***

#857 From: "Joe Delpha" <jdelpha@...>
Date: Wed Jul 7, 2004 6:04 pm
Subject: RE: Substring queries?
jdelpha@...
Send Email Send Email
 
Have a go with SELECT * FROM TABLE WHERE FIRST_NAME LIKE 'JEFF%'
Note that the syntax may vary from vendor to vendor.

-----Original Message-----
From: jephmat@... [mailto:jephmat@...]
Sent: Tuesday, July 06, 2004 10:08 PM
To: SQLQueriesNoCode@yahoogroups.com
Subject: [SQLQueriesNoCode] Substring queries?

  Hello there!  My first post.

I am working on a database that uses first and last names in its
search parameters.  I am using traditional queries such as SELECT *
FROM TABLE WHERE FIRST_NAME = 'JEFFREY';

But is there a way you can fashion the SQL query itself so that it
looks for a substring?  For example, is there a way to have the query
look up the string 'JEFF' and also include in its results the
names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
with J-E-F-F?






Yahoo! Groups Links

#858 From: George Clay <georgec36@...>
Date: Wed Jul 7, 2004 7:45 pm
Subject: Re: Substring queries?
georgec36@...
Send Email Send Email
 
There are a couple of ways to do this, using CharIndex or Like.

Select * From Table Where FirstName Like 'Jeff%'

Select * From Table Where CharIndex('Jeff', FirstName) > 0


George Clay
SQL Server DBA
www.sqldudes.com

--- jephmat@... wrote:
>  Hello there!  My first post.
>
> I am working on a database that uses first and last names in its
> search parameters.  I am using traditional queries such as SELECT *
> FROM TABLE WHERE FIRST_NAME = 'JEFFREY';
>
> But is there a way you can fashion the SQL query itself so that it
> looks for a substring?  For example, is there a way to have the query
> look up the string 'JEFF' and also include in its results the
> names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
> with J-E-F-F?
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>




__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

#859 From: "Joe Delpha" <jdelpha@...>
Date: Wed Jul 7, 2004 6:42 pm
Subject: Sql puzzle
jdelpha@...
Send Email Send Email
 

Hello All,

            I have a table with a lay out as follows

OrderNo, OrderLineNo, TransDate, TransType, TransTime, TransAmt, TransQty.

 

The transaction type may be ‘AD’ add, or ‘UP’ update with add always being first within the table.

I would like to select all ‘UP’ transactions for a given transaction day and also select the immediately previous row corresponding to that line.

 

I have tried

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty

FROM TABLEA A

INNER JOIN TABLE A B

            ON A.OrderNo = B.OrderNo AND A.OrderLineNo=B.OrderLineNo

                 AND ((A.TransDate = B.TransDate AND A.TransTime > B.TransTime) OR A.TransDate>B.TransDate)

WHERE A.TransDate=’2004-05-06’ and A.Transtype=’UP’

 

Does anyone know of a method I might use to return only one row from the B side of this query?

 

 


#860 From: "Razvan Socol" <rsocol@...>
Date: Wed Jul 7, 2004 6:28 pm
Subject: Re: Get specifc records from a table
rsocol_fx
Send Email Send Email
 
Prasanna Dhakephalkar wrote:
> [...]
> What I need is collection of records (all fields) for each unique
> value of fld_1, where 'Date' is maximum.
> [...]

SELECT * FROM yourtable a WHERE date=
(SELECT MAX(date) FROM yourtable b WHERE a.fld_1=b.fld_1)

Razvan

#861 From: "Razvan Socol" <rsocol@...>
Date: Wed Jul 7, 2004 8:32 pm
Subject: Re: Substring queries?
rsocol_fx
Send Email Send Email
 
SELECT * FROM your_table WHERE first_name LIKE 'JEFF%'

If you use Microsoft SQL Server, you may also want to try:

SELECT * FROM your_table WHERE DIFFERENCE(first_name, 'Jeffrey')>2

This query can find even variations like "Geoffrey", "Jefry", etc.

For more informations, see "Comparing SOUNDEX and DIFFERENCE" in BOL:
http://msdn.microsoft.com/library/en-us/acdata/ac_8_con_11_1rj9.asp

Razvan

----- Original Message -----
From: jephmat@...
To: SQLQueriesNoCode@yahoogroups.com
Sent: Wednesday, July 07, 2004 5:07 AM
Subject: [SQLQueriesNoCode] Substring queries?

I am working on a database that uses first and last names in its
search parameters.  I am using traditional queries such as SELECT *
FROM TABLE WHERE FIRST_NAME = 'JEFFREY';

But is there a way you can fashion the SQL query itself so that it
looks for a substring?  For example, is there a way to have the query
look up the string 'JEFF' and also include in its results the
names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
with J-E-F-F?

#862 From: Iian Neill <ineill@...>
Date: Thu Jul 8, 2004 1:34 am
Subject: Re: Substring queries?
ineill@...
Send Email Send Email
 
Hi,

For a farther-ranging and perhaps more intuitive search you might like to
use SQL Server's built-in SOUNDEX command. The process dates back to 19th
century, when census-takers needed to account for accidental or traditional
variations in the spellings of surnames: for example, "MacFadyen" and
"McFadden". To my understanding, both names are valid spellings of the same
name, but would not be treated as such in the typical alphabetical listing.
The SOUNDEX process compares the consonantal components - in which case,
both names might render something like "MCFDN".

Apparently the command can also rank the similarity of the matches by
percent. I don't think it works (without tweaking) for surnames that
include hyphens, apostrophes, spaces (i.e, Leonardo "da Vinci"), etc. You
might need to process your search string first (by removing the illegal
characters).

          regards,
          Iian

At 12:45 PM 7/7/2004 -0700, you wrote:
>There are a couple of ways to do this, using CharIndex or Like.
>
>Select * From Table Where FirstName Like 'Jeff%'
>
>Select * From Table Where CharIndex('Jeff', FirstName) > 0
>
>
>George Clay
>SQL Server DBA
>www.sqldudes.com
>
>--- jephmat@... wrote:
> >  Hello there!  My first post.
> >
> > I am working on a database that uses first and last names in its
> > search parameters.  I am using traditional queries such as SELECT *
> > FROM TABLE WHERE FIRST_NAME = 'JEFFREY';
> >
> > But is there a way you can fashion the SQL query itself so that it
> > looks for a substring?  For example, is there a way to have the query
> > look up the string 'JEFF' and also include in its results the
> > names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
> > with J-E-F-F?
> >
> >
> >
> >
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
>
>
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Mail is new and improved - Check it out!
>http://promotions.yahoo.com/new_mail
>
>
>
>
>
>Yahoo! Groups Links
>
>
>
>

#863 From: "Razvan Socol" <rsocol@...>
Date: Thu Jul 8, 2004 6:22 am
Subject: Re: Sql puzzle
rsocol_fx
Send Email Send Email
 
Which is the primary key in this table ? Don't tell me you don't have a primary key...
I suppose the unique key is (OrderNo, OrderLineNo, TransDate, TransTime).
If you also have a surrogate primary key (let's call it PK), the following query might work:
 
SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A2.PK, X.MaxOfTransDate, MAX(B2.TransTime) as MaxOfTransTime FROM YourTable A2
  INNER JOIN YourTable B2 ON A2.OrderNo = B2.OrderNo AND A2.OrderLineNo = B2.OrderLineNo
  INNER JOIN (
    SELECT A1.PK, MAX(B1.TransDate) AS MaxOfTransDate FROM YourTable A1
    INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
    GROUP BY A1.PK
  ) X ON B2.OrderNo = X.OrderNo AND B2.OrderLineNo = X.OrderLineNo
  AND A2.PK = X.PK AND B2.TransDate = X.MaxOfTransDate
  GROUP BY A2.PK, X.MaxOfTransDate
) Y ON B.OrderNo = Y.OrderNo AND B.OrderLineNo = Y.OrderLineNo
AND A.PK = Y.PK AND B.TransDate = Y.MaxOfTransDate AND B.TransTime = Y.MaxOfTransTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'

If your DMBS allows adding a date with a time, you could the following query:
 
SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A1.PK, MAX(B1.TransDate+B1.TransTime) AS MaxOfTransDateTime FROM YourTable A1
  INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
  GROUP BY A1.PK
) X ON B.OrderNo = X.OrderNo AND B.OrderLineNo = X.OrderLineNo
AND A.PK = X.PK AND B.TransDate + B.TransTime = X.MaxOfTransDateTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'
The following query would be shorter (and maybe faster), but it's not ANSI-SQL because it contains the TOP clause:
 
SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A CROSS JOIN YourTable B
WHERE B.PK IN (
  SELECT TOP 1 PK FROM YourTable B1 WHERE A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
  ORDER BY B1.TransDate+B1.TransTime DESC
) AND A.TransDate='2004-05-06' and A.Transtype='UP'
If you don't have a single-column primary key, you should use all the columns that are part of the primary key, instead of the column that I've named "PK":
 
SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransTime, MAX(B1.TransDate+B1.TransTime) AS MaxOfTransDateTime
  FROM YourTable A1 INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
  GROUP BY A.OrderNo, A.OrderLineNo, A.TransDate, A.TransTime
) X ON B.OrderNo = X.OrderNo AND B.OrderLineNo = X.OrderLineNo
AND A.OrderNo = X.OrderNo AND A.OrderLineNo = X.OrderLineNo AND A.TransDate = X.TransDate AND A.TransTime = X.TransTime
AND B.TransDate + B.TransTime = X.MaxOfTransDateTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'
 
Even if this query works, let me add a warning: I think that you are on the wrong track (you want to do something that you should not want to do...). I can't explain why, without knowing the whole picture, but I don't want to know the whole picture (because I imagine that it's a big picture).
 
If these queries didn't work (or did not give the expected results), and you need further help, please provide:
- the exact structure of the table, including primary keys and unique keys (as a CREATE TABLE statement)
- sample data, (as INSERT ... VALUES statements)
- expected results
 
Razvan
----- Original Message -----
From: Joe Delpha
Sent: Wednesday, July 07, 2004 9:42 PM
Subject: [SQLQueriesNoCode] Sql puzzle

Hello All,

            I have a table with a lay out as follows

OrderNo, OrderLineNo, TransDate, TransType, TransTime, TransAmt, TransQty.

 

The transaction type may be ‘AD’ add, or ‘UP’ update with add always being first within the table.

I would like to select all ‘UP’ transactions for a given transaction day and also select the immediately previous row corresponding to that line.

 

I have tried

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty

FROM TABLEA A

INNER JOIN TABLE A B

            ON A.OrderNo = B.OrderNo AND A.OrderLineNo=B.OrderLineNo

                 AND ((A.TransDate = B.TransDate AND A.TransTime > B.TransTime) OR A.TransDate>B.TransDate)

WHERE A.TransDate=’2004-05-06’ and A.Transtype=’UP’

 

Does anyone know of a method I might use to return only one row from the B side of this query?


#864 From: "James P" <pjames_in@...>
Date: Thu Jul 8, 2004 4:50 am
Subject: Re: Substring queries?
pjames_in
Send Email Send Email
 
yes..you can use the soundex function for this...see the books online for the detailed syntax 
 
-------Original Message-------
 
Date: 07/08/04 04:27:18
Subject: Re: [SQLQueriesNoCode] Substring queries?
 
SELECT * FROM your_table WHERE first_name LIKE 'JEFF%'
 
If you use Microsoft SQL Server, you may also want to try:
 
SELECT * FROM your_table WHERE DIFFERENCE(first_name, 'Jeffrey')>2
 
This query can find even variations like "Geoffrey", "Jefry", etc.
 
For more informations, see "Comparing SOUNDEX and DIFFERENCE" in BOL:
 
Razvan
 
----- Original Message -----
Sent: Wednesday, July 07, 2004 5:07 AM
Subject: [SQLQueriesNoCode] Substring queries?
 
I am working on a database that uses first and last names in its
search parameters.  I am using traditional queries such as SELECT *
FROM TABLE WHERE FIRST_NAME = 'JEFFREY';
 
But is there a way you can fashion the SQL query itself so that it
looks for a substring?  For example, is there a way to have the query
look up the string 'JEFF' and also include in its results the
names 'JEFFREY', 'JEFFERY', 'JEFFY' and anything else that begins
with J-E-F-F?
 
 
 
 
------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
--------------------------------------------------------------------~->
 
 
Yahoo! Groups Links
 
<*> To visit your group on the web, go to:
 
<*> To unsubscribe from this group, send an email to:
 
<*> Your use of Yahoo! Groups is subject to:
 
 
____________________________________________________
  IncrediMail - Email has finally evolved - Click Here

#865 From: "Balmukund Lakhani" <lakhani@...>
Date: Thu Jul 8, 2004 7:13 am
Subject: RE:Get specifc records from a table
bmlakhani
Send Email Send Email
 
Hi Prasanna Dhakephalkar,

below query may help you

set nocount on
if exists ( select 'X'
    from sysobjects
    where  type = 'U'
    and  name = 'SQL_QNC')
begin
  drop table SQL_QNC
end
go
Create table SQL_QNC(
RowId int,
fld_1 Varchar(10),
Date datetime,
fld_2 Varchar(10),
fld_3 Varchar(10))
go
insert into SQL_QNC
values(1, 'A', '02-Feb-2004', 'fld_21', 'fld_31')
insert into SQL_QNC
values(2, 'A', '02-Mar-2004', 'fld_22', 'fld_32')
insert into SQL_QNC
values(3, 'A', '02-Apr-2004', 'fld_23', 'fld_33')
insert into SQL_QNC
values(4, 'B', '01-Mar-2004', 'fld_24', 'fld_34')
insert into SQL_QNC
values(5, 'C', '03-Mar-2004', 'fld_25', 'fld_35')
insert into SQL_QNC
values(6, 'C', '03-Jun-2004', 'fld_26', 'fld_36')
go
SELECT * FROM SQL_QNC
select  outer_table.RowId,
   outer_table.fld_1,
   outer_table.Date,
   outer_table.fld_2,
   outer_table.fld_3
from  SQL_QNC outer_table,
   (select fld_1  'FLD_1_INNER',
     max(Date) 'DATE_INNER'
   from   SQL_QNC
   group by fld_1) A
where A.DATE_INNER = outer_table.date
and  A.FLD_1_INNER = outer_table.fld_1
ORDER BY 1




set nocount off



---------------------
    Date: Wed, 07 Jul 2004 15:05:06 -0000
    From: "pdhakeph" <pdhakeph@...>
Subject: Get specifc records from a table

Hi,

I have table as follows

Row Id, fld_1, Date, fld_2, fld_3

fld_1 will be used to join to other table. Multiple records
can have same value for fld_1. The 'Date' field will always have
different value.

What I need is collection of records (all fields) for each unique
value of fld_1, where 'Date' is maximum.

e.g.

Row Id, fld_1, Date, fld_2, fld_3
1, A, 2/2/04, p, q
2, A, 2/3/04, z, v
3, A, 2/4/04, t, y
4, B, 1/3/04, s, d
5, C, 3/3/04, T, h
6, C, 5/6/04, y, u

I should get following set
3, A, 2/4/04, t, y
4, B, 1/3/04, s, d
6, C, 5/6/04, y, u

What would be query ?

Thanks and regards,

Prasanna Dhakephalkar
DISCLAIMER:

Information transmitted by this e-mail may be proprietary to Ramco
Systems Ltd., and / or the authors of the information and is intended for
use only by the individual or entity to which it is addressed, and may
contain confidential or legally privileged information. If you are not
the intended recipient or it appears that this mail has been forwarded
to you without proper authority, you are not authorised to access, read,
disclose, copy, use or otherwise deal with it and any such actions are
prohibited and may be unlawful.

Internet communications cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, arrive late or
contain viruses. Ramco Systems Limited therefore does not accept
liability for any errors, omissions, viruses or computer problems experienced
as a result of this transmission.

If you have received this e-mail in error, please notify us immediately
at mail to: mailadmin@... and delete this mail from your
records. Notice is hereby given that no representation, contract or other
binding obligation shall be created by this e-mail.

#866 From: "pdhakeph" <pdhakeph@...>
Date: Thu Jul 8, 2004 5:52 am
Subject: Re: Get specifc records from a table
pdhakeph
Send Email Send Email
 
Hi,

Thanks ,Michael Gerholdt, Razvan Socol

Regards,

Prasanna.

#867 From: "Darryl" <darryl.godden@...>
Date: Thu Jul 8, 2004 9:57 am
Subject: (No subject)
Fairlane6
Send Email Send Email
 
Hi Guys,



This should be so simple, but I am flumoxed how to achieve it, I
have 2 tables, one with the customers details and a second table
detailing years attended, I would like to pull in the following
information:



tblCustomers

ID
FirstName



tblAttended

EventYear
CustID
Attended

The tblCustomers.ID is the link to the tblAttended.CustID to select
the customers and the years, the field attended is yes/no to see if
they did or didn't. I would like to show the results as follows,
basically a count of the years attended with their name:

Customer          No of Years Attended

L Hughes          4

J Williams        6


Can you show me the way…please?

Regards,

Darryl

#868 From: "John Warner" <john@...>
Date: Thu Jul 8, 2004 1:16 pm
Subject: RE: Sql puzzle
john@...
Send Email Send Email
 
One of life's imponderables, why do people request help with a query, remember to show us tables, but forget to mention DBMS and omit telling us  what the Key(Indexed) fields are in the tables?
 

John Warner
mailto:john@...

-----Original Message-----
From: Razvan Socol [mailto:rsocol@...]
Sent: Thursday, July 08, 2004 2:23 AM
To: SQLQueriesNoCode@yahoogroups.com
Subject: Re: [SQLQueriesNoCode] Sql puzzle

Which is the primary key in this table ? Don't tell me you don't have a primary key...
I suppose the unique key is (OrderNo, OrderLineNo, TransDate, TransTime).
If you also have a surrogate primary key (let's call it PK), the following query might work:
 

#869 From: nilesh babar <nilesh_babar2003@...>
Date: Thu Jul 8, 2004 1:21 pm
Subject: Re: (unknown)
nilesh_babar...
Send Email Send Email
 
Hi,
 
Check with following scripts
 
select a.id,a.name,count(custid) from tblCustomers a,tblAttended b
where a.id=b.custid and b.atended=yes
group by b.custid
i think this will solve ur problem
 
r,
nilesh
Darryl <darryl.godden@...> wrote:
Hi Guys,



This should be so simple, but I am flumoxed how to achieve it, I
have 2 tables, one with the customers details and a second table
detailing years attended, I would like to pull in the following
information:



tblCustomers

ID
FirstName



tblAttended

EventYear
CustID
Attended

The tblCustomers.ID is the link to the tblAttended.CustID to select
the customers and the years, the field attended is yes/no to see if
they did or didn't. I would like to show the results as follows,
basically a count of the years attended with their name:

Customer          No of Years Attended

L Hughes          4

J Williams        6


Can you show me the way…please?

Regards,

Darryl






ALL-NEW Yahoo! Messenger - sooooo many all-new ways to express yourself

#870 From: "Godden, Darryl" <darryl.godden@...>
Date: Thu Jul 8, 2004 1:04 pm
Subject: RE: (unknown)
Fairlane6
Send Email Send Email
 
Hi Guys,

Forgot I posted this, solved it already:

SELECT tblCustomers.FirstName, COUNT(tblAttended.attended) AS
EventsAttended FROM tblAttended, tblCustomers WHERE tblAttended.custID
=  tblCustomers.ID GROUP BY tblCustomers.FirstName
	 ORDER BY tblCustomers.FirstName

Thanks in anticipation of the replies you were preparing...

Darryl

-----Original Message-----
From: Godden, Darryl
Sent: 08 July 2004 10:57
To: SQLQueriesNoCode@yahoogroups.com
Subject: [SQLQueriesNoCode] (unknown)

Hi Guys,



This should be so simple, but I am flumoxed how to achieve it, I
have 2 tables, one with the customers details and a second table
detailing years attended, I would like to pull in the following
information:



tblCustomers

ID
FirstName



tblAttended

EventYear
CustID
Attended

The tblCustomers.ID is the link to the tblAttended.CustID to select
the customers and the years, the field attended is yes/no to see if
they did or didn't. I would like to show the results as follows,
basically a count of the years attended with their name:

Customer          No of Years Attended

L Hughes          4

J Williams        6


Can you show me the way...please?

Regards,

Darryl








Yahoo! Groups Links






***
This e-mail and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they are
addressed.  If you have received this e-mail in error please notify the
system manager using the details below. The contents of this e-mail
represent the views of the individual(s) named above and do not
necessarily represent the views of Merthyr Tydfil County Borough
Council.

Mae'r e-bost hwn ac unrhyw ffeiliau a drosglwyddir gydag ef yn
gyfrinachol ac wedi'u bwriadu ar gyfer pwy bynnag y cyfeirir ef ato neu
atynt. Os ydych wedi ei dderbyn drwy gamgymeriad yna gadewch i'r rheolwr
systemau wybod drwy ddefnyddio'r manylion isod. Mae cynnwys yr e-bost
hwn cynrychioli barn y sawl a enwir uchod, felly nid ydyw'n dilyn ei fod
yn cynrychioli barn Cyngor Bwrdeistref Sirol Merthyr
Tudful.

Cyngor Bwrdeistref Sirol Merthyr Tudful
Canolfan Dinesig
Stryd Y Castell
Merthyr Tudful
CF47 8AN

Merthyr Tydfil County Borough Council
Civic Centre
Castle Street
Merthyr Tydfil
CF47 8AN

Teleffon\Telephone: 01685 725000

http://www.merthyr.gov.uk
____________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

***
This e-mail and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.  If
you have received this e-mail in error please notify the system manager using
the details below. The contents of this e-mail represent the views of the
individual(s) named above and do not necessarily represent the views of Merthyr
Tydfil County Borough Council.

Mae'r e-bost hwn ac unrhyw ffeiliau a drosglwyddir gydag ef yn gyfrinachol ac
wedi'u bwriadu ar gyfer pwy bynnag y cyfeirir ef ato neu atynt. Os ydych wedi ei
dderbyn drwy gamgymeriad yna gadewch i'r rheolwr systemau wybod drwy
ddefnyddio'r manylion isod. Mae cynnwys yr e-bost hwn cynrychioli barn y sawl a
enwir uchod, felly nid ydyw'n dilyn ei fod yn cynrychioli barn Cyngor
Bwrdeistref Sirol Merthyr
Tudful.

Cyngor Bwrdeistref Sirol Merthyr Tudful
Canolfan Dinesig
Stryd Y Castell
Merthyr Tudful
CF47 8AN

Merthyr Tydfil County Borough Council
Civic Centre
Castle Street
Merthyr Tydfil
CF47 8AN

Teleffon\Telephone: 01685 725000

http://www.merthyr.gov.uk
____________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

#871 From: nilesh babar <nilesh_babar2003@...>
Date: Thu Jul 8, 2004 1:45 pm
Subject: Re: (unknown)
nilesh_babar...
Send Email Send Email
 
Hi Darryl,
sorry ignore previous mail pls check following script
 
select a.id,count(b.custid) from tblCustomers a,tblAttended b
where a.id=b.custid and b.atended=yes
group by a.id,b.custid
 
or
 
select a.id,count(b.custid) from tblCustomers a,tblAttended b
where a.id=b.custid
group by a.id,b.custid
 
 
and finaly
 
select cust.id,cust.firstname,d.noofatt from
tblCustomers  cust,
(select a.id,count(b.custid) as noofatt from tblCustomers a,tblAttended b
where a.id=b.custid
group by a.id,b.custid)d
where cust.id=d.id
 
/r,
nilesh
 
 
 
 


Darryl <darryl.godden@...> wrote:
Hi Guys,



This should be so simple, but I am flumoxed how to achieve it, I
have 2 tables, one with the customers details and a second table
detailing years attended, I would like to pull in the following
information:



tblCustomers

ID
FirstName



tblAttended

EventYear
CustID
Attended

The tblCustomers.ID is the link to the tblAttended.CustID to select
the customers and the years, the field attended is yes/no to see if
they did or didn't. I would like to show the results as follows,
basically a count of the years attended with their name:

Customer          No of Years Attended

L Hughes          4

J Williams        6


Can you show me the way…please?

Regards,

Darryl






ALL-NEW Yahoo! Messenger - sooooo many all-new ways to express yourself

#872 From: "Joe Delpha" <jdelpha@...>
Date: Thu Jul 8, 2004 7:31 pm
Subject: RE: Sql puzzle
jdelpha@...
Send Email Send Email
 

Many thanks Razvan. Perfect.

I have been able to select precisely what I need using you’re  many examples.

I am pulling the update row for the date, along with the immediately preceding (and only that one preceding) row for the order line.

I am looking to compare the update quantity and amount of the update, with those from the previous transaction for the order line.

I apologize for the lack of detail on my original inquiry. I’m a bit new at this SQL thing.

Here is my query if anyone is interested.  It is probably not the best solution but it does accomplish what I need.

                                                                                                                                                Thanks again.

SELECT A.A2ORD,

       A.A2LIN,

       A.AUDP2,

       A.AUDDT,

       A.AUDTM,

       A.A2QUO AS ‘UPDQTY’,

       A.A2ASP AS ‘UPDAMT’,

       B.A2ORD,

       B.A2LIN,

       B.AUDDT,

       B.AUDTM,

       B.A2QUO AS ‘PRVQTY’,

       B.A2ASP AS ‘PRVAMT’

FROM MYTABLE A

     INNER JOIN MYTABLE B

     ON A.A2ORD=B.A2ORD

        AND A.A2LIN=B.A2LIN

     INNER JOIN

           (SELECT D.A2ORD,

                   D.A2LIN,

                   MAX(CONCAT(CHAR(D.AUDDT),CHAR(D.AUDTM))) AS MAXDATETIME

             FROM MYTABLE C

                  INNER JOIN MYTABLE D

                  ON C.A2ORD=D.A2ORD

                  AND C.A2LIN=D.A2LIN AND

                  ((C.AUDDT=D.AUDDT AND C.AUDTM>D.AUDTM) OR C.AUDDT>D.AUDDT)

             WHERE C.AUDDT='2004-05-06' AND C.AUDP2='UP'

             GROUP BY D.A2ORD,D.A2LIN) X

     ON B.A2ORD=X.A2ORD

        AND B.A2LIN=X.A2LIN AND

        CONCAT(CHAR(B.AUDDT),CHAR(B.AUDTM))=X.MAXDATETIME 

WHERE A.AUDDT='2004-05-06' AND A.AUDP2='UP'   

 

 

 


From: Razvan Socol [mailto:rsocol@...]
Sent: Thursday, July 08, 2004 2:23 AM
To: SQLQueriesNoCode@yahoogroups.com
Subject: Re: [SQLQueriesNoCode] Sql puzzle

 

Which is the primary key in this table ? Don't tell me you don't have a primary key...

I suppose the unique key is (OrderNo, OrderLineNo, TransDate, TransTime).

If you also have a surrogate primary key (let's call it PK), the following query might work:

 

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A2.PK, X.MaxOfTransDate, MAX(B2.TransTime) as MaxOfTransTime FROM YourTable A2
  INNER JOIN YourTable B2 ON A2.OrderNo = B2.OrderNo AND A2.OrderLineNo = B2.OrderLineNo
  INNER JOIN (
    SELECT A1.PK, MAX(B1.TransDate) AS MaxOfTransDate FROM YourTable A1
    INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
    GROUP BY A1.PK
  ) X ON B2.OrderNo = X.OrderNo AND B2.OrderLineNo = X.OrderLineNo
  AND A2.PK = X.PK AND B2.TransDate = X.MaxOfTransDate
  GROUP BY A2.PK, X.MaxOfTransDate
) Y ON B.OrderNo = Y.OrderNo AND B.OrderLineNo = Y.OrderLineNo
AND A.PK = Y.PK AND B.TransDate = Y.MaxOfTransDate AND B.TransTime = Y.MaxOfTransTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'

If your DMBS allows adding a date with a time, you could the following query:

 

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A1.PK, MAX(B1.TransDate+B1.TransTime) AS MaxOfTransDateTime FROM YourTable A1
  INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
  GROUP BY A1.PK
) X ON B.OrderNo = X.OrderNo AND B.OrderLineNo = X.OrderLineNo
AND A.PK = X.PK AND B.TransDate + B.TransTime = X.MaxOfTransDateTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'

The following query would be shorter (and maybe faster), but it's not ANSI-SQL because it contains the TOP clause:

 

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A CROSS JOIN YourTable B
WHERE B.PK IN (
  SELECT TOP 1 PK FROM YourTable B1 WHERE A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
  ORDER BY B1.TransDate+B1.TransTime DESC
) AND A.TransDate='2004-05-06' and A.Transtype='UP'

If you don't have a single-column primary key, you should use all the columns that are part of the primary key, instead of the column that I've named "PK":

 

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty
FROM YourTable A INNER JOIN YourTable B ON A.OrderNo = B.OrderNo AND A.OrderLineNo = B.OrderLineNo
INNER JOIN (
  SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransTime, MAX(B1.TransDate+B1.TransTime) AS MaxOfTransDateTime
  FROM YourTable A1 INNER JOIN YourTable B1 ON A1.OrderNo = B1.OrderNo AND A1.OrderLineNo = B1.OrderLineNo
  GROUP BY A.OrderNo, A.OrderLineNo, A.TransDate, A.TransTime
) X ON B.OrderNo = X.OrderNo AND B.OrderLineNo = X.OrderLineNo
AND A.OrderNo = X.OrderNo AND A.OrderLineNo = X.OrderLineNo AND A.TransDate = X.TransDate AND A.TransTime = X.TransTime
AND B.TransDate + B.TransTime = X.MaxOfTransDateTime
WHERE A.TransDate='2004-05-06' and A.Transtype='UP'

 

Even if this query works, let me add a warning: I think that you are on the wrong track (you want to do something that you should not want to do...). I can't explain why, without knowing the whole picture, but I don't want to know the whole picture (because I imagine that it's a big picture).

 

If these queries didn't work (or did not give the expected results), and you need further help, please provide:

- the exact structure of the table, including primary keys and unique keys (as a CREATE TABLE statement)

- sample data, (as INSERT ... VALUES statements)

- expected results

 

Razvan

----- Original Message -----

From: Joe Delpha

Sent: Wednesday, July 07, 2004 9:42 PM

Subject: [SQLQueriesNoCode] Sql puzzle

 

Hello All,

            I have a table with a lay out as follows

OrderNo, OrderLineNo, TransDate, TransType, TransTime, TransAmt, TransQty.

 

The transaction type may be ‘AD’ add, or ‘UP’ update with add always being first within the table.

I would like to select all ‘UP’ transactions for a given transaction day and also select the immediately previous row corresponding to that line.

 

I have tried

SELECT A.OrderNo, A.OrderLineNo, A.TransDate, A.TransAmt, A.TransQty, B.TransAmt, B.TransQty

FROM TABLEA A

INNER JOIN TABLE A B

            ON A.OrderNo = B.OrderNo AND A.OrderLineNo=B.OrderLineNo

                 AND ((A.TransDate = B.TransDate AND A.TransTime > B.TransTime) OR A.TransDate>B.TransDate)

WHERE A.TransDate=’2004-05-06’ and A.Transtype=’UP’

 

Does anyone know of a method I might use to return only one row from the B side of this query?




#873 From: "Bob Schroeder" <bob@...>
Date: Fri Jul 9, 2004 4:47 pm
Subject: SELECT statement help please
soaringcorp
Send Email Send Email
 
Consider the following table in sql server 2000.   ProjectInfo
(Project, Phase, Manager, Assistant)  --- all varchar fields, no
keys.

The datalooks like:

Project1, Phase1, Bill, Tom
Project1, Phase2, Bill, Tom
Project2, Phase1, Jill, Joe
Project3, Phase1, Jill, Ted
Project3, Phase2, Jill, Tom

I want to select all records except if for any given project if the
manager and assistant are the same I only want to select one record
and not return the phase.

So using the example above I want my result set to be:

Project1, <null>, Bill, Tom
Project2, Phase1, Jill, Joe
Project3, Phase1, Jill, Ted
Project3, Phase2, Jill, Tom

Is there way to do this in a select statement (preferably without
cursors)?

Thanks!!

Messages 844 - 873 of 4871   Oldest  |  < Older  |  Newer >  |  Newest
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help