开发者

SQL: to decide to use it or not

开发者 https://www.devze.com 2023-02-01 05:24 出处:网络
Hallo: I did some text processing of a database using Shell and Python. For interoperability, I am thinking to do it using SQL. SQL is good 开发者_JAVA技巧for some query task. But i am not sure if th

Hallo: I did some text processing of a database using Shell and Python. For interoperability, I am thinking to do it using SQL. SQL is good 开发者_JAVA技巧for some query task. But i am not sure if the SQL can handle all my tasks. Consider one example database:

item | time | value 
-----+------+-------
 1   | 134  |   3
 2   | 304  |   1
 3   | 366  |   2
 4   | 388  |   2
 5   | 799  |   6
 6   | 111  |   7

I need to profile the sum of #values over certain #time interval. Suppose the time interval is 100, the result should be:

time_interval | sumvalue
--------------+----------
      1       |   10      -- the time interval from 100 to 199
      3       |    5      -- the time interval from 300 to 399
      7       |    6      -- the time interval from 700 to 799

I could not find a better way to do it from the SQL text book than to do it using shell and python.

So my SO friends, any suggestion?

Thanks!


You should be able to do it in mysql with a pretty simple query:

SELECT time DIV 100, SUM(value) FROM yourtable
   GROUP BY time DIV 100

The query takes advantage of the fact that integer division by 100 will give you the interval groupings you have described (eg. 111 DIV 100 = 1 and 134 DIV 100 = 1)


Question is not clear to me.

  • There is a database and you want to process data from there, and you are asking to use or not to use SQL? Answer:Yes, SQL is an interface to many databases, it is quite standart for major databases with minor changes. Use it.

  • If you cannot decide to use or not to use a database for storing and processing some values, then data type, amount of data and relations in data is important. If you want to handle large amount of data and there is relation between datasets, then you may want to use relational database systems such as MySql. The problem you told is a very simple problem for RMDBS. Let me give an example:

select sum(value) from items where time>=100 and time<=200

But if dataset is small you can easily handle it with file I/O.

  • If you will use Python, you may want to use Sqlite as database, it is very lightweight, simple, easy to use and widely used database. You can use SQL with Sqlite too.

If you can give clearer details, we can help more.


Yes, a SQL-based databased like MySQL will probably be a fine choice for your project. You may also want to look at SQLite if you don't want to have to set up a server.

A good introductory text on SQL would be helpful for you. I suggest SQL For Dummies by Allen Taylor.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号