sql

learning to make sense of the syntax of syntax

i want to use alter table but the syntax posted here: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html is very confusing i do not understand what the [ ] mean or the { } mean or the pipes is there some kind of tutorial that can help me understand this? ...

SQL - get the article title with the last comment date

I have articles table, article_comments. I want to get the value: last_modified_all of article. I need this value to be the great date of this two: the last_modified field of articles table. the last comment of article_comments last_modified column. Can someone help me? the structure is very simple, and you can guess is without pro...

SQL Timstamp Function

Is there any difference between these two queries? select * from tbl where ts < '9999-12-31-24.00.00.000000'; and select * from tbl where ts < timestamp('9999-12-31-24.00.00.000000'); When is the timestamp function required? Is there a difference in performance? ...

Resort (reorder) table data

What is the fastest way to reorder (resort) a table so that the physical representation most closely matches the logical, using PostgreSQL? ...

PreparedStatement.setString() method without quotes

I'm trying to use a PreparedStatement with code similar to this: SELECT * FROM ? WHERE name = ? Obviously, what happens when I use setString() to set the table and name field is this: SELECT * FROM 'my_table' WHERE name = 'whatever' and the query doesn't work. Is there a way to set the String without quotes so the line looks like t...

Getting deadlocks in MySQL

We're very frustratingly getting deadlocks in MySQL. It isn't because of exceeding a lock timeout as the deadlocks happen instantly when they do happen. Here's the SQL code that is executing on 2 separate threads (with 2 separate connections from the connection pool) that produces a deadlock: UPDATE Sequences SET Counter = LAST_INSERT...

how to add column in SQL Query that incl. LEFT OUTER JOIN

I have this Query: SELECT p.ProductName, dt.MaxTimeStamp, p.Responsible FROM Product p LEFT JOIN (SELECT ProductID, MAX(TimeStamp) AS MaxTimeStamp FROM StateLog WHERE State = 0 GROUP BY ProductID, State) dt ON p.ProductID = dt.ProductID ORDER BY p.ProductName; It works ...

Possible to exclude or reorder a column from `*` ?

Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? Is it possible to exclude a column from a select * from table statement with SQL Server? I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do th...

sql - get the latest date of two columns

table1 - date1 datetime not null - date2 nvarchar null I want to get the latest date of this two. select date1, date2, (CASE WHEN date1 > CAST(date2 as DateTime) THEN date1 ELSE date2 END) as DateTime) as LatestDate from table1 please note that date2 can be null. in this case, date1 win. ...

Rails/SQl query help: Find all by created_at in past 7 days per each day?

I'm unable to get SQL and Rails to play properly when trying to find Categories that are created each day, the past 7 days. So basically I want to find each Category sorted by the day they were created for the past 7 days. I found this on stackoverflow, but it isn't finding a Category that I just created: Category.all(:conditions => [...

Categorize data without consolidating?

I have a table with about 1000 records and 2000 columns. What I want to do is categorize each row such that all records with equal column values for all columns except 'ID' are given a category ID. My final answer would look like: ID A B C ..... Category ID 1 1 0 3 1 2 2 1 3 2 3 1 0 3 1 4 2...

retrieve columns from sqlite3

I have two tables in sqlite: CREATE TABLE fruit ('fid' integer, 'name' text); CREATE TABLE basket ('fid1' integer, 'fid2' integer, 'c1' integer, 'c2' integer); basket is supposed to have count c1 of fruit fid1 and c2 of fruit fid2 I created a view fruitbasket; create view fruitbasket as select * from basket inner join fruit a on a.f...

Select random line in SQL database

Hi, I would like to select a random line in my database. I saw this solution on a website: SELECT column FROM table ORDER BY RAND() LIMIT 1 This SQL query run but someone said me that it was a non performant query. Is there another solution ? Thx ...

SQL Select data by this week

Hi how do I get the data by current week? Select * from Transaction where transactionDate .... ...

Selecting data effectively sql

Hi, I have a very large table with over 1000 records and 200 columns. When I try to retreive records matching some criteria in the WHERE clause using SELECT statement it takes a lot of time. But most of the time I just want to select a single record that matches the criteria in the WHERE clause rather than all the records. I guess the...

Inserting data into multiple tables through an sql view

Is there any way to insert data into multiple tables through a view in mysql? ...

Database design advice needed.

Hi all, I'm a lone developer for a telecoms company, and am after some database design advice from anyone with a bit of time to answer. I am inserting into one table ~2 million rows each day, these tables then get archived and compressed on a monthly basis. Each monthly table contains ~15,000,000 rows. Although this is increasing month...

Looking for a tool that can return SPs doing a DML on a table in SQL Server 2008

I am working on a big application on SQL Server 2008 with 60 000 stored procedures and over 10 000 tables. Is there a tool through which I can do impact analysis to find out all SPs doing a DML on a table? ...

SQL Substring and Last index of

Hi, i've got stuck with substring. On input i've got a string that looks like Sometext (123456). Those digits at the end are random. I need to get only text from that string. ...

AddObject necessary in Entity Framework?

I'm using Entity Framework (1st time) on a SQL 2005 database for a data migration and found this very odd behavior... Up till now I never had to call the AddObject method to persist new records. SaveChanges always did the trick, so I figured the entity constructor always hooked the new entity to the data context. Now I added migration ...