|
Hi All,
I have a table as Marks, depending upon their marks they got I have to count how many of them got failed, passed and distinction and another table is Class which specifies the class.
Class table has values like
Can anybody please help me in achieving this structure in SQL Server?
<h2>Id | Class</h2>
1 | 7th Class
2 | 8th Class
4 | 10th Class
Marks Table has values as
StudentId | ClassId | Marks |
1 | 1 | 35
2 | 1 | 90
3 | 2 | 75
4 | 2 | 30
5 | 4 | 99
6 | 4 | 25
7 | 4 | 36
8 | 4 | 70
9 | 4 | 65
10 | 4 | 55
Now the resulting table should look as below
Class | Failed | Passed |Distinction
7 th Class count count count
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
You need to define what counts as a fail, a pass, and a distinction.
You also need to specify which DBMS you're using.
Assuming a recent version of Microsoft SQL Server, something like this should work:
DECLARE @DistinctionThreshold int = 90;
DECLARE @PassThreshold int = 70;
WITH cteCounts As
(
SELECT
ClassId,
SUM(CASE
WHEN Marks < @PassThreshold THEN 1
ELSE 0
END) As Failed,
SUM(CASE
WHEN Marks >= @PassThreshold And Marks < @DistinctionThreshold THEN 1
ELSE 0
END) As Passed,
SUM(CASE
WHEN Marks >= @DistinctionThreshold THEN 1
ELSE 0
END) As Distinction
FROM
dbo.Marks
GROUP BY
ClassId
)
SELECT
C.Class,
M.Failed,
M.Passed,
M.Distinction
FROM
dbo.Class As C
INNER JOIN cteCounts As M
ON M.ClassId = C.ClassId
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
CREATE TABLE Class
(
Id BIGINT PRIMARY KEY
,Name VARCHAR(50)
)
INSERT INTO Class (Id, Name) VALUES (1, '7th Class')
INSERT INTO Class (Id, Name) VALUES (2, '8th Class')
INSERT INTO Class (Id, Name) VALUES (4, '10th Class')
CREATE TABLE Marks
(
StudentId BIGINT,
ClassId BIGINT,
Marks BIGINT
)
INSERT INTO Marks (StudentId, ClassId, Marks) VALUES (1, 1, 35)
INSERT INTO Marks (StudentId, ClassId, Marks) VALUES (2, 1, 90)
INSERT INTO Marks (StudentId, ClassId, Marks) VALUES (3, 2, 75)
INSERT INTO Marks (StudentId, ClassId, Marks) VALUES (4, 2, 30) Try something like
SELECT c.Name,
(SELECT COUNT( * ) FROM Marks WHERE ClassId = c.Id AND Marks >= 50) AS [Passed],
(SELECT COUNT( * ) FROM Marks WHERE ClassId = c.Id AND Marks < 50) AS [Failed]
FROM Class c
If you try the query here[^], you'll not just see the actual result, but also get timings and a nice execution plan.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
How to compare same database with different version inside visual studio. I have one project of database, it has different versions. I have to compare any two different version.
Thanks
|
|
|
|
|
Presuming you are using SQL Server (you do not tell us what database) you need to compare the schemas of the 2 databases by looping through the various sysobject and views. There are also a number of tools that will do the job for you, some of them free!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes Mycroft I am using SQL Server, let me try your idea.
Thanks
|
|
|
|
|
I am having the value in column 1 - 12 digits saved as per the selected month, While fetching the data I need to show the month name in select statement.. Please help ASAP Need DB2 Query
SELECT (SELECT MAX(BAUKID) FROM CRPDTA.F58SM039 ) AS RMAX
,BAUKID AS ID
,BADO AS DOCODE
,BAAA AS AMOUNT
,BAOBSZ AS NOOFJOBCODE
,BAS1FU AS EMPPOSNAME
,BAFB03 AS EMPCATEGORY
,BAMNTH AS MONTH ---(Here I need month name ,BAMNTH this contain any number in 1 to 12 )
FROM CRPDTA.F58SM039
GBD
|
|
|
|
|
Is this DB2 running on a mainframe (or m/frame included in any cross-platform stuff) or is it on a PC/Server platform?
|
|
|
|
|
Hello? Is there anybody there?
Which version of DB2 are you running? (It is relevent to how this could be answered)
|
|
|
|
|
I'm probably in the wrong forum,
But I made a huge mistake in a table design for Billing and Shipping addresses in relation to card processing.
I made a single column called attention for the name. But card processors want a first and last name for Billing and Shipping, and a single column for card name.
Opps!
So now I have all these names in a single column.
Name like
Thomas Friedman Sr.
Loopy Jose De SannaAna
Margret Keeneman-Hassel
Kenneth G. Tompson Jr.
So I can do a split in vb.net, and split them up, but I'm not sure how to place them.
Just looking for ideas before I write any code to do the job.
Perhaps just take the first set of chars, and that's the first name, and then the rest is the last name?
|
|
|
|
|
IMHO it is a issue you can not resolve with code...
Human names - and the way they are written down - are reflecting the human brain - lack of any order...
As long as you have only one whitespace in the name you can simply split it, but what should you do with those with more than two parts in it?
There is no existing algorithm to take them apart...(And if you add international customers, you are in deep...)
What I would advise is:
1. Change the design of the tables, when the existing name will go into new 'firstname' column for the beginning...
2. Create logical groups of the names and
2.a. Split the simples
2.b. Check for those with Mr., Msr., Jr. and so in it and split them
2.c. Scan remained names for groups and handle them individually
2.d. Handle manually the rest (hopefully not much)
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
It's good to get a 2nd opinion on that, and you confirmed it.
I did change the design to let the customer choose what is the first and last name, sounds dumb but practical.
So I did a split and select or switch case 2, 3 and 4 - first last, first (1) last, first (1+) (1+), first (1) last (SR-JR)
So out of 5000, I only had 30 Mexican names, that i did by hand.
Thanks for the confirmation!
|
|
|
|
|
You're welcome...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
i created a db through script and now i want to restore in a new DB on a new server.
i need method.
|
|
|
|
|
Would that be MySQL, or another database-server?
If it is a MySQL-script with create and insert-statements, you'll only need to execute it. If it is a SQL Server backup, you'd need to use the restore command.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
yes it is in Sql Server 2008r2. kindly share the restore commands with complete procedure.
|
|
|
|
|
Restore[^] on MSDN.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I painted myself into a corner here. Was going to do a join, but realized I had nothing to join.
Is it possible to get this result in 1 set?
SELECT
cc.ID
, cc.CustomerID
, cc.SessionID
, cc.LoginID
, cc.CardLabel
, cc.CardBrand
, cc.CardName
, cc.CardNumber
, cc.ExpMonth
, cc.ExpYear
, cc.CVV2
, ba.Name1
, ba.Name2
, ba.CompanyName
, ba.StreetAddress1
, ba.StreetAddress2
, ba.City
, ba.StateCode
, ba.PostalCode
, ba.CountryCode
, ba.Phone
, sa.Name1
, sa.Name2
, sa.CompanyName
, sa.StreetAddress1
, sa.StreetAddress2
, sa.City
, sa.StateCode
, sa.PostalCode
, sa.CountryCode
, sa.Phone
FROM CUSTOMER_CARDDATA cc
WHERE cc.ID = @CC_ID
FROM CUSTOMER_BILLING_ADDRESS ba
WHERE ba.ID = @BA_ID
FROM CUSTOMER_SHIPPING_ADDRESS sa
WHERE sa.ID = @SA_ID
<pre>
|
|
|
|
|
If there is no relationship between the tables then that would not be possible unless you create one or you are getting only 1 record from each table, then you could use a cross join
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
The only relationship is the master order table, but it can be different in 3 scenarios
I'll check out the cross join.
Last night I just used one of the master tables, and wrote 2 functions.
|
|
|
|
|
If each of the three resultsets contains exact the same columns, then you can use the UNION keyword.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If there's exactly one matching record to each other and they're in the same order you could use the ROWID to join them / to update the tables without ID with the ID from CUSTOMER_CARDDATA.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Can we install any version of sql server on Windows 8.1?
|
|
|
|
|
Probably not "any", but any recent Express version should do.
Full specs here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
How to use SQL Server in Windows 8, Windows 8.1, Windows Server 2012, and Windows Server 2012 R2[^]:
- SQL 2005: Not supported.
- SQL 2008: Needs at least SP3.
- SQL 2008 R2:
- Needs at least SP2 on Windows 8.1 / Server 2012 R2;
- Needs at least SP1 on Windows 8 / Server 2012;
- SQL 2012:
- Needs at least SP1 on Windows 8.1 / Server 2012 R2;
- No SP required for Windows 8 / Server 2012;
- SQL 2014: No SP required.;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|