Put count and group by in one statement - sql

I am extracting 3 values from a table, I can extract these values from 3 statements. But I need to put these values in one table so I plan to do it in one statement.
select count(*) from fruit;
select count(*) from fruit where color = 'red';
select count(*) from fruit
where color = 'red' and substring(city, 1, 8) = 'New York';
What I am trying to do is similar like this:
select
count(*) total_items,
(count(*) where color = 'red') red_items,
(count(*) where color = 'red' and substring(city, 1, 8) = 'New York') fruit_in_newyork
from
fruit
New Table will have total_items, red_items, fruit_in_newyork as columns.

You could do a conditionally SUM
SELECT count(*) total_items
,sum(CASE WHEN color = 'red'
THEN 1
ELSE 0
END) AS red_items
,sum(CASE WHEN color = 'red' AND SUBSTRING(city, 1, 8) = 'New York'
THEN 1
ELSE 0
END) AS fruit_in_newyork
FROM fruit

Related

How can i sum(color = ‘red’) in postgresql

i run sql on pgadmin but this query is error
Select sum(color = ‘red’) from table
How can i query sum of color is red on postgresql and not like this:
Select count(color) from table where color = ‘red’
You can't sum a string... but I think what you want is:
Select sum(CASE WHEN color = ‘red’ THEN 1 ELSE 0 END)
from table
You can use:
Select sum( (color = 'red')::int )
from t;
Or using filter:
select count(*) filter (where color = 'red')
from t;

Sort out IDs when hitting a case

How to sort out ID´s when i have a table like
CAR
ID COLOR
1 Red
1 Black
2 BLUE
2 Black
3 Yellow
3 Red
My goal is, that all IDs who have the color "Red" get thrown out, so that just ID = 2 is my result. With my SQL i can find the IDs which have the Color Red with Filter = 1 but they still appear because ID = 1 can also have Black(or ID = 3 can have yellow) so that Filter = 0 and gets passed.
with SORTOUT as
(
ID,
CASE WHEN
COLOR = RED THEN 1
ELSE 0
END AS FILTER
FROM TABLE
)
SELECT * FROM ID_TABLE T1
JOIN SORTOUT T2 on (T1.ID = T2.ID)
WHERE FILTER = 0
If you only want the id, use aggregation:
select id
from id_table
group by id
having sum(case when color = 'RED' then 1 else 0 end) = 0;
If you want the original rows, you can use not exists:
select i.*
from id_table i
where not exists (select 1
from id_table i2
where i2.id = i.id and i2.color = 'RED'
);
You can use simple select query to achieve this,
SELECT * FROM ID_TABLE WHERE COLOR != 'Red';
If you still need CASE statement, you can use below
SELECT ID, COLOR,
CASE WHEN COLOR = 'Red' THEN 1 ELSE 0 END AS Val
FROM ID_TABLE WHERE CASE WHEN COLOR = 'Red' THEN 1 ELSE 0 END = 0;
Or
SELECT ID, COLOR FROM
(SELECT ID, COLOR,
CASE WHEN COLOR = 'Red' THEN 1 ELSE 0 END AS Val
FROM ID_TABLE) qry
WHERE Val = 0;

Could anyone help me on this SQL Query?

I have a table having some records.(refer below Image)
In This, I want variant_id based on some condition. Here I tried this query which was not worked for me in the above table.
SELECT distinct variant_id FROM variant_parameter
WHERE (parameter_id = '2' AND parameter_value IN ( 'M' ))
AND (parameter_id = '1' AND parameter_value IN ( 'Black' ))
AND variant_id IN ('1','2','3','4','5')
Expected Output: 4 but no record coming.
Could anyone help me with this?
You can use aggregation and flter with a having clause:
select variant_id
from variant_parameter
where variant_id between 1 and 5 and parameter_id in (1, 2)
group by variant_id
having
max(case when parameter_id = 2 and parameter_value = 'M' then 1 end) = 1
and max(case when parameter_id = 1 and parameter_value = 'Black' then 1 end) = 1
You haven't provided an MCRE, so this just a guess...
SELECT variant_id
FROM variant_parameter
WHERE (parameter_id = 2 AND parameter_value IN ( 'M' ) )
OR (parameter_id = 1 AND parameter_value IN ( 'Black' ))
GROUP BY variant_id
HAVING COUNT(*) = 2
-- AND optional other condition

