Solved

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

Posted on 2014-07-22
6
359 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now