Here you need to use the case-when statement, which is the IF Else statement in sql:SelectA.INS_ID,B.PRODUCT_ID,B.product_name,C.CUST_ID,C.cust_name,C.cust_sex,C.cust_age,C.family_num,--Male--This place is handled differently depending on the database field.--If the data type of cust_sex in the database itself is 0 and 1, then there is no need to convert--just list them.(Case when c.cust_sex= ' man ' then 1 else 0 end) as F,--Female(Case if c.cust_sex= ' then 1 else 0 end) as as M,--Other and so on--Number of family members(case when C.family_num=1 then 1 else 0 end) as P_1,(case when c.family_num=2 then 1 else 0 end) as p_2,(case when c.family_num=3 then 1 else 0 end) as P_3,(case when C.family_num>3 then 1 else 0 end) as GT3,--Customer Age(case when c.cust_age<=25 then 1 else 0 end) as Lt25,(Case if c.cust_age>25 and c.cust_age<=35 then 1 else 0 end) as Gt25lt35,(Case if c.cust_age>35 and c.cust_age<=45 then 1 else 0 end) as Gt35lt45,(Case if c.cust_age>45 and c.cust_age<=55 then 1 else 0 end) as Gt45lt55,(Case if C.cust_age>55 then 1 else 0 end) as Gt55FromInsurance A,Product B,Customer Cwherea.product_id=b.product_idand a.cust_id=c.cust_idThe last group count can be:SelectA.PRODUCT_ID,A.product_name,Count (a.ins_id) as Ins_num,--GenderCount (A.F) as F_num,Count (a.m.) as M_num,--Number of membersCount (a.p_1) as P_1_num,Count (a.p_2) as P_1_num,Count (A.p_3) as P_1_num,Count (A.GT3) as Gt3_num,--AgeCount (Lt25) as Lt25_num,Count (Gt25lt35) as Gt25lt35_num,Count (gt35lt45) as Gt25lt35_num,Count (gt45lt55) as Gt25lt35_num,Count (gt55) as Gt55_numFromSelectA.INS_ID,B.PRODUCT_ID,B.product_name,C.CUST_ID,C.cust_name,C.cust_sex,C.cust_age,C.family_num,--Male--This place is handled differently depending on the database field.--If the data type of cust_sex in the database itself is 0 and 1, then there is no need to convert--just list them.(Case when c.cust_sex= ' man ' then 1 else 0 end) as F,--Female(Case if c.cust_sex= ' then 1 else 0 end) as as M,--Other and so on--Number of family members(case when C.family_num=1 then 1 else 0 end) as P_1,(case when c.family_num=2 then 1 else 0 end) as p_2,(case when c.family_num=3 then 1 else 0 end) as P_3,(case when C.family_num>3 then 1 else 0 end) as GT3,--Customer Age(case when c.cust_age<=25 then 1 else 0 end) as Lt25,(Case if c.cust_age>25 and c.cust_age<=35 then 1 else 0 end) as Gt25lt35,(Case if c.cust_age>35 and c.cust_age<=45 then 1 else 0 end) as Gt35lt45,(Case if c.cust_age>45 and c.cust_age<=55 then 1 else 0 end) as Gt45lt55,(Case if C.cust_age>55 then 1 else 0 end) as Gt55FromInsurance A, product B, customer C where a.product_id=b.product_id < Span class= "Apple-converted-space" > and a.cust_id=c.cust_id ) a group by b.product_id, b.product_name
By now the unknown, the result that we want is completely come out. The whole process is a complex process, which is simplified and complicated by simple piling. for beginners, it seems difficult to cultivate such a way of thinking, but as long as the practice of one or two such needs, this way of thinking is very easy to form, in the end, when you see a report structure, this process of thinking is just a moment, your brain The nautical mile is a data table of N, and after that, your mind gets the structure of the previous result set of the report structure, and then pushes forward the structure of the previous result set until it is pushed to the main table, and then pushes a part forward to the complete report structure, which is a very fast thinking process. , it's likely that you'll know how to write when your brain just pushes one or two steps.
I would like to use this text for beginners to SQL friends.