sql

Efficient Ad-hoc SQL OLAP Structure

Over the years I have read a lot of people's opinions on how to get better performance out of their SQL (Microsoft SQL Server, just so we are all on the same page...) queries. However, they all seem to be tightly tied to either a high-performance OLTP setup or a data warehouse OLAP setup (cubes-galore...). However, my situation today is ...

Is SQL equal test fast for large numbers?

I run the very same query on two almost identical database. The only difference is that the first database has ID entries from 1 to 9000 for 2 tables while the other is in the 458231044 and 103511044 range for the 2 same tables. (for the same 9000 entries) The query compares ID and UNIX time numerous times. Running it on first database...

Order by relevance using boolean fulltext search

Hello, I am having problems using MySQL's fulltext search and returning the results in order by relevance. I have to use boolean full text search, which does not return results in order by relevance. However, I need these results in order of relevance. If I attempt to add an order by clause on the end of the query, the query results to ...

MySQL query aggregating users who perform an activity

I am having trouble writing a query on a mysql table of user activity that will give me a breakdown of how active our users are. The table structure is like so: CREATE TABLE IF NOT EXISTS `ca_activity` ( `id` bigint(20) NOT NULL auto_increment, `user_id` bigint(20) default NULL, `activity_type` varchar(50) collate utf8_unicode_ci...

How to create a view spliting one column to 2 or more using a regular expression?

We have a non normalized table that contains foreign key infomration as free text inside a column. I would like to create a view that will transform and normalize that table. E.g. a column that contains the following text: "REFID:12345, REFID2:67890" I want to create a view that will have REFID1 and REFID2 as 2 separate integer colu...

Trying to join tables and select by one distinct max value

Hi, I am trying to pull multiple columns from 3 joined tables, but I want the result set to contain only one distinct "data entry" per p.id (the pet id which is a foreign key in the data entry table). The issue i have is that there could be two data entries, numbered 1 and 2, belonging to a pet - the query has to just pick the data entry...

Why is LINQ to Entities making a subquery for me?

I'm using .NET 4 and the Entity Framework to construct a simple query. Here's the C# code: return Context.Files.Where(f => f.FileHash != 40) .OrderByDescending(f => f.Created) .Take(5); When I trace the query using ObjectQuery.ToTraceString(), I find the following subquery: SELECT TOP (5) [P...

Groovy, How to do 2 phase commit? Is Sql.withTransaction can manage transaction scope accross multiple databases?

Hi there, Well, I think my question says it all. I need to know if Groovy SQL supports two phase commits. I'm actually programming a Grails Service where I want to define a method which does the following: Get SQL instance for Database 1, Get SQL instance for Databsae 2, Open a transaction some how: Within the transaction call two dif...

Regarding date in sql

Here's my query: select * from test n WHERE lower(process_name) like 'test%' AND ( test_id is NULL OR TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE),-6),'YYYYMM') > TO_CHAR(n.process_date,'YYYYMM') I want check whether date field process_date is greater than 6 months in the query. ...

Block SELECT until results available

I'm trying to write a PHP script for 'long-polling', returning data when new rows are added to a (Postgres) database table. Is there any way to get a SELECT query to return only when it would return results, blocking otherwise? Or should I use another signaling mechanism, outside of the database? ...

SQL Server 2008 - Stored Procedure Gets Stuck When Called From WPF Service Using a SqlDataAdapter

This has me pulling my hair out. We have a workflow, hosted as a WCF service, which makes a call to another WCF service which then calls a stored procedure. Store procedure calls a merge, then iterates through a cursor that calls another sproc. The cursor count is the same as the source count in the merge. If the source count is high...

SQL non-clustered index

I have a table that maps a user's permissions to a given object. So, it is essentially a join table to 3 different tables. (Object, User, and Permission) The values of each row will always be unique for all 3 columns, but not any 2. I need to create a non-clustered index. I want to put the index on the foreign keys to the object and u...

Massive SQL query design ideas

Need to query a database for 12 million rows, process this data and then insert the filtered data into another database. I can't just do a SELECT * from the database for obvious reasons - far too much data would be returned for my program to handle, and also this is a live database (customer order details) and I can't have the database ...

SQL query to get group by and distinct values at the same time

Hello, I'm having trouble trying to define the SQL query for this table: There's a table of patients and their weight readings recorded on visits with the following columns: patient ID weight reading visit ID (one per visit) In other words, if in two records two visit IDs are the same, then two weight readings have been taken on ...

Does table1 UNION ALL table2 guarantee output order table1, table2?

SELECT a FROM b UNION ALL SELECT a FROM c UNION ALL SELECT a FROM d Does UNION ALL guarantee to print out records from tables b, c, d in that order? I.e., no records from c before any from b. This question is not for a specific DBMS. ...

How to limit an SQL query to return at most one of something?

I have a table in my database representing releases of software; the relevant columns of the schema being as follows: ProductID int PlatformID tinyint Date datetime All are not null, and there is also a unique key assigned to the first two columns. I would like to build a query (using LINQ) which returns the latest release ...

How can I give condition on the column names in SQL Server 2008

I have my database with the columns as follows: keyword, part1_d1, part1_d2 ........ part1_d25, part2_d26, ......part2_d34 FYI: d1 through d34 are documents.. How can I give a query to obtain columns with column_name like '%part1%'; as below keyword, part1_d1, part1_d2, ........ part1_d25 I tried the query: select (Select COLUMN...

Count number of NULLs in a row

Is there a way to get a column indicating the number of NULL fields in a row? This would be within a SELECT statement. For Example: Field1 Field2 Num_Null ----------------------- NULL "A" 1 UPDATE: I want this query so I can sort based on how many Affiliates sales there are of a given Book. So having 3 affiliates would b...

[SQL Server] There is already an object named '##Temp' in the database.

I have a stored procedure on SQL Server 2000. It contains: select ... into ##Temp ... ... drop table ##Temp When I run the stored procedure with ADO a second time, it prompts: There is already an object named '##Temp' in the database. Could anyone kindly tell me what's wrong? ...

How to export data from Excel spreadsheet to Sql Server 2008 table

I want to import data from an Excel file - assume Excel 2003 / .xls - to Sql Server 2008. Have tried adding a linked server to the JET OLE DB Access driver, and of course it fails on the 64-bit machine. But when I try to drop the linked server while experimenting, there's another error saying that the linked server already/still exists!...