sql

Getting first day of the week in MySql using Week No

Hi How do I get the first day of a given week whose week number is available? For example as I write this post we are at WEEK 29.I would like to write a MySQL query that will return Sunday 18 July using this WEEKNO 29 as the only available parameter. ...

How to generate this query in sqlalchemy ?

I want to generate this query in sqlalchemy. The table 'demande' exists in the database. There is a subquery that generates the timesteps with the generate_series function. SELECT timesteps.timestep AS timestep, d.count AS count FROM (SELECT DATE_TRUNC('hour',date_demande) AS timestep, COUNT(id) AS count FRO...

Refactoring SQL

I must connect from my Rails app to a remote database on a Java application. I have a query like this: find_by_sql("select c_templateid, c_templateinfoid, c_startdate, c_enddate, c_active, campaign, shortcode, prefix, c_descriptivename, c_description, c_templatename from (active_services aser join activity a on aser.c_primaryprefixid ...

How to implement logging and error reporting in SQL stored procedures?

Background I'm currently working on a project that heavily utilizes SQL stored procedures. Some tables have as many as few hundred thousand procedure executions every day. Because the project serves critical business function for our client logging and fast error detection is crucial. Question How to implement logging and error report...

How to select entries from this relationship?

I have these four tables: feeds, feed_entries, entries_categorias and categorias. Whith these structures: CREATE TABLE `categorias` ( `id` int(11) NOT NULL auto_increment, `nome` varchar(100) collate utf8_unicode_ci NOT NULL, `slug` varchar(100) collate utf8_unicode_ci NOT NULL, `principal` int(1) NOT NULL default '0', `ordem`...

PostgreSQL Index Usage Analysis

Is there a tool or method to analyze Postgres, and determine what missing indexes should be created, and which unused indexes should be removed? I have a little experience doing this with the "profiler" tool for SQLServer, but I'm not aware of a similar tool included with Postgres. ...

Why do SUBSTRING or LEFT make the query much slower?

I have a blacklist of people that should never be contacted. When I want to see if a person is in this list, I do the following: -- Query 1 SELECT * FROM bldb.dbo.blacklist l WHERE l.matchcode = dbo.fn_matchcode('12345','Sesame Street','1','Eddie','Bert') The query runs very fast, since there is an index on matchcode column, and ...

what is the quickest way to run a query to find where 2 fields are the same

i have a table with id, first, last and i want to run a query that says give me every record where the combination of first and last exists more than once (i am trying to find duplicate records) ...

Remove non-ASCII values from Oracle table

How can you remove non-ASCII values from a table in Oracle with PL/SQL? ...

Error: SubQuery Returned More than One Value

I've got the following piece of SQL Code that is giving me a titular error. WHERE (SELECT Tokens FROM StringSplitter(@DocumentValue, '|', 1)) IN (SELECT Tokens FROM StringSplitter(@sortValue, '|', 1)) Where @DocumentValue and @sortValue are both concatenated strings separated by a delimiter (in this case, a '|'). The...

SQL - One result per user record

I have a web site that collects high scores for a game - the sidebar shows the latest 10 scores (not necessarily the highest, just the latest 10). However, since a user can play multiple games quickly, they can dominate the latest 10 list. How can I write an SQL squery to show the last 10 scores but limit it to one per user? ...

Redgate SQL Tools

I am testing the Redgate belt and the set of tools seems pretty amazing... but before I buy anything... could you recommend any other tools similar to these? Thanks ...

Select rows and Update same rows for locking?

I need to write a procedure that will allow me to select x amount of rows and at the same time update those rows so the calling application will know those records are locked and in use. I have a column in the table named "locked". The next time the procedure is called it will only pull the next x amount of records that do not have the "...

SQL/access question: Do not append that student-term record if the student AND the term already exist!

I have another SQL/access 2007 question that seems really basic but I'm not sure how to Google for it. I have this table STUDENT-TERMS with the following fields: StudentTermID StudentID (links to STUDENT table) TermID (links to TERMS table) TermGPA (this is the nugget of info that needs to be recorded) STUDENT table looks like this...

SQL Server restorig backup on Developement (Or Test) Server and user mapping

I got 3 server. development (my local development machine) Test purpose (Beta site). Production I got Instances in both SQL server 2005 and 2008. So, When I want updated data, i take the latest backup from our production server and restore it on the Test or development Server. I got a script that make the changes (restore the file ...

How do I write a function to compare and rank many sets of boolean (true/false) answers?

I've embarked on a project that is proving considerably more complicated than I'd first imagined. I'm trying to plan a system that is based around boolean (true/false) questions and answers. Users on the system can answer any questions from a large set of boolean (true/false) questions and be presented with a list showing the most simila...

PDO and Microsoft SQL: Must declare the table variable "@P1"

I'm trying to use binding in PDO to select some entries from a Microsoft SQL database. The code I'm using looks like it's similar to what I've found in the documentation. However, when I run it, I get the warning below: Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[42000]: Syntax error or access violation: 1087 [M...

SQL: query help

My table had 3 fields: id and unit. I want to count how many ids have <10, 10-49, 50-100 etc units. The final result should look like: Category | countIds <10 | 1516 10 - 49 | 710 50 - 99 | 632 etc. This is the query that returns each id and how many units it has: select id, count(unit) as numUnits from myTable group b...

What is the best way to implement a substring search in SQL?

We have a simple SQL problem here. In a varchar column, we wanted to search for a string anywhere in the field. What is the best way to implement this for performance? Obviously an index is not going to help here, any other tricks? We are using MySQL and have about 3 million records. We need to execute many of these queries per second ...

Get SUM in GROUP BY with JOIN using MySQL

I have two tables in MySQL 5.1.38. products +----+------------+-------+------------+ | id | name | price | department | +----+------------+-------+------------+ | 1 | Fire Truck | 15.00 | Toys | | 2 | Bike | 75.00 | Toys | | 3 | T-Shirt | 18.00 | Clothes | | 4 | Skirt | 18.00 | Clothes | | 5 | ...