crosstab

Division with Aggregate Functions in SQL Not Behaving as Expected

依然范特西╮ 提交于 2019-12-12 21:18:29
问题 I'm trying to do some crosstabs in SQL Server 2008 R2. That part is alright, however, if I try to get percentages for each cell, I run into a problem. Here is a distilled use case: A survey where people give their favorite color and their favorite fruit. I'd like to know how many like a given fruit AND a given color: with survey as ( select 'banana' fav_fruit, 'yellow' fav_color union select 'banana', 'red' union select 'apple', 'yellow' union select 'grape', 'red' union select 'apple', 'blue

How do I convert the fields in one row into columns?

懵懂的女人 提交于 2019-12-12 13:50:48
问题 I have a table with columns such as: Cost Rate Repair 12 Repair 223 Wear 1000 Wear 666 Fuel 500 Repair 600 Fuel 450 Wear 400 and I want this data as columns(Repair,Wear,Fuel) as: Repair Wear Fuel 825 2066 950 How could I do this using an MS Access Query? 回答1: While there's a traditional SQL solution for this that is pretty kludgy, reading this page alerted me to the fact that MS Access has a TRANSFORM ... PIVOT statement you probably should look into and use to do this. I can't be certain but

Crosstab query in SQL that compares and adds columns

╄→尐↘猪︶ㄣ 提交于 2019-12-12 06:38:57
问题 I have a table in sql server that contains three columns: "date", "noon", and "3pm." The first column is self-explanatory, but the latter two contain the names of guest speakers at a venue according to the time they arrived. I want to write a cross-tab query that writes speaker names into the column header and counts the number of times that speaker spoke on that date. Example Date | Noon | 3pm 092916 | Tom | <null> 092816 | Dick | Tom 092716 | <null> | Suzy Desired Output Date | Dick | Tom |

SQL Server 2012 dynamic pivot - concatenation for cross tab

旧街凉风 提交于 2019-12-12 05:41:56
问题 Can I use a concatenation as the cross tab variable in a dynamic pivot? I'm trying to generate a cross tab table of values of transactions, where the column headers are [yyyy-mm] in date order based on a [transaction date] date field. This is the data:- RW_Ref ,Transaction_Date ,Transaction_Type ,Transaction_Value ,Batch_Number ,Month 1 , 03/09/2009 ,11 ,30 ,999 1 , 05/10/2009 ,11 ,25 ,999 1 , 11/10/2009 ,11 ,15 ,999 1 , 15/11/2009 ,11 ,40 ,999 1 , 03/12/2009 ,11 ,30 ,999 2 , 05/10/2009 ,11

Calendar with crosstab in jasperreports

大憨熊 提交于 2019-12-12 01:24:11
问题 I want to create a crosstab in the form of a calendar (Years as first group and months as second group in the columns) with jasperreports. I'm using MySQL database. 1) The first problem is, I'm getting only the months where the measure exists, I want to display all months of the year wether the measure exists for this month or not. 2) Second, I want to have a seperate crosstab for every year in a seperate page. 3) I can't get the months in the right order, they're ordered in alphabetical

Problems with aggregating and crosstabulating data

吃可爱长大的小学妹 提交于 2019-12-12 01:13:36
问题 My data looks like this, ca 3300 rows of data: Year Location Catch 1991 0313 45100 1989 0711 323 1991 0312 1100 1991 0313 45100 1989 0711 323 1991 0312 400 1990 0313 101000 1981 0711 623 1999 0312 410 2000 0313 145100 1987 0711 323 1987 1285 770 .... Years cover the period 1977-2015, and there are approx 500 different locations, not all having data in every year. I need an output like this, summing up the catch for each cell, tabulated by location (rows) and year (columns): Location '1977'

How to filter information within a Cross Tab in Crystal Reports

天大地大妈咪最大 提交于 2019-12-11 19:28:26
问题 I am trying to create a single cross tab that includes both filtered and non-filtered data. I understand that I could do this by creating separate cross tabs in separate groups, then using filter expert on the group level, but I need the results to displayed in the same cross tab. I believe a similar solution to this might work, but am unclear on the implementation: Crystal Reports Cross-Tab Column Totals as Variables I am attempting to report on the frequency of known customers and walk-in

Crosstab query: Getting Null Data for Missing Data from Access DB

冷暖自知 提交于 2019-12-11 18:24:40
问题 I have data in Access Database which contains data for multiple days. But it sometime have missing data for some dates. In example, I have data for myDate Location Price 11/1/2013 South 10 11/1/2013 West 20 11/1/2013 East 10 11/2/2013 South 10 11/2/2013 West 20 11/2/2013 East 10 11/4/2013 South 10 <---- 11/3/2013 Data Missing 11/4/2013 West 30 11/4/2013 East 10 The way I tried to solve it was to find missing date in Access Database, and filled it with Null value using calender table. myDate

Pivot the table

一曲冷凌霜 提交于 2019-12-11 15:26:36
问题 Id Year Values 1 2014 10 1 2015 4 1 2016 7 1 2017 17 2 2014 5 2 2015 6 2 2016 7 2 2017 9 Answer should be Id 2014 2015 2016 2017 ---------------------- 1 10 4 7 17 2 5 6 7 9 I tried as below Select * from crosstab(select id,value,year from table) As res(id int,year int,year int,year int,year int,year int); 回答1: Another option than using crosstab() would be using GROUP BY and some aggregate function like MAX() with CASE END to pivot. As extra bonus this is ANSI SQL so it will run in most RDBMS

PostgreSQL Crosstab - dynamic columns by query

倾然丶 夕夏残阳落幕 提交于 2019-12-11 15:21:24
问题 Is it possible to put query inside the crosstab function in the last bracket to don't hardcode? SELECT * FROM crosstab( 'SELECT id, brand, price FROM cars ORDER BY 1,2' ) AS ct ("id" int, "BMW" float, "Mercedes" float); I have SELECT prepared which stores 1 row and the query looks like this: SELECT '"id" int, "' || string_agg(DISTINCT brand::text, '" float, "' ORDER BY brand::text) || '" float' FROM cars; and it displays exactly this: "id" int, "BMW" float, "Mercedes" float Is it possible to