Solved

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

Posted on 2014-07-22
6
364 Views
Last Modified: 2014-07-30
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
Comment
Question by:sora-x
6 Comments
 
LVL 6

Expert Comment

by:rjohnsonjr
ID: 40212289
select name,timestamp, type

from table
where
name='A'
order by timestamp,type asc
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40212295
select [name],[timestamp], [type] from table where [name]='A' order by [timestamp]

Open in new window

0
 
LVL 8

Accepted Solution

by:
Surrano earned 500 total points
ID: 40213649
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:sora-x
ID: 40213976
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
 

Author Closing Comment

by:sora-x
ID: 40228753
This comment inspired me how to do it. Thanks
0
 
LVL 8

Expert Comment

by:Surrano
ID: 40228856
Glad I could be your muse ;)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question