logo
down
shadow

convert rows into columns in my sql databse


convert rows into columns in my sql databse

By : jesus monzon
Date : November 18 2020, 11:13 AM
this one helps. You can use conditional aggregation max with case, grouping by the date:
code :
select report_date,
    max(case when item_id = 101 then value end) sprite,
    max(case when item_id = 102 then value end) egg
from item_data
group by report_date


Share : facebook icon twitter icon
sum all rows from databse if particular columns in those rows are the same

sum all rows from databse if particular columns in those rows are the same


By : Maria Rangel
Date : March 29 2020, 07:55 AM
it helps some times In my project I need to sum ammount of all rows with the same type and same params, other just leave as they are. For example is i have such a table: , Use "group by" clause:
code :
use test;

create table Testsum(id int, type int, ammount int,  param1 int, param2 int,  param3 int);

insert Testsum(id, type, ammount, param1, param2, param3) 
values(1, 1,  5, 1, 3, 2),
(2, 1, 20, 2, 3, 2),
(3, 1, 21, 1, 3, 2),
(4, 2, 10, 1, 3, 2),
(5, 2,  3, 1, 2, 1);

/*
id | type | ammount | param1 | param2 | param3
1  |  1   |    5    |   1    |    3   |   2
2  |  1   |    20   |   2    |    3   |   2
3  |  1   |    21   |   1    |    3   |   2
4  |  2   |    10   |   1    |    3   |   2
5  |  2   |    3    |   1    |    2   |   1
*/

select  type, sum(ammount) as ammount, param1, param2, param3
from Testsum
group by  type,  param1, param2, param3;
type,ammount,param1,param2,param3
1,26,1,3,2
1,20,2,3,2
2,3,1,2,1
2,10,1,3,2
select  max(id) as id, type, sum(ammount) as ammount, param1, param2, param3
from Testsum
where ammount is not null or ammount <> 0
group by  type,  param1, param2, param3 ;
convert all rows to columns and columns to rows in Arrays

convert all rows to columns and columns to rows in Arrays


By : JohnY
Date : March 29 2020, 07:55 AM
I wish this help you I'm trying to create this program (in python) that converts all rows to columns and columns to rows. , You can use zip to transpose the data:
code :
arr = [[13, 4, 8, 14, 1], [9, 6, 3, 7, 21], [5, 12, 17, 9, 3]]
new_arr = zip(*arr)
# [(13, 9, 5), (4, 6, 12), (8, 3, 17), (14, 7, 9), (1, 21, 3)]
Summarize rows by dataframe columns and then convert these rows to columns

Summarize rows by dataframe columns and then convert these rows to columns


By : katano66
Date : March 29 2020, 07:55 AM
I hope this helps . I have the dataframe below , Use dplyr and tidyr:
code :
library(dplyr)
    library(tidyr)

    x <- df %>%
      group_by(year, city, spec) %>%
      summarise(num = sum(num)) %>%
      spread(spec, num)

# Groups:   year, city [1]
   year city     df    vb
  <dbl> <fct> <dbl> <dbl>
1  2016 NY      112   179
SQL Server 2008 R2, convert columns to rows and rows to columns

SQL Server 2008 R2, convert columns to rows and rows to columns


By : kim man chong
Date : March 29 2020, 07:55 AM
Does that help well, after carefully looking at your question, I think that this is not duplicate, you not only want to convert columns to rows, you want to convert rows to columns simultaneously.
So you have to combine two solutions - unpivoting columns to rows and pivoting rows to columns, something like:
Convert Rows to columns and Columns to rows without pivot

Convert Rows to columns and Columns to rows without pivot


By : Nanha
Date : March 29 2020, 07:55 AM
I wish did fix the issue. , One way would be using case and apply
code :
Create table #temp (ProductCode varchar(20), YTMAch int,  YTMTg int,  MTDTg int,  YTDPer int,  MTDPer int)

