Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

how to query database from this data. ( see inside )

Hello, Expert

I got the data and schema like this

[id] --- [name] --- [timestamp] - [ type]
 1    ---    A   ---- 2012-09-01 10:00:00 --- 1
 2    ---    A   ---- 2012-09-01 10:01:00 --- 2
 3    ---    B   ---- 2012-09-01 10:01:00 --- 1
 4    ---    A   ---- 2012-09-01 10:02:00 --- 2
 5    ---    C   ---- 2012-09-01 10:01:20 --- 1
 6    ---    A   ---- 2012-09-01 10:03:00 --- 2
 7    ---    A   ---- 2012-09-01 10:03:10 --- 2
 8    ---    A   ---- 2012-09-01 10:03:00 --- 1
 9    ---    A   ---- 2012-09-01 10:03:30 --- 1
 10    ---    A   ---- 2012-09-01 10:00:20 --- 1
from above, i want to select "A" with condition
 - type 1 come first  and must follow by 2 then follow by 1 again ( with time sort )....
the result i expected is

A   ---- 2012-09-01 10:00:20 --- 1
A   ---- 2012-09-01 10:03:10 --- 2
A   ---- 2012-09-01 10:03:30 --- 1

is it possible to do this in database side. ( any database sql , nosql  , mssql , etc.. )

Thanks,
0
sora-x
Asked:
sora-x
1 Solution
 
rjohnsonjrCommented:
select name,timestamp, type

from table
where
name='A'
order by timestamp,type asc
0
 
Randy PooleCommented:
select [name],[timestamp], [type] from table where [name]='A' order by [timestamp]

Open in new window

0
 
SurranoCommented:
Can't really see the logic in the output:

  - type 1 come first  and must follow by 2 then follow by 1 again ( with time sort )....

I interpret it something like:
- sort type 1 entries by time
- sort type 2 entries by time
- comb the two together

but your output goes something like:
- pick oldest type 1 entry
- pick newest type 2 entry
- pick type 1 entry that is greater than prev type 2 entry

Can you please provide a sort of the full input set, or describe the sorting requirements precisely?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sora-xAuthor Commented:
sorry for my poor expected output it's should be

A   ---- 2012-09-01 10:00:00 --- 1
A   ---- 2012-09-01 10:01:00 --- 2
A   ---- 2012-09-01 10:03:00 --- 1
A   ---- 2012-09-01 10:03:10 --- 2
A   ---- 2012-09-01 10:03:30 --- 1

the sorting requirement is

- 1 always come before 2
- display the oldest 1
- after display 1 the next must be 2 if got many 2 in the time range display the oldest. ( type 2 timestamp greater than prev
 type 1  but less than next type 1 (if have )   )

- then display next type 1 timestamp that greater than type 2 but less than next type 2 ( if have )
- repeat ..

Thanks
0
 
sora-xAuthor Commented:
This comment inspired me how to do it. Thanks
0
 
SurranoCommented:
Glad I could be your muse ;)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now