Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sikharma13
Starting Member
44 Posts |
Posted - 2013-10-29 : 21:55:50
|
I have a table for education non compliant.here's the structureDROP TABLE IF EXISTS `s_abcdeeh`.`ed_abcdeeh`;CREATE TABLE `s_abcdeeh`.`ed_abcdeeh` ( `PROVINCE` varchar(20) default NULL, `MUNICIPALITY` varchar(25) default NULL, `BARANGAY` varchar(20) default NULL, `HOUSEHOLD_ID` varchar(35) default NULL, `ENTRY_ID` varchar(15) default NULL, `MONTH1` varchar(10) default NULL,) ENGINE=InnoDB DEFAULT CHARSET=latin1;under ed_abcdeeh, there is a month1, month2..this is the sample data that is displayed under ed_abcdeehprovince|municipality|barangay|household_id|entry_id|month1|sn.diego|san fernando|concepci|12345-678901|11122233|-july-|----records 1 & 2 are with the same household id,sn.diego|san fernando|concepci|12345-678901|13214553|-july-|----but with different entry_idsn.diego|san fernando|concepci|12321-673121|13214123|-july-|sn.diego|san fernando|concepci|22321-973224|13124130|-july-|sn.diego|san fernando|concepci|44421-123225|16541220|------|this is the structure of py_abcdeehDROP TABLE IF EXISTS `s_abcdeeh`.`py_abcdeeh`;CREATE TABLE `s_abcdeeh`.`py_abcdeeh` ( `PROVINCE` varchar(30) NOT NULL default '', `MUNICIPALITY` varchar(30) NOT NULL default '', `BARANGAY` varchar(30) NOT NULL default '', `HOUSEHOLD_ID` varchar(30) NOT NULL default '', `ENTRY_ID` varchar(12) NOT NULL default '', `NO_ELI_FOR_EDUC_GRANT` varchar(1) NOT NULL default '', `NO_NCOM_EDUC_M1` varchar(5) NOT NULL default '0', `EDUC_GRANT_M1` varchar(5) NOT NULL default '', ) ENGINE=InnoDB DEFAULT CHARSET=latin1;this is the sample data that is displayed under my "current" py_abcdeehwhich is e_grant_m1 = no_eli_for_educ_grant * 300legend: no_ncom_educ_m1 = number of non compliant for education month 1------: e_grant_m1 = education grant for month 1province|municipality|barangay|household_id|entry_id|no_eli_for_educ_grant|no_ncom_educ_m1|e_grant_m1|sn.diego|san fernando|concepci|12345-678901|11122233|--------3------------|-------0-------|---900----|sn.diego|san fernando|concepci|12321-673121|13214123|--------2------------|-------0-------|---600----|sn.diego|san fernando|concepci|22321-973224|13124130|--------1------------|-------0-------|---300----|sn.diego|san fernando|concepci|44421-123225|16541220|--------0------------|-------0-------|----0-----|if you link the table ed_abcdeeh and py_abcdeeh.. here is my expected resultprovince|municipality|barangay|household_id|entry_id|no_eli_for_educ_grant|no_ncom_educ_m1|e_grant_m1|sn.diego|san fernando|concepci|12345-678901|11122233|--------3------------|-------2-------|---300----|sn.diego|san fernando|concepci|12321-673121|13214123|--------2------------|-------1-------|---300----|sn.diego|san fernando|concepci|22321-973224|13124130|--------1------------|-------1-------|----0-----|sn.diego|san fernando|concepci|44421-123225|16541220|--------0------------|-------0-------|----0-----|here is my current query... how to insert it here.. insert into py_abcdeeh(province,municipality,barangay,household_id,entry_id,no_eli_for_educ_grant) select m1.province,m1.municipality,m1.barangay,m1.household_id,m1.entry_id,m2.countfrom m_abcdeeh m1inner join (select household_id,sum(case when eligible_for_educ_grant = 'yes' then 1 else 0 end) as count FROM m_abcdeeh GROUP BY household_id )m2on m2.household_id = m1.household_idwhere m1.grantee = 'yes'please help... thanks in advance..VFP9.0 via MySQL 5.0 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-30 : 01:58:35
|
this?insert into py_abcdeeh(province,municipality,barangay,household_id,entry_id,no_eli_for_educ_grant,no_ncom_educ_m1,e_grant_m1) select m1.province,m1.municipality,m1.barangay,m1.household_id,m1.entry_id,m2.count,COALESCE(e.mnthcnt,0),(m2.count - COALESCE(e.mnthcnt,0)) * 300from m_abcdeeh m1inner join (select household_id,sum(case when eligible_for_educ_grant = 'yes' then 1 else 0 end) as count FROM m_abcdeeh GROUP BY household_id)m2on m2.household_id = m1.household_idLEFT JOIN (SELECT province,municipality,barangay,household_id,COUNT(month1) AS mnthcnt FROM s_abcdeeh.ed_abcdeeh GROUP BY province,municipality,barangay,household_id )eON e.province = m1.province,,m1.barangay,m1.household_idAND e.municipality = m1.municipalityAND e.barangay = m1.barangay AND e.household_id = m1.household_id where m1.grantee = 'yes' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sikharma13
Starting Member
44 Posts |
Posted - 2013-10-30 : 02:26:03
|
seems to have error sir visakh...have u tried it urself in ms sql? just askin... thank you! :)VFP9.0 via MySQL 5.0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-30 : 03:29:33
|
quote: Originally posted by sikharma13 seems to have error sir visakh...have u tried it urself in ms sql? just askin... thank you! :)VFP9.0 via MySQL 5.0
atleast post the error message rather than telling its causing error------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|