Insert into #temp values
('PrimaxX'  , 1, 2 , 3, 4, 5),
('SuperGrip', 1, 2 , 3, 4, 5),
('WC'       , 1, 2 , 3, 4, 5),
('WP'       , 1, 2 , 3, 4, 5)

Select id,
  max(case when ProductCode = 'PrimaxX' then value end) PrimaxX,
  max(case when ProductCode = 'SuperGrip' then value end) SuperGrip,
  max(case when ProductCode = 'WC' then value end) WC,
  max(case when ProductCode = 'WP' then value end) WP
from 
(Select * from #temp cross apply (values ('YTMAch',YTMAch), 
                                         ('YTMTg',YTMTg),   
                                         ('MTDTg',MTDTg),   
                                         ('YTDPer',YTDPer),  
                                         ('MTDPer',MTDPer))v(id,value))c 
group by id
Related Posts Related Posts :
  • select the records with the highest amount
  • Changing Mysql golbal variable
  • How to create Effective Indexing on MySQL
  • `require': Incorrect MySQL client library version
  • How to save a FirstName of a Person efficiently without duplicate entries
  • Can I store my own sql query by mysql special function and execute stored query later?
  • MySQL comma separated field query
  • Why does the same exact query produce 2 different MySQL explain results?
  • Suppress warning output in bash
  • Doctrine "on update CURRENT_TIMESTAMP" annotation (Symfony2)
  • Designing "Friend" relationships across two mysql tables
  • Importing sql file using phpmyadmin in EasyPHP
  • Easy PHP and mysql ..php cannot see database on local pc (all works on server)
  • MySQL join optimization - bypassing filesort
  • How to Select record with the maximum value in a GROUP BY?
  • MySQL Innodb Full text Proximity Search Gives Horrible Performance
  • MySQL best approach for db normalising, relationships and foreign keys
  • How to update one table based on the count value of another table while matching ids?
  • MAMP Pro 5.1.1-terminates MySQL when quitting
  • SQL - Get date of most votes cast
  • "Fuzzier" search in mysql
  • Tracing mysqld Using SystemTap
  • SELECT + INSERT + Query Cache = MySQL lock up
  • SQL vs. NoSQL for medium complexity search systems
  • SQL Error when creating table
  • Is it possible to write a query to compare rows to other rows in same table?
  • Display all duplicate records based multiple columns
  • summing time column in mysql
  • InnoDB: Cannot open table
  • Why is IN() considered O(logN) operation?
  • Can a deadlock occur on commit?
  • Is there a fast way to do a '%phrase%' search in any DB?
  • Is using an IN over a huge data set a good idea?
  • How can I know if a table is already arranged in ascending order in MySQL?
  • MySQL - Designing database for a simple game
  • Why am I getting "Host '192.168.1.220' is not allowed to connect to this MySQL server"?
  • what type of password hash? possible mysql hash?
  • MySQL - Using a date range vs functions
  • Using IS NULL and COALESCE in OrderBy Doctrine Querybuilder
  • How to merge MySQL columns into one column if there are empty values in the table?
  • Cardinality Confusion
  • Renaming column multiple ids in SELECT
  • Group all from and to messages per user
  • How to handled the null values in WHERE IN condition in mysql
  • Is it okay to update primary key field to shift a record to the top?
  • Stop query through pdo
  • How to check whether particular value in field in MySQL exists
  • Execute a statement for every table in a database
  • insertion 74G data into mysql table cost more then 2 days, how to improve insert performance
  • Where can I see the differents "Hibernate Console Configurations" in Eclipse
  • MySql | relational database vs non relational database in terms of Performance
  • Exporting mysql database using mysqldump including procedures
  • Trigger MySQL not working
  • How do I update values for individual rows quickly?
  • MySQL join query with multiple where condition
  • MySQL stored procedure is not working
  • MySQL - Order By exact match at the end
  • Select max date or null date from same table MySQL
  • Best solution workaround for needing a mysql event on insert?
  • MySQL select several columns of several tables at the same time without using JOIN
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org