sql

Need to Get Data from a table which contains unique id with different category in mysql

Hello all, My Table Structure is: artist_id category 1 Song 1 Song 1 Song 1 Video 1 Video Now, my output must be id category total 1 Song 3 1 Video 2 How this can done by using MYSQL. ...

Should I ignore an exception on database insert?

I have two tables, A and B and a simple table, X, that maintains a relationship between the two. X contains AID and BID as its primary key. I'm using Linq-to-Sql to insert a relationship like: public void InsertRelationship(int y, int z) { DataContext.X.InsertOnSubmit(new x { AID = y; BID = z }); } The problem is that two calls to ...

SQL2000 to SQL2005. Query now a lot slower

Hi This query used to take 3secs in SQL2000, now it takes about 70secs. Both databases give the same results. The 2005 database is not running in compatibility mode. Currently we're rebuilding the query to run in SQL2005.. by a process of elimination and understanding the logic. However - can anyone see anything obvious that we've m...

Creating Breadcrumbs structure from SQL Query

Hi, I am using SQL Query and below are the tables. Organization OrgID Name RAOGID RAOID SubGroupID 1 Org RAOG 1 NULL NULL 2 Org RAO NULL 1 NULL 3 Org Sub Group NULL NULL 1 RAOG RAOGID AccredID 1 2 RAO RAOID RAOGID 1 1 Sub Group SubG...

Iterating over rows in a database and removing them

I'd just like to check, is this a good way of iterating over rows in a database and deleting them when processed? I first had Grab all Process all Delete all But figured that might leave to problems if new rows were added (from outside) while the "Process all" step is running. // the limited for loop is to prevent an infinite loop /...

Multiple Counts in CakePHP

I have a PHP application using CakePHP. I need to present a table showing the count of particular events for each day of the month. Each event is record in the DB with a name and a Data. What is the best way to create a table for this, do I really need to do 31 SQL calls to count the events for each day, or take out the data for the whol...

SQL question about counting...

I want to make a query so that I can grab only Locations that have at least 50 Places. I have a table of Locations: Id, City, Country 1, Austin, USA 2, Paris, France And a table of Places connected to Locations by Location_id Id, Name, Details, Location_id 1, The Zoo, blah, 2 2, Big Park, blah, 2 I can join them like so: SELECT p...

SQL LEFT outer join with only some rows from the right?

Hi noble SOuls, I have two tables TABLE_A and TABLE_B having the joined column as the employee number "EMPNO". I want to do a normal left outer join. However, table B has certain records that are soft-deleted (status='D'), I want these to be included. Just to clarify, TABLE_B could have active records(status= null/a/anything) as well ...

SQL Removing duplicates one row at a time

I have a table where I save all row-changes that have ever occurred. The problem is that in the beginning of the application there was a bug that made a bunch of copies of every row. The table looks something like this: copies |ID |CID |DATA | 1 | 1 | DA | 2 | 2 | DO | 2 | 3 | DO (copy of CID 2) | 1 | 4 | DA (copy of CID 1) | 2...

How can I optimize my database queries using my actual django model ?

I can't post image because I'm new so here's a link of what I want. So, I have the model on the left and I want the view on the right. As of now, I'm looping over every thread I'm interested in. Template code: {% for thread in threadlist %} {% for post in thread.postlist %} ... Model code: class Thread (models.Model): ... def po...

is there such list control in c++

I am using MFC. I need a control just like listControl, it has such functions: MyListControl mylistControl = new MyListControl(); mylistControl.setDataSource(...); mylistControl.setSQLStatement("select a, b, c, d from table where a > 3"); and system will have a listControl which is populated with the data from database, and generate t...

delete all but minimal values, based on two columns in SQL Server table

how to write a statement to accomplish the folowing? lets say a table has 2 columns (both are nvarchar) with the following data col1 10000_10000_10001_10002_10002_10002 col2 10____20____10____30____40_____50 I'd like to keep only the following data: col1 10000_10001_10002 col2 10____10____30 thus removing the duplicates based on t...

ERD tool that generates MySQL schema

Hi, I'm looking for a tool that will allow me to draw an ERD and will generate the SQL that creates the corresponding MySQL schema. Additional requirements are: Runs on windows Zero cost Bonus points if it runs on Ubuntu too, but this isn't a 'must-have'. Thanks, Don ...

sp_MSforeachtable order by

I am using sp_MSforeachtable to get a rowcount of specific tables in my database. I want these ordered by name. How do I add an ORDER BY clause to sp_MSforeachtable? ...

Awkward SQL query for "friend feed"

I am trying to code a social networking element to our website. We have two tables, one showing users and one showing friendships between those users. Users: userid - PK, int name profilepic Friendships: id - PK int userid friendid datecreated The friendship table is two-way - ie someone can befriend someone without them frien...

Sql recursion without recursion

I have four tables create table entities{ integer id; string name; } create table users{ integer id;//fk to entities string email; } create table groups{ integer id;//fk to entities } create table group_members{ integer group_id; //fk to group integer entity_id;//fk to entity } I want to make a query that returns all groups where a...

Deciding if I should split a table

I am building a multi language site I have a descriptions table that hold the description of each product and a column that indicate the language. So it contain a row for each language description Now my concern is that I have various different types of products in the system and reading the description of any product will have to go t...

Joining tables in LINQ/SQL

I have below a collection of rows and each row consists of productid, unitid, countryid. I need to find the details for each row in the corresponding tables (products, units, country) for product - select product name, updatedby for unitid - select unit name , updatedby for countryid - select countryname, uploadby and returning...

How to preserve the table schema when you write dataset out as XML

The heading pretty much explains what I would like to achieve, but here is an example to elaborate further: Given the following table: CREATE TABLE [CustSchema].[TestTable]( [Desc] [varchar](50) NOT NULL ) ON [PRIMARY] When I load this table into a DataTable, and then spit it out again using DataSet.WriteXml(), I get the followin...

Generating random strings with T-SQL

If you wanted to generate a pseudorandom alphanumeric string using T-SQL, how would you do it? How would you exclude characters like dollar signs, dashes, and slashes from it? ...