Recently received a question from someone asking for a single SQL statement to retrieve the counts for two subqueries (or something like that).
I had altered the question and and my answer below.
A company has several customers and courses. Each customer may be signing up on several courses.
Write an SQL query to display the number of women and the number of men signing on each course.
And my simple database design
CREATE TABLE `CUSTOMER` (
`CUSTOMER_ID` INT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 100 ) NULL ,
`GENDER` CHAR( 1 ) NULL ,
`EMAIL` VARCHAR( 150 ) NULL
) ENGINE = MYISAM ;
CREATE TABLE `COURSE` (
`COURSE_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 50 ) NOT NULL ,
`DESCRIPTION` VARCHAR( 255 ) NOT NULL ,
`START_DATE` DATE NOT NULL ,
`END_DATE` DATE NOT NULL
PRIMARY KEY ( `COURSE_ID` )
) ENGINE = MYISAM ;
CREATE TABLE `CUSTOMER_COURSE` (
`CUSTOMER_ID` INT NULL ,
`COURSE_ID` INT NULL ,
`STATUS` VARCHAR( 5 ) NULL
) ENGINE = MYISAM ;
And the SQL to “display the number of women and the number of men signing on each course”
select
sum(total_males) as total_males,
sum(total_females) as total_females,
COURSE_id
from
(select COUNT(e.GENDER) as total_males , 0 as total_females, ep.COURSE_ID
from CUSTOMER e, CUSTOMER_COURSE ep
where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'M'
group by ep.COURSE_ID
union all
select 0 as total_males, COUNT(e.GENDER) as total_females ,ep.COURSE_ID
from CUSTOMER e, CUSTOMER_COURSE ep
where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = 'F'
group by ep.COURSE_ID
) t1
group by COURSE_id
Explanation:
- the two subqueries generate 3 columns: total males, total females, course ID
- one of the columns in the two subqueries is 0
- the two subqueries would then UNION ALL to populate all results for the parent SQL
- the parent SQL would then do a SUM/COUNT for the subqueries by grouping the course ID.
- the ‘t1′ is just a empty table name thrown back to the parent for reference
VN:F [1.9.15_1155]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.15_1155]
By default, struts’s html radio tag do not allow you to make a radio to be selected as they have no “selected” property or something
<html:radio property="gender" value="F" />Male
<html:radio property="gender" value="M"/>Female
To make a radio button to be selected on load, set the value that you want to be selected in your action form reset method
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset(mapping, request);
gender= "F";
}
VN:F [1.9.15_1155]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.15_1155]
Saw this challenging question posted in the hardwarezone forum on sorting an array with one for loop without using array list.
The question is
This is a simple programming question...very simple and short question, but its really hard to solve..
Anyone who could do it, you are god of programming.
Given an array of random numbers let say..
arrayX = {15, 20, 35, 45, 10, 40, 1, 3, 45};You are supposed to use ONLY ONE loop to swap the above into the following:
arrayX = {15, 20, 3, 1, 10, 40, 45, 35, 45};
NOTE: The purpose of your loop is to move all digits < 25 from the right hand side to the left side, and move all digits >= 25 to the right side. You can't sort the array but to use a single loop to scan the array from left and scan the array from right...and do the necessary swapping.
You can try do it with C, C++...etc..
Not easy as it seems to be!
Since the night is still young, I took some time to sit down and figure out the solution.
Continue reading ‘Array sorting problem posted from hardwarezone’
VN:F [1.9.15_1155]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.15_1155]
Just when I was posting source code in my previous post, I realised that my newly installed wordpress does not format the source code nicely. (Read: I’m new to WordPress!)
Nevertheless, I google out and finally able to post source code in my blog.
Here’s how.
- Download Plugin: Go to SyntaxHighlighter Plugin Page and download the plugin
- Upload Plugin: Extract all files from the ZIP file, making sure to keep the file/folder structure intact, and then upload it to
/wp-content/plugins/.
- Activate Plugin: Go to the admin area of your WordPress install and click on the “Plugins” menu. Click on “Activate” for the “SyntaxHighlighter” plugin.
And here’s how to use it
When creating a new post, toggle to HTML view.
If the source code you want to use is in javascript, type…
["language"]alert(‘hello world’);[/ "language"]
in that view. Replace the above “language” with the language of your choice. In this case, it is replaced with “javascript” (For full supported language, visit author website.)
And it should display like the below.
alert('hello world');
VN:F [1.9.15_1155]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.15_1155]
I was assigned with a mini project to do a team notice board that read XML files using Spry.
While reading plain text in the XML is straightforward, reading of HTML codes like <br> tag, <b> tag are not.
<?xml version="1.0" encoding="utf-8"?>
<posts>
<post>
<msg>This is a <strong>message</strong></msg>
</post>
</posts>
<msg>This is a <strong>message</strong></msg>
When you type these tags(e.g. strong tag as above) in the XML, it simply ‘breaks’ the XML formatting and would thus caused an error when it is loaded with Spry
So… to input HTML data in XML…
1. Use CDATA wrapper in your XML
<msg>
<![CDATA[
This is a <strong>message</strong>
]]>
</msg>
2. Set setColumnType(“msg”, “html”) to your Spry dataset for that field
var ds1 = new Spry.Data.XMLDataSet("data.xml", "posts/post");
ds1.setColumnType("msg", "html");
Spry with HTML Data Source Code
Spry with HTML Data Online Demo
VN:F [1.9.15_1155]
Rating: 7.0/10 (1 vote cast)
VN:F [1.9.15_1155]