Count number of occurrences by Name [closed] - html

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First timer here so please excuse me if my question is somewhat confusing. I am attempting to display things in a dynamic table using data pulled from a SQL table and I am having difficulty figuring out the logic.
The table structure looks something like this
NAME Homeruns Hits Bunts Total
Jeff 0 3 1 4
Sally 2 4 0 6
John 3 7 0 10
The data in the table is structured in a way that the type of play being made (Home run, hit, bunt, etc) is in a single column. I'll call this column PLAY. The name is in a separate column. This column will be called NAME. Table name is BASEBALL.

This is called a pivoting query. You can do this in standard SQL with aggregation:
select name,
sum(case when play = 'HomeRun' then 1 else 0 end) as HomeRun,
sum(case when play = 'hit' then 1 else 0 end) as Hit,
sum(case when play = 'bunt' then 1 else 0 end) as Bunt,
count(*) as Total
from baseball bb
group by name;
This is a pretty simple SQL query, so I'm guessing your expertise is less on the database side than on the programming side. I would suggest that you take the time to learn the SQL language properly.

Related

Finding First Instance of Multiple Variable in Single Column in SQL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I've been trying to write some queries and subqueries but have struggled with one in particular.
Using the database at the below link, I am trying to find a way to separate multiple variables from a single column in to two.
https://www.w3resource.com/sql-exercises/soccer-database-exercise/subqueries-exercises-on-soccer-database.php
I can return the first match with a player scoring a goal from a penalty
`
SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringpenalty FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'P'
GROUP BY a.player_id`
Player_ID
Firstmatchscoringpenalty
160333
2016-06-26
160235
2016-07-03
and the first match in which a player scored a "normal" goal`
`SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringnormal FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'N'
GROUP BY a.player_id`
Player_ID
firstmatchscoringnormal
160236
2016-06-27
160316
2016-07-01
I would, however, like to return a result for all players who scored both a penalty and normal goal that includes both results across three columns, so something like:
Player_ID
Firstmatchscoringpenalty
firstmatchscoringnormal
Cell 1
Cell 2
Cell 3
Cell 4
Cell 5
Cell 6
Any ideas on how to do this would be much appreciated.

count all records columns with value 0 or 1 in mysql/php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Please help me friends. I have attached a screenshot of my table I wish to count all columns with value 0 or 1 where buno="$busno".
Sorry But I don't have any code. I have database table and I don't know how to query at such condition. In the table you can see fields busno with seat A1,A2,A3,A4,B1,B2......N1,N2,N3 and N4. They have only 2 values 1 and 0. 0 for vacant and 1 for booked. I wish to find out how many seats are left (in numbers like 10 0r 20) for a particular busNo. Are you getting my problem?
Get the result row in PHP with the query buno = "$busno", and iterate resultset in php and count the number you want.

SQL Query conditional row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table called 'list' that is like this:
system item1 exception
------------------------
A John 1
A Sarah 0
A Tim 1
A Blake 0
B Nikki 1
B Rick 0
C Jimbo 1
I am trying to build a query and I'm so terrible at it and I'm stuck. I want to return all rows unconditionally UNLESS system = 'A'. If system = 'A' then only return the rows where exception is true.
Thanks for your time.
What about this?
SELECT * FROM table WHERE system != 'A' OR exception = 1
SQL FIDDLE DEMO
select *
from list
where (system = 'A' and exception = 1)
or system <> 'A'
You can group conditions in the WHERE clause by enclosing each block in ()
http://www.techonthenet.com/sql/and_or.php

MYSQL SUM Value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 table :
table structure for "kegiatanrenja"
idkegiatan tahun koderekening detail
renja001 2001 1.03.03.04.1 A
renja002 2001 1.03.03.03.2 B
renja003 2001 1.08.08.05.3 C
renja004 2001 1.08.08.05.2 D
table structure for "sumberdana"
idkegiatan nilai kodesumberdana
renja001 100 1
renja002 200 2
renja003 50 1
renja004 100 1
I want to create a query that displays the value of all the columns "nilai" if MID (koderekening, 9,2) the same value, along with the value of another column, where each row in the group by where each row in the group by MID (koderekening, 9,2).
kode rekening nilai_group(SUM Of value the same kode MID(koderekening,9,2)
1.03.03.04 300
1.08.08.05 150
please help, every suggestion will be very appreciated.
SELECT LEFT(koderekeneing,0,10), sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
GROUP BY MID(koderekening,9,2)
UNION
SELECT 'ALL', sum (nilal)
FROM kegiatanrenja k
JOIN sumberdana s ON s.idkegiatan = k.idkegiatan
Please note, this is VERY easy, so if you are asking this sort of question on stack lot, you are going to receive a negative response. Please get yourself a good SQL book and do some studying.

Executing "simple" sql query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Here is data in my table :
id name surname place
1 test1 isdad YES
2 test2 spreda YES
3 test3 me NO
4 test4 smallvile YES
What I'd like to get, all ids where place='YES'. So Ids are
1
2
4
and the result count is 3. I want to append that number to the end of the results, so the result should look like this:
1
2
4
3
There is almost definitely a better way to do this. That said, you could do:
SELECT id FROM myTable WHERE place = 'YES'
UNION ALL
SELECT COUNT(*) FROM myTable WHERE place = 'YES'
UNION ALL will append the result number as a new row rather than attempting to add it as another column.
Please don't do this; the results will be mixed, it stands in stalwart defiance of good software design and will be baffling to anyone who comes along later and has to work on this piece of code.
In MySQL, you can do something similar to this in a single query using WITH ROLLUP:
select id, count(*)
from myTable
where place='YES'
group by id
with rollup
SELECT id FROM TABLE WHERE place='YES'
will return with the ids what you want, and the rowcount will store the count number, if you use this with php or c++/c#