| |
|
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.
|
 |
|

08-07-10, 23:56
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
|
|
Quote:
Originally Posted by r937
... 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.
|
|

08-08-10, 07:56
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
|
|
Quote:
Originally Posted by sakitram
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
|
|

08-08-10, 08:37
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
|
|
Quote:
Originally Posted by r937
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
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
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
|
|

08-08-10, 09:54
|
|
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
|
|

08-08-10, 10:33
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
Quote:
Originally Posted by r937
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
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..
|
|

08-08-10, 15:36
|
|
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
|
|

08-09-10, 01:10
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
Quote:
Originally Posted by r937
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
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.. 
|
|

08-09-10, 05:15
|
|
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)
|
|

08-09-10, 05:41
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
great!! It's amazing.. Thanks. 
|
|

08-09-10, 07:58
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
|
|
Quote:
Originally Posted by sakitram
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!!!
|
|

08-09-10, 08:01
|
|
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.
|
|

08-09-10, 08:18
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
|
|
Quote:
Originally Posted by hobbylu
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
|
|

08-09-10, 08:20
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
|
|
Quote:
Originally Posted by r937
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
|
|

08-09-10, 08:47
|
|
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
|
|

08-09-10, 10:12
|
|
Registered User
|
|
Join Date: Nov 2002
Posts: 21
|
|
Right SQL, Right Database
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|