473,386 Members | 1,790 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,386 software developers and data experts.

Exatract Data, thanks.

Greetings!

I would like to extract data from the raw data below, here are the
criteria:

(1) I want accounts starting with 2 only, i.e., excluding accounts
starting with 3.
(2) I want to keep the person_id, account, and open_date for each
account starting with 2.
(3) I want to create a new variable to show each person's earliest
open_date of the accounts starting with 2.

My query below would give me person 10001's earliest open_date for
EVERYONE. Can anyone tell me what the problem is? Thanks a lot!

select person_id, account, open_date
,min_open=(select min(open_date) from test_5 where
person_id=test_5.person_id
and account like '2%')
from test_5
where account like '2%'

-Raw Data-
person_id account open_date
10001 22000001 5/15/2003
10001 22000002 6/20/2004
10001 30000001 2/2/2002
10002 22000003 8/12/2004
10002 22000004 9/15/2004
10002 30000002 2/16/2005

-Ideal Output-
person_id account open_date Min_Open
10001 22000001 5/15/2003 5/15/2003
10001 22000002 6/20/2004 5/15/2003
10002 22000003 8/12/2004 8/12/2004
10002 22000004 9/15/2004 8/12/2004

-My Output-
person_id account open_date Min_Open
10001 22000001 5/15/2003 5/15/2003
10001 22000002 6/20/2004 5/15/2003
10002 22000003 8/12/2004 5/15/2003
10002 22000004 9/15/2004 5/15/2003

Jul 23 '05 #1
3 1123
On 16 Mar 2005 12:17:29 -0800, ro******@gmail.com wrote:

(snip)
My query below would give me person 10001's earliest open_date for
EVERYONE. Can anyone tell me what the problem is? Thanks a lot!

select person_id, account, open_date
,min_open=(select min(open_date) from test_5 where
person_id=test_5.person_id
and account like '2%')
from test_5
where account like '2%'


Hi rong.guo,

You didn't post CREATE TABLE and INSERT statements, so I can't test it,
but I guess that this is caused because you forgot to use aliasses to
distinguish the outer and the inner version of the table.

select person_id, account, open_date
,min_open=(select min(open_date)
from test_5 AS t2
where t2.person_id = t1.person_id
and account like '2%')
from test_5 AS t1
where account like '2%'

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Thanks Hugo! It works perfectly!

I also tried Method 2, which gave me the same result as your method. I
am more used to using JOIN function. But I am wondering if Method 1 has
any advantages over Method 2 in the long term. It seems Method 1
doesn't need GROUP BY while Method 2 does, I am wondering if I have
more than 50 variables in the SELECT, will the GROUP BY in Method 2
create any potential problems? Thanks!

--Method 1--
select person_id, account, open_date
,min_open=(select min(open_date) from test_5 as X where
(X.person_id=Y.person_id
and account like '2%'))
from test_5 as Y
where account like '2%'

--Method 2--
select a.person_id, a.account, a.open_date, min(b.open_date)
from test_5 as a
join test_5 as b
on a.person_id=b.person_id
where a.account like '2%'
group by a.person_id, a.account, a.open_date

Jul 23 '05 #3
On 17 Mar 2005 06:24:54 -0800, ro******@gmail.com wrote:
Thanks Hugo! It works perfectly!

I also tried Method 2, which gave me the same result as your method. I
am more used to using JOIN function. But I am wondering if Method 1 has
any advantages over Method 2 in the long term.
Hi rong.guo,

That's hard to say from here. It depends on lots of factors. The best
way to find out which version is better in your situation, is to test
both (emptying the cache in between) and compare the execution times,
the output of SET STATISTICS IO ON and the execution plans used.

There's also a third method you can use:

SELECT a.person_id, a.account, a.open_date, b.min_open_date
FROM test_5 AS a
JOIN (SELECT person_id, MIN(open_date) AS min_open_date
FROM test_5
WHERE account LIKE '2%'
GROUP BY person_id) AS b
ON a.person_id = b.person_id
WHERE a.account LIKE '2%'

(snip)--Method 2--
select a.person_id, a.account, a.open_date, min(b.open_date)
from test_5 as a
join test_5 as b
on a.person_id=b.person_id
where a.account like '2%'
group by a.person_id, a.account, a.open_date


You'll have to add
AND b.account LIKE '2%'
to this query, otherwise it is not equivalent to query 1.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #4

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

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
14
by: atse | last post by:
Hi experts, I retrieve data from the database and display on ASP, then I export these data to a file, like Excel (the best) or text file. Is it possible? I think it is possible, but how can I do...
2
by: skura | last post by:
I am trying to understand how the data in sql server is stored and also regarding fill factor and page splitting. 1) My first question what is the difference between Index pages and Data pages....
8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
32
by: Neil Ginsberg | last post by:
We're using SQL Server 7 with an Access 2000 MDB as a front end with ODBC linked tables. I recently created a new set of tables for the app, and users are complaining that unsaved data is being...
2
by: davidw | last post by:
As I asked in last post, I want to put some logic in a object and all my webcontrol instance will access that object, the object is responsed to retrieve data from database if the data has not been...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.