sql

"Pivoting" a Table in SQL (i.e. Cross tabulation / crosstabulation)

I'm working on trying to generate a report from a couple of database tables. The simplified version looks like this Table = Campaign CampaignID Table = Source Source_ID | Campaign_ID Table = Content Content_ID | Campaign_ID | Content_Row_ID | Content_Value The report needs to read like this: CampaignID - SourceID - ContentRow...

SQL Server 2005 Pivot on Unknown Number of Columns

I am working with a set of data that looks something like the following. StudentName | AssignmentName | Grade --------------------------------------- StudentA | Assignment 1 | 100 StudentA | Assignment 2 | 80 StudentA | Total | 180 StudentB | Assignment 1 | 100 StudentB | Assignment 2 | 80 Student...

SharePoint 2007 - SQL Query to find a list of documents in site collection

I need to get a list of all documents in a site collection, which I believe I can do with either the alldocs table or the alluserdata table (MOSS 2007 SP1) but do not see how I can get the author information for the document. I do not need the contents of the document (e.g. AllDocStreams content) Something like this: SELECT tp_Di...

SQL Query - Using Order By in UNION

How can one programmatically sort a union query when pulling data from two tables? For example, SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1 Throws an exception Note: this is be attempted on MS Access Jet database engine ...

Why specify primary/foreign key attributes in column names

A couple of recent questions discuss strategies for naming columns, and I was rather surprised to discover the concept of embedding the notion of foreign and primary keys in column names. That is select t1.col_a, t1.col_b, t2.col_z from t1 inner join t2 on t1.id_foo_pk = t2.id_foo_fk I have to confess I have never worked on any databa...

What are views good for?

I'm just trying to get a general idea of what views are used for in RDBMSes. That is to say, I know what a view is and how to make one. I also know what I've used them for in the past. But I want to make sure I have a thorough understanding of what a view is useful for and what a view shouldn't be useful for. More specifically: Wha...

Checking if mysql_query returned anything or not

$query = "SELECT * FROM `table`"; $results = mysql_query($query, $connection); If 'table' has no rows. whats the easiest way to check for this.? ...

Reconciling a column across two tables in SQL Server

There are two Databases, Database A has a table A with columns of id, group and flag. Database B has a table B with columns of ID and flag. Table B is essentially a subset of table A where the group == 'B'. They are updated/created in odd ways that are outside my understanding at this time, and are beyond the scope of this question ...

Is there any performance reason to use powers of two for field sizes in my database?

A long time ago when I was a young lat I used to do a lot of assembler and optimization programming. Today I mainly find myself building web apps (it's alright too...). However, whenever I create fields for database tables I find myself using values like 16, 32 & 128 for text fields and I try to combine boolean values into SET data field...

What's the best way to strip literal values out of SQL to correctly identify db workload?

Does anyone know of any code or tools that can strip literal values out of SQL statements? The reason for asking is I want to correctly judge the SQL workload in our database and I'm worried I might miss out on bad statements whose resource usage get masked because they are displayed as separate statements. When, in reality, they are p...

SQL for the web

Does anyone have experience with a query language for the web? I am looking for project, commercial or not, that does a good job at making a webpage queryable and that even follows links on it to aggregate information from a bunch of pages. I would prefere a sql or linq like syntax. I could of course download a webpage and start doing ...

Options for in-process databases under medium trust

I have seen a few different in-process SQL databases for .NET (including one from Microsoft), but either they do not work under medium trust (ASP.NET) or the documentation/websites don't even talk about. What experiences have you had with in-process databases in general, and do you know of any that work under medium trust for ASP.NET? ...

SQL Builder for PHP, with JOIN support?

Hi, Are any of you aware of a library that helps you build/manipulate SQL queries, that supports JOIN's? It would give a lot of flexibility i'd think if you have something where you could return an object, that has some query set, and still be able to apply JOIN's to it, subqueries and such. I've search around, and have only found SQL...

can't insert a record into table

Hi, I wrote a program which includes writing and reading from database. When I run the app and try to perform writing I call the following method: public static void AddMessage(string callID, string content) { string select = "INSERT INTO Sporocilo (oznaka_klica, smer, vsebina, prebrano, cas_zapisa) VALUES (@cal...

Database Abstraction - supporting multiple syntaxes

In a PHP project I'm working on we need to create some DAL extensions to support multiple database platforms. The main pitfall we have with this is that different platforms have different syntaxes - notable MySQL and MSSQL are quite different. What would be the best solution to this? Here are a couple we've discussed: Class-based SQL ...

NHibernate custom SQL object creation

Somewhat-simplified example situation: I have entities A and B which are incredibly "heavy" domain objects. Loading one from the database is a pretty big deal. Then I have an entity C, which is a very simple object that has a label string, one A, and one B -- both lazy. I'm doing some low-level querying to create huge lists of C, so I k...

Group by date in a particular format in SQLAlchemy

I have a table called logs which has a datetime field. I want to select the date and count of rows based on a particular date format. How do I do this using SQLAlchemy? ...

Emulate MySQL LIMIT clause in Microsoft SQL Server 2000

When I worked on the Zend Framework's database component, we tried to abstract the functionality of the LIMIT clause supported by MySQL, PostgreSQL, and SQLite. That is, creating a query could be done this way: $select = $db->select(); $select->from('mytable'); $select->order('somecolumn'); $select->limit(10, 20); When the database s...

Skipping primary key conflicts with SQL copy

I'm new to database development so hopefully this is trivial. I have a large collection of raw data (around 300million rows) with about 10% replicated data. I need to get the data into a database. For the sake of performance I'm trying to use SQL copy. The problem being when I commit the data, primary key exceptions prevent any of the d...

Storing C# data structure into a SQL database

I am new to the world of ASP.NET and SQL server, so please pardon my ignorance ... If I have a data structure in C# (for e.g. let's just say, a vector that stores some strings), is it possible to store the contents of the vector as is in SQL table? I want to do this so that it fast to convert that data back into vector form as fast as p...