I have a query that counts records based on various criteria, but I would like to split the results into two grouped fields based on said criteria. Is there a way to do this inside the query - or is it necessary to have two queries?
select count(PT_TASK_ASSAY_DISP) as 'Count' from vbasedata v
where
v.PT_TASK_ASSAY_DISP like 'SS%' or
(v.PT_TASK_ASSAY_DISP like 'IP%' and
v.PT_TASK_ASSAY_DISP not like 'IP CUT' and
v.PT_TASK_ASSAY_DISP not like 'IP NEG');
I'm trying to group by IP and SS to get a total count.
I am not really sure what you want. But if you want one count per 'SS%' and one for 'IP%'. Then maybe something like this:
select
SUM(CASE WHEN PT_TASK_ASSAY_DISP LIKE 'SS%' THEN 1 ELSE 0 END) as SSCount,
SUM(CASE WHEN PT_TASK_ASSAY_DISP LIKE 'IP%' THEN 1 ELSE 0 END) as IPCount
from vbasedata v
where
v.PT_TASK_ASSAY_DISP like 'SS%' or
(v.PT_TASK_ASSAY_DISP like 'IP%' and
v.PT_TASK_ASSAY_DISP not like 'IP CUT' and
v.PT_TASK_ASSAY_DISP not like 'IP NEG');
Related
I have the following tables available through this link:
tables.
I'm trying to do a query where I list out the degrees (profession) and the counts associated with that profession. If the degree is listed more than twice, it should output 'Popular Degree'. I coded:
select profession, count(profession_id), count(profession_id) > 2 as Popular
from Profession
where profession_id in
(select profession_id from provider_profession)
group by profession
I get an output of this, which is wrong:
My query result
I am not sure how to query the result.
replace count(profession_id) > 2 as Popular with (case when count(profession_id) > 2 then 1 else 0 end) as Popular
Try this:
select p.profession,
count(pp.provider_id),
case
when count(pp.provider_id)>2 then 'Popular degree'
else ''
end as DegreePopularity
from profession p inner join provider_profession pp on p.profession_id=pp.profession_id
group by p.profession
I have tried to keep the answer as close to ANSI sql as possible, though only tested it out on a sql server
I need to verify that a list of information given to me contains the information that has at least two items - engine (starts with either 'E' or 'PE') - and one another item - chief unit (starts with 'BC').
My initial query was this and I need help modifying it to show me the correct data.
select distinct IncidentNum, Unit
from unit
where (unit like 'E%'
or unit like 'PE%'
or unit like 'BC%')
and unit not like 'EMS%'
and IncidentNum = '19-00001912'
group by incidentnum, unit
having count(*) > 2
You can use conditional aggregation to find the incidentnums that match the conditions:
select IncidentNum
from unit
group by IncidentNum
having sum(case when unit like 'E%' or unit like 'PE%' then 1 else 0 end) >= 2 and -- meets P/E condition
sum(case when unit like 'BC%' then 1 else 0 end) > 0 and -- meets BC condition
You can modify the having clause to get the inverse -- the ones that don't match both conditions (= 0 or = 0).
I do not know what these conditions are for:
unit not like 'EMS%'
IncidentNum = '19-00001912'
Because they are not part of your question.
I've got a query in Bigquery which i'd like to pivot the output on Project Site Name and Study ID.
This is the code:
SELECT
Project_Site_Name,
NIHR_Portfolio_Study_ID,
CASE WHEN (Recruitment_Year = '2017/18')
THEN COUNT(Patient_Local_Number) END AS rec1718,
CASE WHEN (Recruitment_Year = '2018/19')
THEN COUNT(Patient_Local_Number) END AS rec1819
FROM `fourth-jigsaw-118116.Partners.PARData`
WHERE
NIHR_Portfolio_Study_ID = 1358 AND
Recruitment_Year IN ('2017/18', '2018/19')
GROUP BY
Project_Site_Name,
NIHR_Portfolio_Study_ID,
Recruitment_Year
Currently, this is the output:
And I'd like it to look like this:
I've been searching for a solution for some time now, I think it might have something to do with nesting the aggregation in a subquery but that's about as far as i've got. Any suggestions?
best wishes
Dave
If you want to pivot out result columns based on the recruitment year, then you should not be grouping by that column. Instead, use conditional aggregation to determine the counts for each recruitment year.
SELECT
Project_Site_Name,
NIHR_Portfolio_Study_ID,
COUNT(CASE WHEN Recruitment_Year = '2017/18' THEN 1 END) AS rec1718,
COUNT(CASE WHEN Recruitment_Year = '2018/19' THEN 1 END) AS rec1819
FROM fourth-jigsaw-118116.Partners.PARData
WHERE
NIHR_Portfolio_Study_ID = 1358 AND
Recruitment_Year IN ('2017/18', '2018/19')
GROUP BY
Project_Site_Name,
NIHR_Portfolio_Study_ID;
I have the following query where if brand1/camp1 taken individually, query returns the correct value but if I specify more than one brand or campaigns, it returns some other number and I am not sure what the math is behind that. It is not the total of the two either.
I think it is IN operator that is specifying OR with "," as opposed to what I require it to do which is consider AND
select campaign,
sum(case when campaign in ('camp1', 'camp2') and description in ('brand1', 'brand2') then orders else 0 end) as brand_convs
from data.camp_results
where campaign in ('camp1', 'camp2') and channel='prog' and type='sbc'
group by campaign
having brand_convs > 0
order by brand_convs desc;
Any thoughts?
The problem is in the IN part as you suspected: The two IN operators do not affect eachother in any way, so campaign can be camp1 while description is brand2.
If your DBMS supports multiple columns in an IN statement, you use a single IN statement:
SELECT campaign, SUM(
CASE WHEN (campaign, description) IN (
('camp1', 'brand1'),
('camp2', 'brand2')
) THEN orders ELSE 0 END
) [rest of query...]
If not, you're probably going to have to use ANDs and ORs
SELECT campaign, SUM(
CASE WHEN
(campaign='camp1' AND description='brand1')
OR (campaign='camp2' AND description='brand2')
THEN orders ELSE 0 END
) [rest of query...]
Writing an SQL query to get a device count out of spiceworks.
I need to get the device type, the model, how many are in use and how many are unassigned.
Currently anything thats unassigned is marked as decommissioned.
So I have the following query
SELECT `device_type` AS "Device",
`model` AS "Model",
SUM (CASE WHEN `user_tag` NOT LIKE "%decommissioned%" THEN 1 ELSE 0 END) AS "Assigned",
SUM (CASE WHEN `user_tag` LIKE "%decommissioned%" THEN 1 ELSE 0 END) AS "Avail."
FROM `devices`
WHERE `auto_tag` LIKE "%LA2%" OR `user_tag` LIKE "%LA2%" OR `location` LIKE "%LA2%"
Group by `device_type`, `model`
EG: if marked as decommissioned count as 1 toward available, otherwise count as 1 toward assigned.
My current problem is that the Second SUM opperation is returning no data. Not even a 0.
Im sure its something simple that im missing.
Thanks in advance for the help.
It came down to SpiceWorks choking on the formatting of the query.
the back ticks were removed from around column names, double quotes replaced with single quotes.
Works as expected.
Thank you all for your assistance.
SELECT
device_type
, model
, COUNT(CASE WHEN user_tag NOT LIKE '%decommissioned%' THEN 1 ELSE NULL END) as 'Assigned'
, COUNT(CASE WHEN user_tag LIKE '%decommissioned%' THEN 1 ELSE NULL END) as 'Avail'
, COUNT(*) as 'TotalItems'
FROM devices
WHERE auto_tag LIKE '%LA2%' OR user_tag LIKE '%LA2%' OR location LIKE '%LA2%'
GROUP BY model, device_type
ORDER BY location DESC, device_type ASC