pivot

Doing a count MySQL query?

I have the following table: UID | ID | Type 1 | 1 | product 1 | 2 | product 1 | 3 | service 1 | 4 | product 1 | 5 | product 2 | 6 | service 1 | 7 | order 2 | 8 | invoice 2 | 9 | product I want to end up with: UID | product | service | invoice | order 1 | 4 | 1 | 0 | 1 2 | 1...

SQL Server 2005 - Pivoting Data without a sum / count and dynamic list of values

Sorry if this is covered elsewhere in another question, but I can't find any examples which exactly match what I need and I'm getting confused. I have data in the table like this :- Name | Value --------------- John | Dog John | Cat John | Fish Bob | Python Bob | Camel And I would like the data like this.......

Convert "flat" SQL query result into table structure

I've got a Sql Table that returns a result as follows: Amount Descrip ---------------------------- 1.34 Group1 Description1 2.36 Group2 Description2 5.46 Group3 Description3 4.54 Group1 Description4 6.23 Group2 Description5 I need to make it look something like this. Descrip Group1 Group2 Group3 ----------...

Ledger format for datagrid or dataset

I am trying to work up a general ledger appearance in an asp.net page. Here's the data i have so far. AccountName Desc Type Amount ----------------------------------------- Account1 Stuff Debit 5000 Account1 Stuff Credit 4000 Account2 Other Debit 3000 Account2 Other Credit 6000 A...

Select 1 row per order, but include order_line table?

I have two tables order [ order_id ] order_line [ order_id, product_id, cat_id ] I want to select every order & associated order_line, but I want row per order, and i only want to select cat_id from order_line so i want a result like this.. results: [0] order_id, cat_id1 , cat_id2, cat_id3 [1] order_id, cat_id Possible? ...

Pivot / unpivot in SQL

Hi, I have a view in SQL that I have generated by analysing the values in tables so that field either contain the value 'N', 'D' or 'V'. I can work out the totals by column but not by row... Is this possible? Example: Data No, Col_1, Col_2, Col_3 1, N, N, N 2, N, D, D 3, N, V, D 4, V, ...

sql charateristic function for avg dates

I have a query which I use to grab specific dates and a price for the date, but now I'd like to use something similar to grab the avg prices for particular days of the week. Here's my current query which works for specific dates to pull from a table called availables: SELECT rooms.name, rooms.roomtype, rooms.id, max(availables.updated_...

How do I pivot dynamically

I have two tables; Leads, Territories and Referrers. Lead has columns: ID, Name, TerritoryId Referrers has: ID, LeadId, Name Territory has: ID and Name A lead always relates to a territory and a lead can optionally relate to a Referrer. Leads and Referrer records are regularly inserted (Referrers less frequently). I want to outpu...

Excel: Formula to reference an entire pivot table?

I have a bunch of sheets with detailed data sets and pivot tables. On a summary sheet, I want to display just the pivot tables. (Of course, I'd rather stay DRY and not create a whole new set.) How can I reference the old pivot tables? I can use VBA to do this if necessary. ...

Problem in dynamic pivoting + sql server 2005

I have a problem. Actually in our application, earlier the customer was allowed to pay 3 installemnt per month but now it can be any number . So I have the earlier query declare @tbl table([MonthName] varchar(50), [Installment] int) insert into @tbl select 'Jan',100 union all select 'Jan',200 union all select 'Jan',300 union all sele...

Pivot rows to columns based on content in Oracle 10g PL/SQL

I have a table in my database, user_answers that stores users answers to a series of questions, with rows; user_id, question_id, answer_id and text_entry. Question text and answer text (if any) are stored in lookup tables. There are three types of questions, single-answer questions, multiple-answer questions and text-entry answer quest...

Retrieve ADO Recordset Field names (Classic ASP)

Hi all, I wonder if someone can help: Long story short, I'm using MSSQL2005 to build a Pivot table. The data being examined is limited by date range (All data for 1 week from the nearest Monday to the date selected) When I run the Stored Proc and pass it a date, I get The correct table back eg: Time | 1 Jan 09 | 2 Jan 09 | ...

SQL question : How do I pivot mutiple results to single row?

I have table in data base name "train delay, with columns train number(int), DelayTime(int), DelayReason(nchar) so the train may have more than one delaytime for each delay reason, for example: trainnumber,Delaytime,DelayReason 1 ,5 sec ,x 1 ,10 sec ,Z 1 ,70 sec ,TY I want to create a crystal rep...

SQL Pivot Table

I have a SQL view with following data: ID ClassName Description Flags 1 Class1 Desc1 F1 2 Class1 Desc1 F2 3 Class1 Desc1 F3 4 Class1 Desc1 F4 5 Class2 Desc2 F2 6 Class2 Desc2 F6 7 Class3 Desc3 F1 8 Clas...

Using PIVOT in SQL Server 2008

Let's say I have some data, either in a SQL Server 2008 table or a [table]-typed variable: author_id review_id question_id answer_id 88540 99001 1 719 88540 99001 2 720 88540 99001 3 721 88540 99001 4 722 8...

Crazy SQL question: How to do a sort of cross apply with a pivot?

Hello. Customer has shortcuts in their data where they have a quantity of records in a field. When I parse them, I need to manufacture records, one for each quantity, incrementing the "identifier" by 7 days (because the number represents a date.Example: a single product that is on sale for four weeks and I need four records, one product...

SQL Server dynamic pivot returning null value when none exist in the data

I have 3 tables (tblPreference, tblCustomer, tblCustomerPreference) that look something like the following: tblPreference: ID | Name | DefaultValue (int PK) | (nvarchar(100)) | (nvarchar(100)) ------------------------------- 1 | Preference1 | 1 2 | Preference2 | Yes 3 | Preference3 | 1 ...

SQL Convert Rows To Columns

I know this has been asked a few times before, but I can't find any solution that fits my example. I currently have a table of user permissions to use certain pages. The table would look like this: UserID pagename pageid ----------------------------------- 1 home 1 1 contacts 3 3 h...

rails db neutral pivot table or crosstab

Does anyone know of a way to build a pivot table using activerecord which would be remotely DB neutral? I've tried to avoid using find_by_sql and DB specific queries but for a pivot table or crosstab query I have no idea how to do it in a way which is not specific to say MySQL. IE my mySQL find_by_sql breaks on a postgresql DB. I foun...

mysql join query

I have a query with the following tables (reduced to show only the interested columns). t1 code t2 code, period, status t3 period, desc Now what I have is, t3 is a table of unique "periods". t1 is a table of unique codes. t2 is the join table linking both together, along with a status, for sake of this example status=(A,B,C). Wh...