pivot

Select multiple aggregates of a joined table on Postgres

ε祈祈猫儿з 提交于 2020-02-25 13:13:50
问题 Given the tables projects : id | bigint | not null default nextval('projects_id_seq'::regclass) name | character varying | created_at | timestamp(6) without time zone | not null updated_at | timestamp(6) without time zone | not null and tasks : id | bigint | not null default nextval('tasks_id_seq'::regclass) name | character varying | project_id | bigint | not null created_at | timestamp(6) without time zone | not null updated_at | timestamp(6) without time zone | not null status | task

Using PIVOT with 2 related tables

烈酒焚心 提交于 2020-02-25 04:18:17
问题 I am working in SQL Server (SSMS) with two tables like: prop_ppl: id_numb id_pers cdate val 1 4 NULL NULL 2 2 2018-12-12 250 3 1 2018-12-01 250 4 3 2018-12-11 500 5 6 2018-01-01 500 6 5 2018-12-12 480 ppl: id_perc name 1 John 2 Derek 3 Mia 4 Chris 5 Ann 6 Dave Then i need to get the table like this: name id_numb value for these tables it should be, when its nececcary to find all values for ppl with date 2018/12/12: Derek Ann 2 250 0 6 0 NULL Code: CREATE TABLE ppl( id_perc smallint PRIMARY

Using PIVOT with 2 related tables

白昼怎懂夜的黑 提交于 2020-02-25 04:16:30
问题 I am working in SQL Server (SSMS) with two tables like: prop_ppl: id_numb id_pers cdate val 1 4 NULL NULL 2 2 2018-12-12 250 3 1 2018-12-01 250 4 3 2018-12-11 500 5 6 2018-01-01 500 6 5 2018-12-12 480 ppl: id_perc name 1 John 2 Derek 3 Mia 4 Chris 5 Ann 6 Dave Then i need to get the table like this: name id_numb value for these tables it should be, when its nececcary to find all values for ppl with date 2018/12/12: Derek Ann 2 250 0 6 0 NULL Code: CREATE TABLE ppl( id_perc smallint PRIMARY

pandas pivoting a dataframe, duplicate rows [duplicate]

喜你入骨 提交于 2020-02-13 05:35:26
问题 This question already has answers here : How to pivot a dataframe (2 answers) Closed 2 years ago . I'm having a little trouble with pivoting in pandas. The dataframe (dates, location, data) I'm working on looks like: dates location data date1 A X date2 A Y date3 A Z date1 B XX date2 B YY Basically, I'm trying to pivot on location to end up with a dataframe like: dates A B C date1 X XX etc... date2 Y YY date3 Z ZZ Unfortunately when I pivot, the index, which is equivalent to the original dates

SQL - Total Pivot Column

坚强是说给别人听的谎言 提交于 2020-02-08 08:27:46
问题 for the following sql script, I would like to add a "Total" column at the end to sum the data in the columns for each row. please help: select * from (SELECT Calender.YrPer, [Period DATA 6b].PRODUCT_NO, isnull(sum([Period DATA 6b].LI_QTY+[Period DATA 6b].RG_QTY*1.0),0) as "TotalUnits" FROM Calender INNER JOIN [Period DATA 6b] ON Calender.[Starting Period] = [Period DATA 6b].STARTING_PERIOD LEFT OUTER JOIN Allproducts ON [Period DATA 6b].PRODUCT_NO = Allproducts.PRODUCT_NO where Calender.YrPer

SQL - Total Pivot Column

↘锁芯ラ 提交于 2020-02-08 08:27:28
问题 for the following sql script, I would like to add a "Total" column at the end to sum the data in the columns for each row. please help: select * from (SELECT Calender.YrPer, [Period DATA 6b].PRODUCT_NO, isnull(sum([Period DATA 6b].LI_QTY+[Period DATA 6b].RG_QTY*1.0),0) as "TotalUnits" FROM Calender INNER JOIN [Period DATA 6b] ON Calender.[Starting Period] = [Period DATA 6b].STARTING_PERIOD LEFT OUTER JOIN Allproducts ON [Period DATA 6b].PRODUCT_NO = Allproducts.PRODUCT_NO where Calender.YrPer

MySQL Pivot or Excel Solution

倖福魔咒の 提交于 2020-02-05 03:52:12
问题 I have a table: CREATE TABLE `Issues` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; I have another table: CREATE TABLE `Attachments` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `issue_id` int(11) DEFAULT NULL, `attachment` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB; How can I get the data to look like this: issue_id title attachment1 attachment2 attachment3 ------------------------------------------------------------

SQL Server PIVOT Function

大憨熊 提交于 2020-02-04 06:36:26
问题 I have a query that retrieves all agents and thier modules, the result set will return 1 row per module. SELECT am.agentID AS agentid, pa.agentDisplayName agentdisplayname, m.ModuleName ModuleName FROM AgentModule AS am JOIN primaryagent AS pa ON am.agentID = pa.AgentID JOIN Module AS m ON am.ModuleID = m.ModuleID WHERE m. Active = 1 AND pa.groupID = 75 Dataset is return as below agentid | agentdisplayname | modulename 94 | Agent1 | Module 1 94 | Agent1 | Module 2 94 | Agent1 | Module 3 23 |

Pivoting a table along with sum of column value when column type is nvarchar

落爺英雄遲暮 提交于 2020-01-30 06:01:25
问题 I have a table with following structure. I want to Transpose it. BookId Status ---------------------- 123A Perfect 123B Restore 123C Lost 123D Perfect 123A Perfect 123B Restore 123A Lost 123B Restore I need the transpose table to look something like this. Output BookId Total Perfect Restore Lost ----------------------------------------- 123A 3 2 0 1 123B 3 0 3 0 123C 1 0 0 1 123D 1 1 0 0 I have tried this select BookId, sum('Perfect') as Perfect, sum('Restore') as Restore from [dbo].[Orders]

Pivoting a table along with sum of column value when column type is nvarchar

谁说我不能喝 提交于 2020-01-30 06:00:26
问题 I have a table with following structure. I want to Transpose it. BookId Status ---------------------- 123A Perfect 123B Restore 123C Lost 123D Perfect 123A Perfect 123B Restore 123A Lost 123B Restore I need the transpose table to look something like this. Output BookId Total Perfect Restore Lost ----------------------------------------- 123A 3 2 0 1 123B 3 0 3 0 123C 1 0 0 1 123D 1 1 0 0 I have tried this select BookId, sum('Perfect') as Perfect, sum('Restore') as Restore from [dbo].[Orders]