So I have a table with a column that I need to group by certain categories within that column. For example there is 20 codes in the column that goes in one group called Residential and 30 codes that go in Commercial. Is this possible? How would I create groups made out of multiple different values in the same columns?
You could use a case expression:
SELECT code_group, COUNT(*)
FROM (SELECT CASE WHEN code IN ('code1', 'code2', 'etc') THEN 'Residential'
WHEN code IN ('code3', 'code4') THEN 'Commercial'
ELSE NULL
END AS code_group
FROM . . .) t
GROUP BY code_group
Related
I have table with the following fields
UserId, Username, Category, filledform
I want a query which should retrieve how many have filled the form categorywise.
Output should be something like this
Category Usercountfilled totalnumberofusers
I tried with the following query
Select Category, count(Category) from tablename where filledform !=' '
But I also want the totalnumberofusers of that category in the same query so that I know how many have filled the form and how many still pending category wise.
So filledform is a space when it is not filled and not a space when it is filled...
SELECT
category,
COUNT(CASE WHEN filledform !=' ' THEN 'x' END) as UserCountFilled,
COUNT(*) as TotalNumberOfUsers
FROM tablename
GROUP BY category
A few points:
case when will return 'x' when the filledform is not a space, and a null when it is. COUNT counts the Xs and ignores the nulls
use COUNT(*) not COUNT(category) unless category is NOT NULL - any nulls in category would cause that user not to be counted
please turn on ONLY FULL GROUP BY mode
Okay so I have two tables:
hscust and hssales_rep
I need to create a view that shows me the reps fname and lname (as well as the customers) and show how much the customer is over on there credit balance.
This is the code I have:
CREATE VIEW OverLimit AS
SELECT
CONCAT(hssales_rep.last,hssales_rep.first) AS Rep,
CONCAT(hscust.last,hscust.first) AS Cust,
SUM(credit_limit - balance)
FROM hscust
INNER JOIN hssales_rep ON hscust.sales_rep = hssales_rep.repid
And it returns an empty result.
Any help is greatly appreciated!
salesrep table
cust table
A CREATE VIEW statement doesn't return a resultset.
A SELECT statement can return an empty resultset. But we'd expect the SELECT statement in your view definition to return either a single row, or throw an error.
I suggest you break this down a bit.
1) What problem is being solved by the CREATE VIEW statement. Why do you need a view?
2) Before you write a CREATE VIEW statement, first develop and test a SELECT statement that returns the required resultset. Do that before you put that into a view definition.
I also strongly recommend that you qualify all column references in the SELECT statement either with the table name or (preferably) a short table alias.
If you want to return a row for each Cust with an aggregate function (e.g. SUM) in your SELECT list, then add an appropriate GROUP BY clause to your SELECT statement.
It's not clear why we would want to use a SUM aggregate function.
The difference between "credit_limit" and "balance" would be the available (remaining) credit. A negative value would indicate the balance was "over" the credit limit.
SELECT CONCAT(r.last,r.first) AS Rep
, CONCAT(c.last,c.first) AS Cust
, c.credit_limit - c.balance AS available_credit
FROM hscust c
JOIN hssales_rep r
ON c.sales_rep=r.repid
ORDER
BY CONCAT(r.last,r.first)
, CONCAT(c.last,c.first)
, c.custid
If we only want to return rows for customers that are "over" their credit limit, we can add a WHERE clause.
SELECT CONCAT(r.last,r.first) AS Rep
, CONCAT(c.last,c.first) AS Cust
, c.credit_limit - c.balance AS available_credit
FROM hscust c
JOIN hssales_rep r
ON c.sales_rep=r.repid
WHERE c.credit_limit - c.balance < 0
ORDER
BY CONCAT(r.last,r.first)
, CONCAT(c.last,c.first)
, c.custid
Again, get a SELECT statement working (returning the required resultset) before you wrap it in a CREATE VIEW.
Here is my brief snipt:
select * from tbl_test
What I want to do is:
1.
select g_size, sum(g_num)
from tbl_test
group by g_size
2.
select m_size, sum(m_num)
from tbl_test
group by m_size
Here is my question, how can I push all these stuffs into one single sql string?
Is it possible to do this?
If you want single row for eigher single g_size or m_size then you can't do in single sql, but if it is okay for you that there can be multiple row for 1st group by field then you can do-
For example if you have one more row g_size=5, g_num=1, m_size=2, m_num=1 and you use below query-
select g_size, sum(g_num),m_size, sum(m_num)
from mytable
group by g_size,by m_size;
then you will get 2 rows for g_size=5 as per below-
g_size= 5, sum(g_num)=5, m_size=1, sum(m_num)=30
and
g_size= 5, sum(g_num)=1, m_size=2, sum(m_num)=1
Further you can use with rollup function which will first provide sub-total (m_size wise) and then total (g_size wise) and at the end grand total (all m_size grand total).
Simply add the columns and the group by:
select g_size, sum(g_num),m_size, sum(m_num) from tbl_test group by g_size, m_size;
SELECT quantity, materialTypeId ,
(SELECT typeName
FROM invTypes
WHERE TypeID IN (SELECT materialTypeId
FROM invTypeMaterials
WHERE typeId= 12743
)
) AS material
FROM invTypeMaterials
WHERE TypeID=12743
so this query gives me nice results except the column material. only shows me the first entry instead of giving the name of each row.
if i run these sql seperate they work and i do see what i want. i just need them combined into 2 columns.
what i want to do is, i query one table for data, one of the column has a value wich i want to convert to a name, and that is in another table and its linked by a unique TypeID
Chilly
May be this will work :
SELECT tm.quantity, tm.materialTypeId , t.typeName
FROM invTypeMaterials tm
INNER JOIN invTypes t ON t.TypeID = tm.materialTypeId
WHERE tm.TypeID=12743
If you want to lookup the materialTypeID's name for the current record, you must not use a separate subquery but use the materialTypeID value from the outer query.
This is called a correlated subquery:
SELECT quantity, materialTypeId,
(SELECT typeName
FROM invTypes
WHERE TypeID = invTypeMaterials.materialTypeId
) AS material
FROM invTypeMaterials
WHERE TypeID=12743
I have a table containing the names, emails, positions, etc of a students, as well as their "status" (which can be one of Y or N.) I want to write a query that counts the number of each type of position, as well as the number of Y AND the number of N within each type using JOIN. (That is, it would be a table with three columns: Position, StatusIsYes, and StatusIsNo.)
I have already done this using the CASE clause the following way, but I can't figure out how to do it using the JOIN clause.
SELECT position,
COUNT(CASE WHEN status = 'Y' THEN 1 ELSE NULL END) AS StatusIsYes,
COUNT(CASE WHEN status = 'N' THEN 1 ELSE NULL END) AS StatusIsNo
FROM
students GROUP BY crd
I appreciate any suggestions!
EDIT: I know it can be done without using JOIN, but I want to know how it is possible to do it with a JOIN.
You don't need a join:
SELECT
position,
SUM(status = 'Y') AS StatusIsYes,
SUM(status = 'N') AS StatusIsNo
FROM students
GROUP BY position
Note the rather funky dispensing of the CASE, because in mysql (only) true is 1 and false is 0, so sum() of a condition counts how many times it is true :)
You can use SELF JOIN in the case when you want to fetch records from same table.
For ex:
Table Name: employee
Fields : EmpId,EmpName,ManagerId
Now if you want to get the details of Empolyees who are in Manager Position for that we need to write query like this:
SELECT e1.EmpId, e1.EmpName FROM EmployeeDetails e1, EmployeeDetails e2 where e1.EmpId=e2.ManagerId;
Hope it will help you.
Fro more information please check this link.
Try ::
SELECT
position,
COUNT(status = 'Y' ) AS StatusIsYes,
COUNT(status = 'N' ) AS StatusIsNo
FROM
students GROUP BY POSITION