Count the no of columns with same value in SQL - sql

I have a database MySQL table as following
Id
Column1
Column2
Column3
First
value1
value1
value2
Second
value2
value1
value2
I want to retrieve the count of value1 for the row with id First example table as follows
Id
COUNT(value1)
First
2
I searched on google but found posts only regarding counting in one specific column.
Can anyone help me with the SQL query for retrieving the above type.

Use a case expression to determine for each column whether to count it or not e.g.
select
case when Column1 = 'value1' then 1 else 0 end
+ case when Column2 = 'value1' then 1 else 0 end
-- ...
from MyTable
where id = 'first'

Related

Oracle SQL - A count that excludes completely a Key with the if not equal statement

Ok here goes:
MyTableName
KEY Value1
1 ABC
1 DEF
1 GHI
2 ABC
2 DEF
2 YUI
I have a child table that is linked via a Key (stated above)
I need to find records, linked to the parent table (i have no issues with my join) where value 1 meets conditions and does not meet certain conditions.
So, for my Example, I want to retrieve the value '1' from the "KEY" above, because my need is where value 1 = 'ABC' or value 1 = 'DEF' but value 1 != YUI,
so I would want '1' to come back, but not 2
SELECT KEY, count(*)
FROM MyTableName
WHERE (value1 = 'ABC'
OR value1 = 'DEF')
AND value1 != 'YUI'
GROUP BY KEY
HAVING count(KEY) = 2
The above statement, find both Key 1 and 2, where I need it only to find Key 1.
Can anyone help?
Use the HAVING clause with conditional counts:
select key
from mytablename
group by key
having count(case when value1 in ('ABC','DEF') then 1 end) > 0
and count(case when value1 = 'YUI' then 1 end) = 0;
(You can add where value1 in ('ABC','DEF','YUI'), which may speed up the query, because you are not interested in rows containing other values.)
(Some people prefer to include an explicit else branch: count(case when value1 = 'YUI' then 1 else null end) and others prefer sum(case when value1 = 'YUI' then 1 else 0 end). That's a matter of personal preference.)

How to get count values for multiple values in one column

My data is like this
Name Values
A Val1
A Val1
A Val2
A Val2
A Val2
B Val1
B Val2
I want to ouput my data is this way
Name Val1Count Val2Count
A 2 3
B 1 1
I can get the Name and count(*) for Val1 with this query.
select [Name],count(*) FROM [table1]
where [values]='Val1'
group by [Name]
But I am not sure how to get the count(*) for val2 in the same row.
I tried doing this, but looks like this is not supported
select [name],#val1count= (above query for val1), #val2count = (above query for val2)
Please help. Thanks for looking.
This is called pivoting. Some databases provide a PIVOT function. However, you can also do this manually.
SELECT [Name],
SUM ( CASE WHEN [Values]='VAL1' THEN 1 ELSE 0 END ) AS Val1Count,
SUM ( CASE WHEN [Values]='VAL2' THEN 1 ELSE 0 END ) AS Val2Count
FROM [table1]
GROUP BY [Name]
Explanation:
The CASE WHEN ... END gives each row a "boolean" value for whether or not the row matches your condition.
The SUM ( ... ) counts the number of rows which returned "true" (or 1).
The GROUP BY [Name] consolidates the rows down to one row per distinct name.
If you add conditions to a WHERE clause, the CASE WHEN will only see the rows matching your WHERE conditions.

Get addition of row values based on coresponding row values SQL Server

I have data base table(dbo.TestTable_1) like following:
Result Value1 Value2
Y A 1
A 0
B 1
A 1
B 0
I have to read top 1 of column "Value1" and check, in which row(of column Value1) matches the value. get the corresponding rows values and add it.If my addtion is > 0 then I want to return Yes, else return NO.
For example:
"select top 1 Value1 from dbo.TestTable_1" will return A.
Then I need to find the row number where A exists in "Value1". That is (here it is at), Row number 1,2 and 4. Then I want to add the corresponding values in "Value2" column. That is 1+0+1 = 2(this is greater then 0) then I want to return YES.
My output table should look like following:
Result Value1 Value2
YES A 1
A 0
B 1
A 1
B 0
I have tried following query to achieve this. But I'm unable to proceed further.
declare #val1 int
set #val1 = (select top 1 Value1 from dbo.TestTable_1)
create table #Temp
(
Val1 int
)
Insert into #Temp
select Value1
from dbo.TestTable_1
select * from #Temp
I hope, my explanation is understood. Please help.Thanks.
You are close, you just need to sum value2. You don't need a temp table either:
declare #val1 int
set #val1 = (select top 1 Value1 from dbo.TestTable_1)
select CASE WHEN SUM(Value2) > 0 THEN 'Yes' ELSE 'No' END
from dbo.TestTable_1
WHERE value1 = #val1

Searching in SQL based on values in 2 columns

I currently have 2 columns for my database and I'm trying to return all values in column 1 that don't contain a certain value in column two:
ex: Column 1 has 9 digit random value, sometimes repeated. There are 4 different options for column 2; P1, P2, P3, P4.
I'm trying to only display values in column 1 that don't have a value of P4 in column 2. If they don't have a P4, then I want them all to be displayed, but once a Column 1 value is associated with P4, I don't want any of the column 1 values displayed. This process will continue through all column 1 values until the only values displayed in column 1 are values that do not have a P4 column 2 value associated with them.
You mean something like this?
SELECT *
FROM YOUR_TABLE
WHERE COLUMN1 NOT IN (
SELECT COLUMN1
FROM YOUR_TABLE
WHERE COLUMN2 = 'P4'
)
Wouldn't this just be
SELECT column1 FROM <table> WHERE column2 != 'P4'
This is an example of a query where you are looking at sets within sets -- that is, sets of column2 within values of column1. I prefer using group by and having for these queries:
select column1
from t
group by column1
having sum(case when column2 = 'P4' then 1 else 0 end) = 0
To get all the values, you would join back to the original table:
select t.*
from t join
(select column1
from t
group by column1
having sum(case when column2 = 'P4' then 1 else 0 end) = 0
) c1
on t.column1 = c1.column1

How to display NULL when criteria not met

I have this table
Column1 Column2
1 value1
2 value2
3 value3
4 value4
Using this statement:
SELECT * FROM table WHERE column2='value2'
Only displays this:
Column1 Column2
2 value2
I want to display this:
Column1 Column2
1
2 value2
3
4
How?
Well, your WHERE clause is saying "show me the rows where column2 = value2" - so, as written, it can't possibly include any rows where column2 has any other value, because they've been filtered out.
Here's one way to accomplish what you're looking for, using a CASE expression (and no WHERE clause):
SELECT column1, column2 = CASE
WHEN column2 = 'value2' THEN 'value2'
ELSE NULL
END
FROM dbo.table;