sql

Check constraint on table lookup

Hi, I have a table, department , with several bit fields to indicate department types One is Warehouse (when true, indicate the department is warehouse) And I have another table, ManagersForWarehouses with following structure: ID autoinc WarehouseID int (foreign key reference DepartmentID from departments) ManagerID int (foreign key r...

sum in query / subquery

Table has the following info: date |vendor|sales|percent| -------------------------- 2009-03-03| 10 |13.50| 1.30 | 2009-03-10| 10 |42.50| 4.25 | 2009-03-03| 21 |23.50| 2.30 | 2009-03-10| 21 |32.50| 3.25 | 2009-03-03| 18 |53.50| 5.30 | 2009-03-10| 18 |44.50| 4.45 | I want it to sort into separate tables depending on d...

Approximating Page Views Per Tag (or Tag Group) Per Month with Limited Data?

Using the Stack Overflow public data dump, I've created three simple tables: Questions (Question_Id, View_Count, Creation_Date) Tags (Tag_Name) QuestionTags (Question_Id, Tag_Name) The Questions table has hundreds of thousands of rows with Creation_Date spanning from a year ago to today. Looking over the data, there are two notable t...

Help with SQL Grouping

A partial fragment of my output looks as follows: CNEP P000000025 1 CNEP P000000029 1 NONMAT P000000029 1 CNEP P000000030 1 CWHCNP P000000030 1 MSN P000000030 1 Each row represents a term that a student is in a particular curriculum. Right now I am grouping the information to make sure that each UserID correlates to a partcul...

How to increment DateTime field in SQL Server 2005 by a month?

I have a DateTime field in SQL Server for some products expiration (ExpirationDate). I need to increment all the items manually, and set their expiration a month later than the date stored in the field currently. How can I do that? ...

SQL Subselect in an Update

Oh great Stackoverflow, I beseech thee... I need to do the following: CREATE UNIQUE INDEX UserName ON Emp(UserName) -- Sheesh! UPDATE Emp SET UserName = Left(FirstName,1)+LastName WHERE NOT exists an employee with that UserName already. ...

Incorrect syntax near 'E'.

How can I correct the following so that I don't receive a syntax error in Microsoft SQL Server 2005? UPDATE Emp E SET UserName = Left(FirstName,1)+LastName WHERE EmpID=1 AND NOT EXISTS( SELECT * FROM Emp WHERE UserName=Left(E.FirstName,1)+E.LastName ) ...

SQL Server 2005 Pivot Table question

I have an Entity-Value set of tables, and would like to pivot the results. Here's the effect that I'm looking for, except you see that the SELECT stmt using Common Table Expressions isn't working exactly right yet. My question is: Am I on the right track, or is there some sort of easy pivot command? USE TempDB Declare @Person TABLE( Per...

SQL Server 2005 Cursors

what are some of the cases that will cause a SQL Server 2005 cursor to deallocate prematurely? ...

Database and ERP

I am trying to master a new ERP application. Basically I have good functional knowledge. And I was advised to learn SQL .I do not know where to start. I have two books .Below are the urls which has the TOC of each book. Which book should I read first? The idea is to get a good understanding of the relationship between ERP and database a...

Find GUID in database

I am currently working on a project where some (significant) data corruption has occurred. Specifically the database is using GUIDs as primary keys on most tables, however it is not enforcing data integrity between those tables (due to a long-winded "this was bought from another company and integrated with our stuff" discussion). There...

SQL Server 2005 T-SQL remap columns

I have an application that is ready to go live, once we take data from a MS Access DB and import it into SQL Server 2005. I have used the Migration - Access tool to get the Access db into SQL Server, but now I need to take the data from that table and put it into the tables that our app is going to use. Is there a T-Sql way to Insert m...

Database Design

I am making a webapp right now and I am trying to get my head around the database design. I have a user model(username (which is primary key), password, email, website) I have a entry model(id, title, content, comments, commentCount) A user can only comment on an entry once. What is the best and most efficient way to go about doing thi...

SQL selecting rows where one column's value is common across another criteria column

I have a cross reference table that looks like this: id document_id subject_id 1 8 21 2 5 17 3 5 76 4 7 88 5 9 17 6 9 76 7 2 76 It matches documents to subjects. Documents can be members of more than one subject. I want to return rows from thi...

Get SQL statement after parameters added

Is there a way (in MySQL) to get the pure SQL statement after parameters have been added? I'm having issues with one of my statements once I start using parameters, and I want to see what's being executed. Of course it has to do with dates. Using MySQL .NET Connector. I have access to MySQL server, and I use SQLYog to administrate. Tha...

Best way to count this Data...

In short I have 2 tables: USERS: ------------------------ UserID | Name ------------------------ 0 a 1 b 2 c CALLS: ------------------------ ToUser | Result ------------------------ 0 ANSWERED 1 ENGAGED 1 ANSWERED 0 ANSWERED ect ect (i use a numerical referance for result in reality) I have over ...

How to form the connection string for a SQL Server express instance?

I don't know how to form the connection string. Lets say the server is server.com, with a SQL Instance called MSSQL.1 Looking at a previous example, it looks like the data source would be server.com\MSSQL.1 I installed SQL Express using all the defaults on Windows Server 2003. Any help is appreciated. Thanks Kevin ...

MySQL - Change date string to date type in place?

(Hi all. I'm new-ish to SQL and MySQL in particular. I know some PHP.) I have a table which contains a "date_string" column. For legacy reasons, this is a text field, containing the date in d/m/YY format. e.g. "22/11/09" for 22nd November 2009. Question: how could I convert all fields in this column to a standard MySQL date format (YY...

PHP time ahead of MySQL time

I run PHP and MySQL on the same machine, but both report different times. I set the timezones by the following methods: PHP: date_default_timezone_set('Asia/Kuala_Lumpur'); MySQL: SET time_zone = "Asia/Kuala_Lumpur"; The difference between two times is always exactly 24 seconds. Any idea why this is happening? I thought the times...

SQL Express 2008 Not committing LINQ to SQL writes in sequence?

Hi, I've an event-driven app that has recently started using SQL Express 2008 (instead of Std version) in dev and test to store the current state of a state machine. After moving to SQL Express, we started seeing a discrepancy between the order in which we write to the database (using LINQ to SQL) and the order in which SQL Express Prof...