sql

How to select a row based on a max count? (using standard sql)

I have a table: ComputerID, UserID, LoginDate I need a select statement that returns: ComputerID, UserID, LoginCount For all computers, but for each computer, showing just the one user that logged on most often to that computer. (If there is a tie, I guess I would want to just arbitrarily pick one of the users....so this would seem t...

PHP - Show results, conditional based on link

Looking for some help with the code below. I think there may be an issue with the way I've organized the php call and function calls. I'm a beginner and am trying to allow users to display results from different tables, depending on what link they click. I get this error: Parse error: syntax error, unexpected '{' in /home/content/c/e...

help me in this query

i am trying to develop a site where users can become friends of each other and can see their posts after the date the became friends........i have already posted this question about 5-7 days but could not find any solution........!! so... i have two tables.......... posts and friends and my query is $sql = mysql_query(" SELECT * FROM p...

database sql join question

i have 2 tables called Location (id, name) Person (id, name, location_id) A person has a location Id which joins these tables . . i would like a SQL query that gives me each location and the count of person table for that id. i could do something like this and then add up the records in code but i want to find out a way that i only...

String padding in tsql

I print out a bunch of DDL statements that are dynamically created and want to align the output in a specific way. PRINT 'ALTER TABLE ' + @TableName + ' WITH NOCHECK ADD CONSTRAINT CK_' + @TableName + '_' + @ColumnName + '_MinimumLength CHECK (LEN(' + @ColumnName + ') > 0)' Output: ALTER TABLE SignType ADD CONSTRAINT CK_SignType_Desc...

Select distinct + select top to merge multiple rows

I'm trying to select rows from a table, one row per email address, and return one firstname from the top row in the email list. The query, though, returns multiple email addresses. What am I doing wrong? SELECT DISTINCT email, (SELECT TOP 1 firstname FROM onsite_clients_archive oc WHERE oc.client_id=oca.client_id ...

How do I create a table alias in MySQL

I am migrating an MS Access application (which has linked tables to a MSSQL Server) to MySQL. As a means to overcome some MSAccess table naming problems, I am seeking a solution to add a MySQL table alias that will point to an existing table in the MySQL database. Ideally I would like to create the alias 'dbo_customers' in mysql that ...

Oracle shared memory error

The following query generates the error below in 10g: select DBMS_METADATA.GET_DDL('TABLE','TEST_TABLE','TEST') from dual; Got: ORA-04031: unable to allocate 128 bytes of shared memory ("shared pool","SELECT /*+rule*/ SYS_XMLGEN(...","sql area","qeeOpt: qeesCreateOpt") ORA-06512: at "SYS.DBMS_METADATA", line 1546 ORA-06512: at "SYS....

Characters found after end of SQL statement VBA code Access 2007

I'm writing a vba code in Excel to access an Access database. I'm using this provider "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & DBFile & ";" and here is my SQL string SQlSrc = "SELECT YEAR FROM Data ORDER BY YEAR ASC" SQlSrc = SQlSrc & ";SELECT STN FROM STN_List WHERE include = TRUE" When I open each recordset individua...

Business rules in the repository?

Let's assume that an application has all necessary business rules in the model / presentation layer and that they work fine. My question is one of whether or not redundant business rules (i.e. a span of two dates cannot overlap any other existing spans) should be used in a repository like SQL Server. Is it necessary/beneficial to add a...

Comparing SQL Table to itself (Self-join)

I'm trying to find duplicate rows based on mixed columns. This is an example of what I have: CREATE TABLE Test ( id INT PRIMARY KEY, test1 varchar(124), test2 varchar(124) ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 1, 'A', 'B' ) INSERT INTO TEST ( id, test1, test2 ) VALUES ( 2, 'B', 'C' ) Now if I run this query: SELE...

Unbuffered queries using Postgres+PHP?

Is there a way to execute unbuffered queries against PostgresSQL from PHP, or at least limit the size of the local buffer that a driver uses? ...

RAISERROR and returning error message to the calling application

Hi Assuming Asp.Net app calls procedure dbo.ApprovePost and @@ERROR contains a value greater than 0, then the following code will be executed: CREATE PROCEDURE dbo.ApprovePost … IF @@ERROR > 0 BEGIN RAISERROR(‘Approval of post failed’, 16, 1) ROLLBACK TRANSACTION ApprovePost RETURN 99 END COMMIT TRANSACT...

How should I setup my database schema for a messaging system complete with attachments?

I'm working on creating a messaging system as a pet project, that will include the ability to have file attachments. This will be used on a website of mine for the internal messaging system. One of the features of this system is I want to maintain the MD5 checksum of every file that is uploaded so if duplicate files are uploaded, the t...

MySql and inline SELECTs

I have a query that looks like this: select id , int1 , int2 , (select count(*) from big_table_with_millions_of_rows where id between t.int1 and t.int2) from myTable t where .... This select returns exactly one row. The id used in the inline select is an indexed column (primary key). If I replace t.int1 and t.int2 with the values...

Getting a distinct value across 2 union sql server tables...

I know this is a silly question, but Im a newb I'm trying to get all distinct values across 2 tables using a union... The idea is to get a count of all unique values in the columnA column without repeats so that I can get a summation of all columns that contain a unique columnA... sounds simple eh? THis is what I tried (sql server expr...

How can i use pivot ?

I am trying to use pivot on a table to rotate(Transpose) the table but i m not getting how to do that. I want to know how it actually works. i found many egs but i was not able to understant how those work. Check this, e.g. How tis is working, i just want to transpose my rows into cols and cols into rows Say i have 31 cols for day1 da...

Dump the body of a function or procedure in sqlplus

How can I dump out the body of a function or a procedure when using sqlplus to connect to an oracle database? ...

SQL query to return top X sequential descending rows, by group, for a particular value..

I need a query to return, by group, a true or false if the most recent x number of sequential rows, in descending date order, have a column with a false value where x can be different for each group. For example, a Configuration table would have the number of records that have to match sequentially by companyId and serviceId: CompanyI...

Combining several query results into one table, how is the results order determined?

I am retuning table results for different queries but each table will be in the same format and will all be in one final table. If I want the results for query 1 to be listed first and query2 second etc, what is the easiest way to do it? Does UNION append the table or are is the combination random? ...