Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

सिंगल कॉलम डेटा के आधार पर डायनेमिक हेडर के साथ MySQL पिवट टेबल

इस समान सूत्र पर एक नज़र डालें जहां मैंने कार्य को पूरा करने के लिए एक एसपी लिखा था

दो तालिकाओं (1-एम संबंध के साथ) में शामिल हों जहां दूसरी तालिका को एक पंक्ति में 'चपटा' करने की आवश्यकता होती है

संपादित करें। अपडेट किया गया उत्तर

create table `pivot` (
  `id` int(11) not null auto_increment,
  `categoryid` int(11) default null,
  `processdate` date default null,
  `percentchange` int(11) default null,
  primary key (`id`)
) engine=myisam auto_increment=9 default charset=latin1;

/*Data for the table `pivot` */

insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (1,4,'2011-05-10',1);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (2,4,'2011-05-11',22);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (3,4,'2011-05-12',3);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (4,7,'2011-05-10',4);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (5,7,'2011-05-11',5);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (6,12,'2011-05-10',6);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (7,12,'2011-05-12',7);
insert  into `pivot`(`id`,`categoryid`,`processdate`,`percentchange`) values (8,4,'2011-05-13',12);



delimiter //
drop procedure if exists dynamic_view2//
create procedure dynamic_view2(in sdate date,in edate date)
begin
declare finish int default 0;
declare cdate date;
declare str varchar(10000) default "select categoryid,";
declare curs cursor for select processdate from pivot where processdate between sdate and edate group by processdate;
declare continue handler for not found set finish = 1;
open curs;
my_loop:loop
fetch curs into cdate;
if finish = 1 then
leave my_loop;
end if;
set str = concat(str, "max(case when processdate = '",cdate,"' then percentchange else null end) as `",cdate,"`,");
end loop;
close curs;
set str = substr(str,1,char_length(str)-1);
set @str = concat(str," from pivot
            group by categoryid");

prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
end;//
delimiter ;


mysql> call dynamic_view2('2011-05-10','2011-05-13');
+------------+------------+------------+------------+------------+
| categoryid | 2011-05-10 | 2011-05-11 | 2011-05-12 | 2011-05-13 |
+------------+------------+------------+------------+------------+
|          4 |          1 |         22 |          3 |         12 |
|          7 |          4 |          5 |       NULL |       NULL |
|         12 |          6 |       NULL |          7 |       NULL |
+------------+------------+------------+------------+------------+
3 rows in set (0.00 sec)



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. सामान्य अजाक्स और लंबे मतदान के बीच अंतर

  2. PHP MySQL SQL पार्सर (INSERT और UPDATE)

  3. DUAL से 1 चुनें:MySQL

  4. MySQL के साथ रेल पर रूबी कैसे स्थापित करें और इसे एक कदम दर कदम गाइड के साथ काम करें?

  5. बैश से .sql स्क्रिप्ट कैसे निष्पादित करें