I have a table named GAINLP
The table contains fields
'Record#" (INT),
'SightingDate' (DATE),
'SpeciesName' (VARCHAR)
Need SQL to output an array that contains an integer that corresponds to the sum of SightingDate for each month.
Example: 0,0,0,0,1,5,10,12,5,3,0,0
Instead, the following code causes null value sums to be skipped and I'm left with 1,5,10,12,5,3
`select count(SightingDate) from GAINLP where SpeciesName LIKE '%Actias luna' GROUP BY MONTH(SightingDate)`
I understand that this can be done by joining with a calendar table, but I've not found examples of such code that also employs the WHERE operator.
You could use conditional aggregation here:
SELECT
MONTH(SightingDate) AS month,
COUNT(CASE WHEN SpeciesName LIKE '%Actias luna' THEN 1 END) cnt
FROM GAINLP
GROUP BY
MONTH(SightingDate);
But, this might not be as efficient as a calendar table based join approach. Here is how you might do that:
SELECT
t1.month,
COUNT(t2.SightingDate) cnt
FROM (SELECT DISTINCT MONTH(SightingDate) AS month FROM GAINLP) t1
LEFT JOIN GAINLP t2
ON t1.month = MONTH(t2.SightingDate)
WHERE
t2.SpeciesName LIKE '%Actias luna'
GROUP BY
t1.month;
Note: It might be possible that your data could span more than a given year, in which case you would probably want to report both the year and month.
You seem to be looking for conditional aggregation. The principle is to move the filtering logic to a CASE construct within the aggregate function.
SELECT
SUM(
CASE
WHEN SpeciesName LIKE '%Actias luna'
THEN 1
ELSE 0
END
)
GROM gainlp
GROUP BY MONTH(SightingDate)
Related
i have this table
table with evaluators and evaluatees.
Every row of the table above is a relationship between two people(First row:person a evaluates person b, Second row person a evaluates person c and so on).
I want to trasform this table into looking like this
Wanted Output
So Each row will correspond one evaluator and each column will be people being evaluated by him. (Because some evaluators have less people to evaluate than others the remaining columns for someone who doesnt have that many evaluatees will be NULL.
Many thanks hope you can help me out
Since you have SQL Server tag so, i would use row_number() function with conditional aggregation :
select evalutor,
max(case when seq = 1 then evalutee end) as evalutee1,
max(case when seq = 2 then evalutee end) as evalutee2,
max(case when seq = 3 then evalutee end) as evalutee3
from (select t.*, row_number() over (partition by evalutor order by evalutee) as seq
from table t
) t
group by evalutor;
I have a table that has different columns display different values.
I need to add a new column that displays sum of 1 column in each row of other column.
This is what i need to display.
I have written following query but its only displaying 1 in each row of last column.
select inStation.name TapInStation , outStation.name TapOutStation,
count(trx.passengerCount) PassengerCount, sum(trx.amount) Fare,
(select sum(passengerCount) from transactions iTrx
where iTrx.ID = trx.ID) PassengerPercent
from transactions trx
inner join
station inStation on inStation.ID = trx.fromStation
inner join
station outStation on outStation.ID = trx.toStation
GROUP BY
TapInStation, TapOutStation
If you want the total, then remove the correlation clause. This may do what you want:
select inStation.name as TapInStation , outStation.name as TapOutStation,
count(trx.passengerCount) as PassengerCount,
sum(trx.amount) as Fare,
(select sum(passengerCount) from transactions iTrx) as PassengerPercent
I'm not sure why you would called the "total" something like PassengerPercent, but this should return the overall total.
I also suspect that you might want a sum() for the previous expression.
alright this is my mistakes, I asked question that nobody would understand it, now I'm trying to make my question clearer and more simple.
Here I've 3 tables, first is employeeid contains (nameid, salary), second is overtimeid contains (nameid 'from employeeid', period, tot_ot), and the third is absenceid contains (nameid 'from employeeid', period, tot_absence).
how can I populate those three tables into one query containing (nameid, salary, period (fr overtimeid should be the same with absenceid), tot_ot, tot_absence).
please help me master of ms-access, I can't do wonder thing without your help,..... thanks before.
You can use query like this:
SELECT nameid, period, Sum(tot_absence) AS SumOftot_absence, Sum(tot_ot) AS SumOftot_ot
FROM
(SELECT nameid, period, tot_absence, Null AS tot_ot
FROM absenceid
UNION ALL
SELECT nameid, period, Null AS tot_absence, tot_ot
FROM overtimeid)
GROUP BY nameid, period;
Then outer join it with employeeid and you will receive the list of employees for each period
Edit
Query for tables from comments:
SELECT NMKARY, PERIODGJ, sum(JMLMBUR) as sumofJMLMBUR, sum(TOTABS) as sumofTOTABS, sum(KASBON) as sumofKASBON
FROM (
SELECT NMKARY, PERIODGJ, JMLMBUR, Null as TOTABS, Null as KASBON
FROM overtimeid
Union ALL
SELECT NMKARY, PERIODGJ, Null as JMLMBUR, TOTABS, KASBON
FROM potonganid
)
GROUP BY NMKARY, PERIODGJ;
I have below requirement in mysql/SQL Server
Table Name: basic(pid int,av int,sid int,st int,wid int,wt int)
For each pid, there would be 10 rows (containing sid,st values and wid,wt values for each pid). These sets could be from 1 to 10.
So, for a pid value (example: 3213 and 3214), there will be 10 rows like below
Like the above, there could be millions of records
What am trying to achieve is, I want to get the pid's whose (sid=2 and respective st>=7) and also whose (wid=9 and respective wt>=6)
If I apply this condition, I should get list of pid's which must have two pid's 3213 and 3214.
How can I achieve this using simple sql query or i can divide the table into three like basic1(pid,av), basic_sk(pid,sid,st) and basic_wc(pid,wid,wt)
since I can use pid as reference, I can join .. even I tried using joins, and couldn't achieve the required result.
I used below join -
select t1.pid from basic1 t2
inner join basic_sk t2 on t1.pid=t2.pid
inner join basic_wc t3 on t3.pid=t2.pid
where (((t2.sid=2) and (t2.st>=7)) and ((t3.wid=9) and (t3.wt>=6)))
but no luck.
How about if I have multiple sid and st values in where condition and wid and wt values..
like in sets {sid=2,st>=7} and {sid=4,st>=9}
and {wid=9,wt>=6} and {wid=5,wt>=5}
How can I achieve my requirement using simple sql query ?
Any possibility is fine for me, with one table or multiple tables (using join)
One method is to use aggregation and a having clause;
select b.pid
from basic b
group by p.pid
having sum(case when (b.sid = 2) and (b.st >= 7) then 1 else 0 end) > 0 and
sum(case when (b.wid = 9) and (b.wt >= 6) then 1 else 0 end) > 0;
Each condition in the having clause counts the rows that match each condition. The > 0 ensure that there is at least one row for each.
In MySQL, i have a table with a column full of positive integers and i want to filter out all the odd integers. It seems like there is nothing in the MySQL documentation. I tried the following query.
select kapsule.owner_name,
kapsule.owner_domain,
count(xform_action)
from kapsule, rec_xform
where rec_xform.g_conf_id=kapsule.g_conf_id
and (count(xform_action))%2=0
group by kapsule.owner_name;
I want to keep only those values where count(xform_action) is even. The table looks like this.
To filter out resultset after GROUP BY you need to use HAVING clause.
WHERE clause is used to filter source rows before GROUP BY occurs.
Try
SELECT k.owner_name,
k.owner_domain,
COUNT(x.xform_action) cnt -- < you probably meant to use SUM() instead of COUNT() here
FROM kapsule k JOIN rec_xform x -- < use JOIN notation for clarity
ON x.g_conf_id = k.g_conf_id
GROUP BY k.owner_name
HAVING cnt % 2 = 0
You probably meant to use SUM() (sums values of a column of all rows in a group) aggregate instead of COUNT() (returns number of rows in a group)
Here is SQLFiddle demo (for both SUM() and COUNT())
For aggregate functions like COUNT(*) using GROUP BY you need to use HAVING clause
select kapsule.owner_name, kapsule.owner_domain,
count(xform_action) from kapsule, rec_xform
where rec_xform.g_conf_id=kapsule.g_conf_id and
group by kapsule.owner_name, kapsule.owner_domain
HAVING (count(xform_action))%2=0
or you could use alias (i.e. AS) like:
select kapsule.owner_name, kapsule.owner_domain,
count(xform_action) count_form from kapsule, rec_xform
where rec_xform.g_conf_id=kapsule.g_conf_id and
group by kapsule.owner_name, kapsule.owner_domain
HAVING count_form%2=0
And you could use JOIN as more efficient than the old one of joining tables. And by the way
if you have GROUP BY the fields before the aggregate function should be in GROUP BY like:
select kapsule.owner_name, kapsule.owner_domain,
count(xform_action) count_form from kapsule A
INNER JOIN rec_xform B
ON A.g_conf_id=B.g_conf_id and
GROUP BY by A.owner_name, A.owner_domain
HAVING count_form%2=0
See examples here