I have a table that keeps track of transactions.
The table is setup as:
transactions:
id, account_id, budget_id, points, type
I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'
I know how to do each, but not both in one query.
expected result set:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
SELECT budget_id,
SUM(IF(type = 'allocation', points, 0)) AS allocated,
SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
select budget_ID,
sum(case when type = 'allocated' then points else 0 end) as allocated,
sum(case when type = 'issued' then points else 0 end) as issued
..rest of your query...
group by budget_ID
Cases can be used to sum only when a certain criteria is met.
Related
I have a table that keeps track of transactions.
The table is setup as:
transactions:
id, account_id, budget_id, points, type
I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'
I know how to do each, but not both in one query.
expected result set:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
SELECT budget_id,
SUM(IF(type = 'allocation', points, 0)) AS allocated,
SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
select budget_ID,
sum(case when type = 'allocated' then points else 0 end) as allocated,
sum(case when type = 'issued' then points else 0 end) as issued
..rest of your query...
group by budget_ID
Cases can be used to sum only when a certain criteria is met.
I have this table:
And I need:
Where:
Complete have all records with same _legajo and _count > 0
Zero have all records with same_legajo and _count = 0
Track have all records with same _legajo, some records with _count 0 and some with _count >
My table is 20k records
I working on PHP and MySQL
some ideas?
You could first select MIN and MAX for each _legajo:
SELECT _legajo, MIN(_count), MAX(_count)
FROM signups
GROUP BY _legajo;
If MAX is zero, then it naturally belongs to the zero category. If MIN is larger than zero then it belongs to complete category (MAX is naturally larger than zero then). If MIN is zero and MAX isn't, then it's in track.
Then you can just use SUM to determine the counts with the conditions:
SELECT
SUM(CASE WHEN min>0 THEN 1 ELSE 0 END) as complete,
SUM(CASE WHEN max=0 THEN 1 ELSE 0 END) as zero,
SUM(CASE WHEN min=0 AND max>0 THEN 1 ELSE 0 END) as track
FROM (SELECT _legajo, MIN(_count), MAX(_count)
FROM signups
GROUP BY _legajo) a;
It confuses me.. total left calculation seems doesn't work.
I am trying to get the total voucher and get total used and the total left.
please help.
SELECT
(IFNULL(SUM(value), 0)) AS total_voucher,
(
SELECT
IFNULL(SUM(value), 0))
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 1 AND DATE(FROM_UNIXTIME(datetime)) = '2014-03-04'
) AS total_used,
(total_voucher-total_used) AS total_left
FROM
voucher_history
WHERE
idUser = 1 AND isUsed = 0 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05'
You can do this with conditional aggregation, rather than using a subquery:
SELECT coalesce(SUM(value), 0)) AS total_voucher,
sum(case when is_used = 1 then value else 0 end) as total_used,
sum(case when is_used = 1 then 0 else value end) as total_left
FROM voucher_history
WHERE idUser = 1 AND DATE(FROM_UNIXTIME(datetime)) <= '2014-03-05';
Your query has the problem that it is trying to use column aliases (total_voucher and total_used) in the same select statement. SQL does not support this. You would need to use a subquery to get that functionality.
The scenario is I have a column named "States" in a table, States can be 0,1,2,3 or 4. What I want to do is get a count of each state using WHERE State = in a single query.
The main purpose is I want to show the count of records (identified by their state). Like this, 20 records have State 0 etc.
Is this possible? If yes, then how can I achieve this?
Edit: I know about Count. Here's what I have tried:
SELECT State, Date_Created, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=1) as State_One, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=2) as State_Two, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=0) as State_Zero, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=4) as State_Four FROM [ECOS].[eco].[tb_projects_details] WHERE Date_Created < dateadd(week,-3,getdate());
If I understand it correctly, you want to group and count:
SELECT mt.States, COUNT(*) total
FROM my_table mt
GROUP BY mt.States
To generate a list of values and their counts you can do this:
SELECT State, COUNT(*) AS C
FROM mytable
GROUP BY State
To generate one row that contains value counts as columns you can do this:
SELECT
COUNT(CASE State WHEN 0 THEN 1 ELSE NULL END) AS State_0_Count,
COUNT(CASE State WHEN 1 THEN 1 ELSE NULL END) AS State_1_Count,
COUNT(CASE State WHEN 2 THEN 1 ELSE NULL END) AS State_2_Count,
COUNT(CASE State WHEN 3 THEN 1 ELSE NULL END) AS State_3_Count,
COUNT(CASE State WHEN 4 THEN 1 ELSE NULL END) AS State_4_Count
FROM [...]
WHERE [...]
It will be very easy if you use group by clause after where condition. Then you will get the number of each state very easily.
SELECT state, count(*)
FROM table_name
GROUP BY state
Next time read a sql for beginers book first.
Select state, count(*)
from table
group by state
I have a table with a field called type, where 3 various rows are possible: 1, 2 and 3.
Now, I don't care about 3 at all. I need to count how many rows there are with type = 1 and with type = 2. I am doing this with 2 queries, like this:
Query1: SELECT COUNT(id) as count FROM users WHERE type='1'
Query2: SELECT COUNT(id) as count FROM users WHERE type='2'
Can I do this with only 1 single query? If so, should I, or not? How would the query look?
SELECT type,
COUNT(id) AS count
FROM users
WHERE type IN ('1','2')
GROUP BY type
SELECT type, COUNT(id) AS count
FROM users
GROUP BY type
HAVING type < 3
SELECT sum(case when type = '1' then 1 else 0 end) as count_for_one,
sum(case when type = '2' then 1 else 0 end) as count_for_tow
FROM users
WHERE type IN ('1','2')
If you want separate numbers,
SELECT SUM(IF(type='1', 1, 0)), SUM(IF(type='2', 1, 0))
FROM users WHERE type IN ('1', '2')