sql

postgres - partial column in SELECT/GROUP BY - column must appear in the GROUP BY clause or be used in an aggregate function

Both the following two statements produce an error in Postgres: SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by date; SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by substring(start_time,1,8); The error is: column "cdrs.start_time" must appear in the GROUP BY clause or ...

PHP/SQL Cycle items every x minutes.

Hi all! I have a friend who runs an online auction website. He currently has a featured items section on the homepage that he wants to have cycle an item every X amount of minute. The site runs off a MySQL database which I haven't actually seen yet. The current code that he is using is a big, long messy Javascript code that is causing a...

How to design a circular reference to a single database table with an added relationship?

I'm not sure how best to phrase the question, but essentially I have a table of contacts, and instead of doing the typical -- a contact has a reference to a table with spouse information, and a table with children, I want each of those people to be a contact, but then define a relationship between those contacts (brother, sister, child, ...

SQL NOT IN Query

Can anyone help me with this MySQL query? SELECT p.ProductID, p.StoreID, p.DiscountPercentage FROM Products p WHERE p.IsSpecial = 1 AND p.SpecialDate >= date_sub(now(),interval 15 minute) AND p.DiscountPercentage >= ?DiscountPercentage AND p.ProductID NOT IN (SELECT lf.LiveFeedID ...

How To Reduce Number of Rows Scanned by MySQL

I have a query that pulls 5 records from a table of ~10,000. The order clause isn't covered by an index, but the where clause is. The query scans about 7,700 rows to pull these 5 results, and that seems like a bit much. I understand, though, that the complexity of the ordering criteria complicates matters. How, if at all, can i reduce ...

MySQL optimisation by sorting the rows

Hello everybody! I am not an expert of MySQL, and I have search a lot about the following problem without finding the solution. So, I'm using a MySQL table with this structure: CREATE TABLE photos ( file varchar(30) NOT NULL default "", place tinytext NOT NULL default "", description tinytext NOT NULL default "", type char(1) ...

SQL Compute the price of cart in one request

Hello, Typically, I have a really simple cart with some items in it, with a quantity. I wish to do something like this : SELECT SUM(price * quantity) as total FROM products WHERE product_id IN (1,2,3,4); But how can I bind the quantity with the product_id since quantity is not in the database ? Is there any other way to do this wit...

Culture-conversion in C# with DateTime

I'm having trouble getting my ASPX-App to work on the productive server. As my local environment is german but the server is english and my whole code didn't provide culture-support, I'm currently implementing that throughout the script. As you might think, as I'm writing this in here, it doesn't quite work the way I want it to. The Ap...

SSIS how can i insert a blank date to database with visual basic

Hi all, how can i insert a blank date to sql database in ssis working with a visual basic script component? I have to transfer date from excel to database and i would like to insert a blank or null value in the database if in the excel is empty thanks ...

dd/MM/yyyy in SQL Server

I have a DATETIME column "EntryDate" and I am storing values as dd/MM/yyyy format. But when I fetch records through SQL I don't get the expected result. For ex: I have 90 records from date 23/10/2009 to 23/11/2009. I get only 5-10 records. Why is this ? Here is my query : SET DATEFORMAT DMY; SELECT * FROM salesorderlist so LEFT JOIN...

Getting only Month and Year from SQL DATE

I need to access only Month.Year from Date field in SQL Server. Thanks in advance. ...

TV Show Suggestion Algorithm and SQL

There are users that can be referenced by an id as seen in the table below: users: user_id I have attempted at a solution for keeping track of show views tvshow_hits: user_id|show_id There is also a table with details in it: tvshows: show_id|name But that is not required I'm open to change. I feel it isn't necessary to keep trac...

What is the difference between these two queries?

I am writing my join query by the following way UPDATE UPLOAD_TEMP SET UPLOAD_TEMP.Borr_Add_Req = t2.YesNoResponse, FROM UPLOAD_TEMP t1 INNER JOIN GB_RequiredFields t2 ON t1.State = t2.StateCode AND t1.County_Id = t2.CountyId AND t1.Grou...

Duplicates without using While or Cursor in T-SQL

ID Name 1 A 1 B 1 C 2 X 2 Y 3 P 3 Q 3 R These are the columns in a table. I want to get output like ID Company 1 A,B,C 2 X, Y 3 P,Q,R Restriction is that I cannot use WHILE or CURSOR. Please write a query for the same. ...

Nested join on same table (tree structure)

My date is organized in tree structure. The following applies (Oracle SQL syntax): CREATE TABLE TREE ( NAME VARCHAR2(20), ID NUMBER(10, 0), PARENT NUMBER(10, 0) ) ; INSERT INTO "TREE" (NAME, ID) VALUES ('a', '1'); INSERT INTO "TREE" (NAME, ID, PARENT) VALUES ('a.1', '2', '1'); INSERT INTO "TREE" (NAME, ID, PARENT) VALUES ('a.2',...

Copy data from Access to SQL

I am in the process of migrating an existing Access database to a SQL database with a web front-end. I have successfully copied the database to SQL using the SQL Server Migration tool and am working on developing the application. For reasons I won't go into here we cannot just link the Access front-end to the SQL DB and use the SQL DB as...

SQL Trigger 1 row at a time

I'm creating an update trigger that goes like this (SQL Server 2005): Is the state column of the row is 23 or 25 don't update it. Else update it. It's very simple. I'm trying OldState = (Select State from Deleted) If OldState in (25,23) Update it --how to do it easily? else dont do nothing for this row The problem is that the ...

Copy column of table from backup database to main database with WHERE clauses

I make a mistake and I have done a wrong update on my main database (hospital) (blood types have been unknown for most of patients in tblPatientFile table) but I have a daily backup database (hospitalRapor) and data are true for patients before today( and it is about 400000 records) can I copy my old data from like this [hospital].[db...

How to do a firebird SP, SELECT * FROM "TABLE"?

I'm migrating from SQL Server to Firebird. In SQL Server CREATE PROCEDURE Departments_GetAll AS SELECT * FROM Departments I try in Firebird CREATE PROCEDURE DEPARTMENTS_DELETEALL AS BEGIN SELECT * FROM "Departments"; END^ SET TERM ; ^ But that doesn't work. It returns an error "SQL Code -104" ...

Need a Totals column in MDX

I have the following query that gives me an extract (I am putting this into a CSV from my cube). I want to show the Total of all NRx in a column as the first column NRx and the rest of the NRx columns should come up as they do now. I tried to add a SUM (Measures.NRx) to this, but it did not work How do I get a total NRx column? SELECT...