Important alert: (current site time 7/15/2013 7:33:04 PM EDT)
 

VB icon

Advanced Crosstab Query

Email
Submitted on: 2/13/2007 10:51:20 AM
By: Sukrisno 
Level: Advanced
User Rating: By 1 Users
Compatibility: SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier
Views: 11837
author picture
(About the author)
 
     halo friends, i have some scripts for you. the case like this, for example, you have 2 tables, students and countries. students have id, name, year of start and country id, countries have id and name. if you wanna to see the students distribution group by country name, and the statistics of year, you can use the crosstab query i create. it will return the result like the screenshot. ou can altering the query to view the statistics from any year, like this: (if u use MySQL): select country_name, sum(if(year_of_start=2000,1,0)) AS '2000', sum(if(year_of_start=2001,1,0)) AS '2001', sum(if(year_of_start=2002,1,0)) AS '2002', sum(if(year_of_start=2003,1,0)) AS '2003', sum(if(year_of_start=2004,1,0)) AS '2004' from students left join countries on students.country_id=countries.country_id group by country_name; if you use Access, you must replace "if" statement with "iif" but if you use SQL Server, like this: select country_name, sum( case year_of_start WHEN 2000 THEN 1 END) AS '2000', sum( case year_of_start WHEN 2001 THEN 1 END) AS '2001', sum( case year_of_start WHEN 2002 THEN 1 END) AS '2002', sum( case year_of_start WHEN 2003 THEN 1 END) AS '2003', from students left join countries on students.country_id=countries.country_id group by country_name;

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Advanced Crosstab Query
-- Description:halo friends, i have some scripts for you.
the case like this, for example,
you have 2 tables, students and countries.
students have id, name, year of start and country id, 
countries have id and name.
if you wanna to see the students distribution group by country name,
and the statistics of year, you can use the crosstab query i create.
it will return the result like the screenshot. ou can altering the query
to view the statistics from any year, like this:
(if u use MySQL):
select country_name,
sum(if(year_of_start=2000,1,0)) AS '2000',
sum(if(year_of_start=2001,1,0)) AS '2001',
sum(if(year_of_start=2002,1,0)) AS '2002',
sum(if(year_of_start=2003,1,0)) AS '2003',
sum(if(year_of_start=2004,1,0)) AS '2004'
from students 
left join countries
on students.country_id=countries.country_id
group by country_name;
if you use Access, you must replace "if" statement with "iif"
but if you use SQL Server, like this:
select country_name,
sum( case year_of_start WHEN 2000 THEN 1 END) AS '2000',
sum( case year_of_start WHEN 2001 THEN 1 END) AS '2001',
sum( case year_of_start WHEN 2002 THEN 1 END) AS '2002',
sum( case year_of_start WHEN 2003 THEN 1 END) AS '2003',
from students 
left join countries
on students.country_id=countries.country_id
group by country_name;
-- By: Sukrisno
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1123&lngWId=5--for details.--**************************************

create table students
(
student_id char(10) primary key,
name varchar(25),
year_of_start int(4),
country_id int(2)
);
create table countries 
(
country_id int(2) primary key,
country_name varchar(25)
);
insert into countries values (1,'Pemangkat');
insert into countries values (2,'Sambas');
insert into countries values (3,'Singkawang');
insert into students values('03.01.1698','Sukrisno',2000,1);
insert into students values('03.01.1699','Samuel',2000,1);
insert into students values('03.01.1700','Johny',2000,2);
insert into students values('03.01.1701','James',2000,3);
insert into students values('03.02.1800','Jacques',2001,1);
insert into students values('03.02.1801','Harry',2001,2);
insert into students values('03.02.1802','Wardati',2001,3);
insert into students values('03.03.1901','Jenny',2002,2);
insert into students values('03.03.1902','Evita',2002,2);
insert into students values('03.03.1903','Rahayu',2002,3);
select country_name,
sum(if(year_of_start=2000,1,0)) AS '2000',
sum(if(year_of_start=2001,1,0)) AS '2001',
sum(if(year_of_start=2002,1,0)) AS '2002'
from students 
left join countries
on students.country_id=countries.country_id
group by country_name;


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/21/2007 2:31:44 AMCeeMc

Hi
I was confused with the if and iif in tsql and access. Thank you very much for your kindness and help to ez my work.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.