If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > query help

Reply
 
LinkBack Thread Tools Display Modes
  #16 (permalink)  
Old 08-07-10, 23:56
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
... except that there will be only one table involved in the query, namely, the single table that you talked about in your first post
I do not understand the purpose of creating a brand new quest as the following query already fetch each employee's latest sales date.

SELECT a.emp_id, max(b.dt) sales_date
FROM tmp a left join tmp b
on a.emp_id=b.emp_id
GROUP BY a.emp_id

Please give me the correct query, I can not proceed further..

Thanks.
Reply With Quote
  #17 (permalink)  
Old 08-08-10, 07:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
Please give me the correct query, I can not proceed further..
wtf is the "tmp" table? where did the emp_monthly_sales table go?

why are you joining the table to itself?

please just give me the query to get each employee's latest date from the emp_monthly_sales table, so that i can show you how to build the final query that you're looking for
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #18 (permalink)  
Old 08-08-10, 08:37
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
wtf is the "tmp" table? where did the emp_monthly_sales table go?
tmp is used instead of emp_monthly_sale as the table name is big.

Quote:
Originally Posted by r937 View Post
why are you joining the table to itself?
I join two tables one used as a primary key table and another used as a foreign key table to fetch max date of sales.

Quote:
Originally Posted by r937 View Post
please just give me the query to get each employee's latest date from the emp_monthly_sales table, so that i can show you how to build the final query that you're looking for
SELECT a.emp_id, max(b.dt) sales_date
FROM emp_monthly_sales a left join emp_monthly_sales b
on a.emp_id=b.emp_id
GROUP BY a.emp_id

Thanks
Reply With Quote
  #19 (permalink)  
Old 08-08-10, 09:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
lolz, you are persistent, aren't you

okay, here's what i was looking for:
Code:
SELECT emp_id
     , MAX(dt) AS latest_date
  FROM emp_monthly_sales
GROUP 
    BY emp_id
latest date per employee

simple, yes?

please study it carefully and let me know when we can proceed to the second stage because we'll be using this exact query as part of it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #20 (permalink)  
Old 08-08-10, 10:33
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
lolz, you are persistent, aren't you

okay, here's what i was looking for:
Code:
SELECT emp_id
     , MAX(dt) AS latest_date
  FROM emp_monthly_sales
GROUP 
    BY emp_id
latest date per employee

simple, yes?
it's simple.. thanks.

Quote:
Originally Posted by r937 View Post
please study it carefully and let me know when we can proceed to the second stage because we'll be using this exact query as part of it
please let's proceed to the next stage..
Reply With Quote
  #21 (permalink)  
Old 08-08-10, 15:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
good

next stage is to join the original table onto this query

when a query is used in the FROM clause, as a subquery, it is known as an inline view or derived table, because you can think of the results it produces as thought they were a table, which they are

so the rows would be one row per employee and the columns would be emp_id and latest date
Code:
SELECT ...
  FROM ( previous query goes here ) AS q
INNER
  JOIN emp_monthly_sales AS t
    ON t.emp_id = q.emp_id
   AND t.dt = q.latest_date
you are joining each employee's latest date

now, as a result of the join, you can show the value for t.sales_amount, and you are now sure that it is the correct amount for the latest date
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #22 (permalink)  
Old 08-09-10, 01:10
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
when a query is used in the FROM clause, as a subquery, it is known as an inline view or derived table, because you can think of the results it produces as thought they were a table
ah!! that's the trick to use that I never used in my experience..

Quote:
Originally Posted by r937 View Post
now, as a result of the join, you can show the value for t.sales_amount, and you are now sure that it is the correct amount for the latest date
prepared the final query and it's working fine..

thanks for your patience on me..
Reply With Quote
  #23 (permalink)  
Old 08-09-10, 05:15
hobbylu hobbylu is offline
Registered User
 
Join Date: Nov 2002
Posts: 21
Follow sql can do

select * from emp_monthly_sales group by emp_id having date_time=max(date_time)
Reply With Quote
  #24 (permalink)  
Old 08-09-10, 05:41
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
great!! It's amazing.. Thanks.
Reply With Quote
  #25 (permalink)  
Old 08-09-10, 07:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by sakitram View Post
great!! It's amazing.. Thanks.
no, it is not great!!

it is not amazing, either, except in a negative way -- it is amazing how wrong it is

my query produces --
Code:
emp_id  date_time  sales_amount
  102   2010-01-18   750000
  101   2010-04-09  1800000
hobbylu's query produces --
Code:
emp_id  date_time  sales_amount
  102   2010-01-18   750000
see? wrong results!!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #26 (permalink)  
Old 08-09-10, 08:01
hobbylu hobbylu is offline
Registered User
 
Join Date: Nov 2002
Posts: 21
What version if your ASE?
How to get the result with my sql?
Maybe you get wrong way.
I test it at 12.5.3/12.5.4/15.0.3/12.0/11.9. It always get right result.
Reply With Quote
  #27 (permalink)  
Old 08-09-10, 08:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by hobbylu View Post
What version if your ASE?
sorry, i did not use ASE

if your query actually works and actually produces the right results, then that's great, but it says a lot about sybase

my apologies
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #28 (permalink)  
Old 08-09-10, 08:20
sakitram sakitram is offline
Registered User
 
Join Date: Aug 2010
Posts: 13
Quote:
Originally Posted by r937 View Post
no, it is not great!!

it is not amazing, either, except in a negative way -- it is amazing how wrong it is
hobbylu's query produces right result in my Sybase server as well.
my server version = ASE 12.5.2

I do not know what's wrong.
Anyway.. thanks
Reply With Quote
  #29 (permalink)  
Old 08-09-10, 08:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
if it produces the right result, then it can't be wrong, can it

you might have to unlearn some techniques if you ever work with a different daabase, though
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #30 (permalink)  
Old 08-09-10, 10:12
hobbylu hobbylu is offline
Registered User
 
Join Date: Nov 2002
Posts: 21
Right SQL, Right Database
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On