Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All

Am new to this SQL.
How to count words in a column and sum it as a total.

Example:
Table1 contains

State  ||  crs1  || crs2  ||  crs3  || Capacity  ||  crs1_tar  || crs2_tar  ||  crs3_tar
AP     ||  25    ||  10   ||  0     ||  360      ||  Approved  || No        || No
TS     ||  10    ||  20   ||  2     ||  360      ||  no        || Approved  || Approved
MP     ||  21    ||  33   ||  2     ||  360      ||  no        ||  no       ||  no



Here crs1,2,3 is Course names and crs_tar is targets for the course.

Well, i need a consolidated data like the following:

TS        ||  crs1  ||  crs2  ||  crs3
Capacity  ||  0     ||  360   ||  360
Allocated ||  10    ||  20    ||  2  


Here Capacity is counted according to crs_tar.

I hope am clear.

What I have tried:

I have tried it in the excel sheet and need to convert it to SQL
Posted
Updated 22-Feb-16 6:34am
v5
Comments
CHill60 22-Feb-16 11:41am    
The "What I have tried" section is for you to put in the code you have already tried.
I am struggling to see how you have got to your "consolidated" data at all
Member 10376341 22-Feb-16 12:26pm    
Am sorry, i have tried it in the MS-Excel sheet and i have used SUMIFS() function with sum range and criteria range.
Maciej Los 22-Feb-16 12:49pm    
Based on what logic do you want to transpose rows to columns?
I have no idea how Capacity for crs1 become to zero. The same doubt for Allocated...

1 solution

It is difficult to quite get what you are trying to do. But it seems that you need to start by designing a more normalized database.

What are the entities (things) described?

(Courses would seem to be one, "State" may be another but that is not clear, and what is a "Target"?).

What does "Capacity" describe? Is it an attribute of a "State" or an attribute of a Course, or does it refer to something else? And what does it mean that a "Target" is Approved?

My guess is also that in a real world scenario, you would want to define the INSTANCES of a course as separate from the course description, so I'm guessing maybe this is a classroom exercise?

Represent each separate entity in a separate table, and show how they relate to each other.

Something like (just guess on what your intentions are):

Table: States
Columns:  StateID, StateName, Capacity
(1, 'AP', 360),
(2, 'TS', 360),
(3, 'MP', 360)


Table: Courses
Columns:  CourseID, CourseName, ...
(101, 'crs1', ...),
(102, 'crs2', ...),
(103, 'crs3', ...)


Intermediate table ties Courses and States together:

Table: StatesCourses
Columns:  StatesCoursesID, StateID, CourseID, Allocation, Approved
(1001, 1, 101, 25, 'Y'),
(1002, 1, 102, 10, 'N'),
(1003, 1, 103, 0, 'N'),
(1004, 2, 101, 10, 'N'),
(1005, 2, 102, 20, 'Y'),
(1006, 2, 103, 2, 'Y'),
(1007, 3, 101, 21, 'N'),
(1008, 3, 102, 33, 'N'),
(1009, 3, 103, 2, 'N')


Now you can get the following result showing allocated by course and state: State, Capacity, Course, Allocated using a query like this:

SQL
SELECT State, Capacity, Course, Allocated
FROM States as S
INNER JOIN StatesCourses as SC
ON SC.StateID = S.StateID
INNER JOIN Courses as C
ON C.CourseID = SC.CourseID


In order to get the specific layout you are looking for, you can either construct a custom query that only works for the currently known courses and states (not a very flexible solution), or you can look into PIVOT (or TRANSFORM) in your database's Help.


Until you get a handle on the relational data design, you may be better off sticking with keeping your data in a spreadsheet. That isn't necessarily a bad thing. Databases are great when you have a lot of data or it gets difficult to manage and you need more flexibility. But to get that flexibility, you need to have a reasonable design of the database. Search the web for Introduction to Relational Databases for additional help


HTH.
 
Share this answer
 
v2
Comments
Member 10376341 22-Feb-16 13:22pm    
Thank you for the answer. As you suggested, i shall do some research on the Relational Databases and get a grip on it.

Thank you for your time.
Saiekrishna

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900