Merge queries from the same table N times

I have a table of word, a lookup table of where those words are found in documents, and the number of times that word appears in that document. So there might be a record that says Alpha exists 5 times in document X, while Beta exists 3 times in docu

Mistyped dates in SQL

I was wondering if there is a way to detect mistyped dates in SQL in a general sense. For example: Order1 - 2014 Order2 - 2104 Order3 - 2041 I am guessing a form of case statement using wildcards would do the trick. But I am kind of a beginner in tha

SQL Inner Join - Do not return all records

I am trying to get results from two tables using a inner join, this works. However there is a third join, which is the original table, so I used a alias as in table 3 (T3) But some results are not being returned, when the T3 join is zero, it doesn't

What type of index should I create?

Table has 6 columns. column1 is unique, so I created the primary key on column1 and created non-clustered index for all columns: create primary key for column1 create index for col2, col3, col4, col5, col6, col1 So I have any column in where clause w

Query to find projects without a leader

I'm doing a java application with a Postgres database and the following schema: The entity employee, rol, project has some information inside,and the entity participants is empty. I want to show in my application a table of all projects with no leade

How to prevent code duplication to this sql query

Here the sql query The first query is my main select query. It will select millions of records However at the later queries i have to use first selected UserIds. right now my only solution is duplicating this query However i wonder would there be any

SQL WHERE corresponding to any value in a column

I need a table A with 4 columns for order numbers: Order1, Order2, Order3, Order4 (I know, it's horrible, but that's a given). And I have to find the records in table B, where the match is that any value in any order column in table A could be in any

Complete Supervisor Hierarchy for an Oracle SQL Employee

How can we print entire employee hierarchy for each employee using oracle SQL hierarchial query. Sample Employee Table>> EmpID, ENAME, MGR 1, A, 1 2, B, 1 3, C, 2 4, C, 2 5, C, 3 Sample Output>> EmpID, ENAME, Hierarchy 1, A, - 2, B, /1 3, C, /

Gridview Field calculated in the line

I am using Visual Studio 2010 and Visual Basic linked to a SQL database. I have a Gridview in which I want to have a calculated field, let's call it NoHoursOff, based on the fields in that row - BeginTimeOff and EndTimeOff. After doing some research,

Oracle: first date of stay (two columns)

i have three columns id_user, date_stay_start, date_stay_stop and I need to return complete date of the first stay (the oldest). I am currently have something like this: SELECT users.id_user, Min(orders.date_stay_start), Min(orders.date_stay_stop) FR

Oracle SQL: get every second value of a string

I am searching for a way to get every 2nd value of a string via SQL. My string looks like this: 12:115:22:98 and I would like to get every 2nd value out of it, in this case 115 and 98. Tried around with regexp_substr, but best I could do was to get e

Remove duplicates from the table with a huge amount of rows

I have a table with 19 million records. I want to delete duplicates, but the query I am using is taking a very long while and eventually connection is timing out. This is the query I am using: DELETE FROM [TableName] WHERE id NOT IN (SELECT MAX(id) F

Conditional join based on search

Apologies if a similar problem is posted earlier, I couldn't find the same. Problem: I need to join two tables based a conditional look up in the second table. Tables: Below are the two tables which have a subset of the total fields. +---------------

How to display the last records first in MySQL?

I have created a database in MySQL. When I try to get all the records for a table, the last-added records are displayed last. How can I efficiently display the latest records first, without using a datetime column? Adding a datetime column would mean

Update using the subquery in the plsql block

If comm is null, I need to update the comm value to salary, which is 1100, but it is not getting updated. My data is: sal comm 9000 800 2975 800 3000 800 1100 3000 800 My code is: declare cursor c3 is select sal,comm,ename from emp where deptno=20 fo

How can I check Zero rows from SQL SqlDataSource in C #?

Possible Duplicate Here Does anybody know a good way of finding out if a SqlDataSource will return Zero Rows? This is my call to the SQL Database in my C# code behind: string SQL_PROD_DOCS = @"SELECT TOP 3 Documents.ProductID, Documents.ProjectID, Do

Android: How to load a db file on the server?

I am developing an application where users can upload their own recipes. So, I want to create and put a sqlite .db file on my server and modify the database when users upload their recipes. Is this even possible? Instead, it seems that there is a way

Using ORDER BY while retaining the use of the index

I'd like to retrieve some rows utilizing my index on Columns A and B. I was told the only way to ensure my index is being used to retrieve the rows is to use an ORDER by clause, for example: A B offset 1 5 1 1 4 2 2 5 3 2 4 4 SELECT A,B FROM TableX W

Invalid object name error in function in SQL

I have following function defined alter FUNCTION [dbo].[GetXMLValues](@business_id int, @id varchar(30)) RETURNS varchar(30) AS BEGIN declare @xmlValue varchar(30) set @xmlValue = (SELECT top 1000 T.Content.value('(/XmlDataPairDocument/dataitem[@id=s

LINQ to Entities materialization error

I have Entity Framework 4 model over SQLite databese. Part of the model looks like this All Id fields have type Guid (uniqueidentifier in SQLite table DDL). But when I run following LINQ query I get exception. var query = AbonentPfrs.Select(a => new

SQL - Group by column, then sort by results 'count'

What I mean by Title is this: Say I have 100 row. One column is review_id. I then want to Group By that review_id. Then I'd like to count each group of rows, and then Sort By it. Sample results: 20 rows of review_id 132, 17 rows of review_id 434, 8 r

Divide a result of a query by the result of another

I can run SELECT killedby, COUNT(killedby) killcount FROM games_playerinfo WHERE killedby != '' GROUP BY killedby ond get killedby killcount Player1 1 Player2 1 and I can run SELECT ign, COUNT(ign) numberofgames FROM games_playerinfo GROUP by ign and

Regular expression of 'and'

what is the regular expression to not allow enter ' and '' ? because when the user enter the text with ' Example : i'm Mr Right -> it cause a error on SQL 2008. i know, that i have 2 choice: not allow user to enter ' and "" (with regex) i wil

Survey information on the SQL Table column

Is there a way to get for a particular table in an SQL DataBase, the information containing the type and the size of any one column. For example, having table T, with columns C1 being varchar(50) and C2 being Int and C3 being Bits(6). Is there a way

SQL to get separate accounts of two groups on multiple tables?

We have a small randomized study that we're trying to report numbers for. In this database, we have eight tables that contain different randomization groups (treatment vs. control) for each table that are designed like so: +--------+-------+---------

several mysql_query queries in a document

I have a PHP document opening a connection to a database. Everything works fine - but as soon as I add one additional mysql_query request to the document, the mySQL server responds with a 500 internal server error. Any idea why? Is there a reason tha

Odd "UNION" Behavior in Oracle SQL Query

Here's my query: SELECT my_view.* FROM my_view WHERE my_view.trial in (select 2 as trial_id from dual union select 3 from dual union select 4 from dual) and my_view.location like ('123-%') When I execute this query it returns results which do not con

Use table metadata for a select statement in SQL Server?

I have a large database and would like to select table names that have a certain column name. I have done something like this in MySQL, but can't find any info on SQL Server. I want to do something like: select [table] from [db] where table [has colu