Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [pivot]

A pivot (or crosstab) converts row data into columnar data, and vice versa for an unpivot. Not all databases support explicit PIVOT syntax but the functionality can be implemented, often using decision logic (CASE statements, etc), aggregate functions, or an extension.

-1
votes
0answers
21 views

Attendance Report Pivot Table - Mysql

I have 4 tables in my database: students (studentid,studentname) subjects (subjectid, subjectcode,subjecttitle) attendance (id, studentid, subjectid, attendance_date) student_subjects (studentid, ...
0
votes
1answer
13 views

SELECT , GROUP AND DISPLAY IN TABULAR FORM AND COUNTING INSTANCES

Answer to this query was received as - SELECT location, AVG(CASE WHEN qtr = 'Q1' THEN achievement END) q1-average, AVG(CASE WHEN qtr = 'Q2' THEN achievement END) q2-average FROM sourcetable ...
0
votes
1answer
32 views

Pivot data type error

I have a table which needs to do pivot as column to row, but getting data type error, I have below table data Need to pivot and output should be like I tried with query: select prod As product_id ,...
0
votes
0answers
30 views

Mysql How to get joined tables data horizontally

I'm trying to get the addresses with that compairing amounts horizontally which found twice in address table. I'm getting the address name and product name but couldn't figure out how to get amounts ...
0
votes
2answers
52 views

How can i filter by gender from a subquery

I have the following query: SELECT DISTINCT name, surname, id_number, gender FROM table1; How do I filter by gender to get the gender breakdown of the results of the above query? Do I use a subquery?...
-1
votes
0answers
56 views

Oracle Pivot - ORA-00918: column ambiguously defined

In Oracle OBIEE (Oracle Business Intelligence 11.1.1.9.0) I have the following code which throws a ORA-00918: column ambiguously defined error SELECT * FROM ( SELECT Acc_Status, SuspendedStatus, ...
1
vote
1answer
26 views

Pivot information - sql table

I was wondering if anyone could help me with pivoting data in sql? I am a newbies and need some help I want to pivot data based on "LastLogOn" column. Is it possible to do in SQL? From: To:
2
votes
1answer
42 views

How do I pivot to summarize my entries by a specific area?

I have a map where every row represents a bird and a row with the attributes shown in the excerpt of the attribute table: round | area | date | species | behaviour --------------------...
0
votes
1answer
83 views

Transposing repeated rows of datetime into columns in SQL 17

I am working with a dataset of inpatient data in SQL version 17. The fields contain anonymized ID, admission date, discharge date and other relevant fields. Since some of the patients are readmitted, ...
-1
votes
1answer
47 views

How to get one column and select other column as horizontally

Given data of the form... +-------------+--------------+ | Employee_ID | Candidate_ID | +-------------+--------------+ | 1000 | 1 | | 1000 | 2 | | 1000 | 5 ...
-3
votes
1answer
52 views

How to display multiple columns as rows + columns [closed]

I have this table and data: CREATE TABLE #t1 ( [Year] int, Student int, Test char(2), Score int, Achievement varchar(32) ); INSERT #t1 VALUES (2014,1,'MA',697, '...
1
vote
0answers
19 views

Dynamic rows and columns with Pivot and calculated value

I'm trying to build a pivot request but i don't undestand how it's applicable for my database. The value I need has to be calculate (A percentage) for each column and row. I'm using SQL Server 2012. ...
0
votes
1answer
36 views

How to pivot tables view/results from row to columns

I have e-mail aliases tables that looks like this: +---------------------------------+--------------------------+-------------------------+-------------+---------------------+---------------------+---...
0
votes
1answer
23 views

Sort pivot result

I just want to ask how to sort the result of a PIVOT in SQL Server. When I use ORDER BY there is an error: "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, ...
0
votes
2answers
44 views

Divide data in single column to multiple columns

I have source data like Name Is_Trget a Yes s No d Yes f Yes a Yes S No from this source data expected output is Name Is_Yes Is_No a 2 0 s 0 2 d 1 ...
8
votes
2answers
208 views

How to create recurrent weekdays as columns in a pivot?

I am newcomer to programming and databases and I would be grateful for some help on the following scenario. I use PHP with SQL Server. I am building an employee attendance system and I would like to ...
1
vote
2answers
67 views

Create 2D truth table from “cross product” of two tables via PostgreSQL view or function

I have a working Excel-based method for creating a truth table from two vectors exported from a PostgreSQL database. The process takes about 4 hours to complete due to a large number of VLOOKUP and ...
0
votes
1answer
194 views

Oracle convert multiple rows to column using pivot

I have a table that contains the values +-------------+--------------+---------------------+-------------+-------------+--------------+-------+---------+-----------+---------------+--------------+ |...
0
votes
0answers
24 views

How do I PIVOT this schema in MySQL?

I have a relation in a MySQL database with the following schema Data(x: INTEGER, y: INTEGER, value: INTEGER) The combination of (x, y) is unique and spans from 0 to 100. Is it possible to combine ...
0
votes
1answer
106 views

SQL query to convert Column to Row using Pivot/Unpivot [duplicate]

My Select Table. I tried Pivot query to get my expected result but I was struct to generate the expected result. WITH PersonOptions(ID,[Year], [Text], Value1, Value2) AS ( SELECT 'A',...
2
votes
1answer
119 views

How to combine a parent with all children in the same row

SQL Statement Question: I have two tables PARENT and STUDENTS. PARENT Table ParentID | FName | LName | StudentID ---------+-------+-------+---------- 1 | John | Doe | 11 1 | ...
0
votes
2answers
69 views

How can I pivot this data using a cte or the pivot syntax?

Let's say I have these two tables in an MSSQL-Database: Is there any way to create a query that result in a table like this? There is no case where there are multiple Leader/Assistant in the TEAMS-...
2
votes
1answer
15 views

How can I pivot a table then join multiple tables and change values based on information between tables

The database I'm using has multiple tables storing different information types...it's very convoluted. I'm trying to join this information into one table, with all of the details for a single ...
0
votes
4answers
110 views

Can Group By and Count on MySQL be optimized?

I have a 100 million row table on MySQL. I need to count rows for certain ranges and I have the proper indices for filtering rows. Let's say a SELECT statement returns 20000 results, but all I need is ...
0
votes
2answers
56 views

how to change this dynamic rows to columns

I have following columns (it is dynamic, more rows in table with same ID).Type can be duplicate. +--------------+---------+------------ | ID | TYPE | VALUE | +--------------+---------...
2
votes
1answer
171 views

Firebird query pivot

I have a Firebird sql query that returns 3 fields: Surname, Name and date as (YYYY-MM-DD HH:mm:ss) select Surname, Name, "WHEN" from users left join attendant on (users.id = attendant.userid); This ...
0
votes
1answer
37 views

How to find value for specific dates for a process stored in table

I have a table called as tab where we store all the process running on machine in below format proc_name start_date Duration proc1 24-Jul-18 0:0 proc2 24-Jul-18 0:0 ...
2
votes
1answer
1k views

Mysql Pivot rows into dynamic columns with unknown number of columns

I have an output of a query as follows: bid | code | amount | ----------------------------- 2915 | BF | -10700.00 | 2915 | YQ | -300.00 | 2915 | YR | 0.00 | 2915 | YM ...
3
votes
0answers
280 views

Query to split data in a column into multiple columns using id in another column (pivot table; crosstab)

Database engine: Server version: 10.2.16-MariaDB-10.2.16+maria~xenial mariadb.org binary distribution I have a database table defined as follows: CREATE TABLE `disktemp` ( `sample_time` datetime ...
1
vote
2answers
47 views

Pivot values to columns

I'm trying to create a view that groups score data for each week and displays it for each user but I don't know which commands to use. Sample data: // scorecards table week | user | score | 1 ...
1
vote
0answers
35 views

need help with multiple column aggregate using pivot in mssql

below is the temporary table for class assessments. this table contain list of class assessment which include the assessment code, date of assessment, total item and passing percentage. create table #...
0
votes
0answers
21 views

crosstab query : count a number occurence in a row

I have a crosstab query that shows 1 and 0 in the value area. Function 1 Function 2 ..Function n project 1 1 0 1 project 2 0 0 1 .. project ...
-1
votes
1answer
30 views

How to do looping and use case when logic from dynamicaly variable in mysql

I'm new in mysql Before I have question and have the answer from someone. But I need a improve again from the query. This is my sampling data on mysql db table test With this query on below from ...
2
votes
1answer
39 views

PIVOT with SQL Server

I have SQL Server table data returning the below data. (Green text is a note on the value of each cell in 'Outcome' column) I want to PIVOT that query result so that it uses the latest 'ReceivedTime' ...
1
vote
1answer
194 views

Default values for crosstab results

I'm trying to write my first crosstab query in Postgres. Some of the results have no entries and so the rows that are returned have null entries. How do I set a default value for those entries? Here ...
0
votes
1answer
60 views

How to pivot a column showing its content as new columns

I'm working with MySQL 5.6 and I'm having headaches with this issue. I've been looking in several groups the result, but I think this isn't very common. I have a table like the following one: direc ...
2
votes
1answer
54 views

Find time difference in different rows with a condition [closed]

I have a table showing statuses (working, not working) for several devices (ordered by device desc, datetime desc): rownum device datetime status ---------------------------------...
0
votes
1answer
32 views

Query multiple tables

I store time stamps in four different tables and I like to query mysql to give me total amount of timestamps for a given day. SELECT time_stamp AS Tid, Count(time_stamp) as Tot FROM (select ...
1
vote
1answer
241 views

Join tables and pivot

tbl_orders order_id, qty 123, 2 124, 1 125, 3 126, 1 127, 1 128, 1 129, 4 tbl_score order_id, name, created 123, product 1, 2018-06-01 124, product 1, 2018-06-01 125, product 1, 2018-06-02 126, ...
1
vote
0answers
355 views

How to transposing rows into columns in MySQL?

I have three table (student's_info,subject and marks): this is table student's_info: ---------------- |s_id | s_name | ---------------- | 1 | Ali | | 2 | Jon | --------------- This is ...
3
votes
2answers
69 views

Merge multiple rows into fewest number of rows of distinct values

In SQL Server does anyone know of a nice way to merge/flatten multiple rows of data into the fewest possible rows containing only the distinct non null values. I.e. A dataset like this: As this: ...
0
votes
1answer
29 views

Show Rows as Columns in SQL Server [duplicate]

I havent worked with Pivot. Not sure if pivot is the right thing to use. Is it possible to show this data: ------------------- | Name | Code | ------------------- |Test A | 1 | |Test B | ...
3
votes
1answer
555 views

Pivot with rollup in sql server

Is it possible to use a cursor to create a dynamic table and then use those columns to aggregate data in SQL server 2008? For example take the following table. CREATE TABLE Billing ( BillingId ...
0
votes
0answers
15 views

get an array with a crosstab as multiple join

I have a ad_id from action_states u1 join with the table ad_media on u1.ad_id=ad_media.ad_id so basically as a return result i must have a set of array ad_media_id , it keeps giving me the results ...
-1
votes
1answer
98 views

Selecting roles from multiple rows into a single row

I have a table like this: As you can see studentId: 43 has two different GIDs (these are roles, with a max of 3). I have to select the record like:
0
votes
1answer
545 views

monthly report in mysql

can any body help me to edit this code to met requirements of charts pages i'm preparing for my project ? code : SELECT section, DATE_FORMAT( date_adm, '%Y-%m' ) AS datetime, COUNT( section ) AS ...
0
votes
1answer
180 views

Add a New Column with ALIASES on 'date' by automation based on search results

I am trying to create a new column based on date search result. I have written a code in the link SQL fiddle as I am looking for date automation on each column based on the date format. the date ...
1
vote
1answer
138 views

How to optimize a fetch into a crosstab

I have a schema like: [ad_id] . [name] . [valueofname] 1 . name . "brian" 1 . age . "23" 2 . job . "IT" 2 . name . "Jack" The row name contains ...
4
votes
2answers
788 views

Dynamic dates Pivot

Sample Data from StartKPA table: I wrote the following query: SELECT * FROM ( SELECT BookInventoryDate, EndKpa,CASE WHEN (EndKPA*100)>80 THEN '>80' WHEN (EndKPA*100) ...
1
vote
1answer
314 views

Transpose Rows to column

Can anyone Please help me to transpose below result +------+------------+-------+ | SVR | dates | count | +------+------------+-------+ | SRV1 | 03/25/2018 | 1 | | SRV1 | 04/06/2018 | 59 ...