How to SELECT COUNT multiple values in one column

rather a newbie at SQL, so please be gentle....as I think this is a basic one.
I'm trying to write a query with multiple (13) counts, based off Column1. The 1st Count is the over-all total. And then the 12 others are filtered by Color. I can get my results by doing multiple Counts all in one query, but this gives me 13 rows of data. The goal here is to get everything on just one row. So, almost like each count would be its own column. Here is an example of the data model
Database = CARS, Table = TYPES, Column1 = LICENSE, Column2 = COLOR
SELECT COUNT (LICENSE) AS 'Total ALL Cars'
FROM CARS.TYPES WITH (NOLOCK)
SELECT COUNT (LICENSE) AS 'Total RED Cars'
FROM CARS.TYPES WITH (NOLOCK)
WHERE COLOR = 'RED'
And on & on & on for each remaining color. This works, but again, I'm trying to streamline it all into one row of data, IF possible. Thank you in advance
You simply need to include color in select statement and group by it to count cars of each color.
SELECT Color, Count(*)
FROM CARS.TYPES WITH(NOLOCK)
GROUP BY Color
or
SELECT COUNT(CASE WHEN Color = 'RED' THEN 1
ELSE NULL
END) AS RedCars
,COUNT(CASE WHEN Color = 'BLUE' THEN 1
ELSE NULL
END) AS BlueCars
,COUNT(*) AS AllCars
FROM CARS.TYPES WITH ( NOLOCK )
You can do this with a conditional SUM():
SELECT SUM(CASE WHEN Color = 'Red' THEN 1 END) AS 'Total Red Cars'
,SUM(CASE WHEN Color = 'Blue' THEN 1 END) AS 'Total Blue Cars'
FROM CARS.TYPES
If using MySQL you can simplify further:
SELECT SUM(Color = 'Red') AS 'Total Red Cars'
,SUM(Color = 'Blue') AS 'Total Blue Cars'
FROM CARS.TYPES
Or with PIVOT
SELECT RED + BLUE + GREEN AS total,
RED,
BLUE,
GREEN
FROM CARS.TYPES PIVOT (COUNT (LICENSE) FOR COLOR IN ([RED], [BLUE], [GREEN])) P
SELECT SUM(Al) AllCount, SUM(Red) RedCount, SUM(Green) GreenCount, ...
(
SELECT 1 AS Al
, CASE WHEN Color = 'Red' THEN 1 ELSE 0 END AS Red
, CASE WHEN Color = 'Green' THEN 1 ELSE 0 END AS Green
...
FROM CARS.Types
)

SQL display summation of data in row

I have a table like this
No.
--
b
r
g
g
r
b
r
g
I want resultset like below
Type of color | Ocurrence
Blue 2
green 3
red 3
TOTAL 8
Please help
Sounds like CASE and GROUP BY would be what you need;
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
END "Type of color", COUNT(color) "Occurrence"
FROM Table1
GROUP BY color
ORDER BY color;
An SQLfiddle to test with.
To get a total, one (not necessarily the simplest) way is to just UNION with the total;
WITH cte AS (
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
END "Type of color", COUNT(color) "Occurrence"
FROM Table1
GROUP BY color
UNION
SELECT 'TOTAL',COUNT(*)
FROM Table1
)
SELECT * FROM cte
ORDER BY CASE WHEN "Type of color" = 'TOTAL' THEN 1 END;
Another SQLfiddle.
Joachim's answer is fine, except there is an easier way to get the total using rollup:
SELECT
CASE WHEN color = 'r' THEN 'red'
WHEN color = 'g' THEN 'green'
WHEN color = 'b' THEN 'blue'
when color is NULL then 'Total'
END "Type of color", COUNT(*) "Occurrence"
FROM Table1
GROUP BY color with rollup
ORDER BY (case when color is null then 1 else 0 end), color