Getting multiple variables to show up in sql server? - sql

I have two queries:
Select count(*) as countOne where field = '1'
Select count(*) as countTwo where field = '2'
What I want to see after executing these queries in my results viewer:
countOne | countTwo
23 | 123
How can I get the results from both queries by only running one query?

SELECT COUNT(CASE WHEN field = '1' THEN 1 END) AS countOne,
COUNT(CASE WHEN field = '2' THEN 1 END) AS countTwo
FROM YourTable
WHERE field IN ( '1', '2' )

The simplest way is to run each as a subselect eg.
SELECT
(
Select count(*) where field = '1' as countOne,
Select count(*) where field = '2' as countTwo
)
BUt this is not necesarily the best way
Another wayto do it would be to Group by field and then do PIVOT to select out each group as a separate column.

Related

How to group-filter field by values in a different field in BigQuery

I have the following data in BigQuery:
user action
1 0
1 1
2 0
2 0
3 1
3 0
I would like to filter the users by those who have at least one action equal to 1, but keep every row for these users. So, the result should be this:
user action
1 0
1 1
3 1
3 0
What I have done is the following. First, I group by user and create an array for the actions, filter for those that have the value 1 in the arrays (table_2), and then I unnest.
WITH table_1 AS (
SELECT '1' AS user, '0' AS action
UNION ALL SELECT '1' AS user, '1' AS action
UNION ALL SELECT '2' AS user, '0' AS action
UNION ALL SELECT '2' AS user, '0' AS action
UNION ALL SELECT '3' AS user, '1' AS action
UNION ALL SELECT '3' AS user, '0' AS action
),
table_2 AS (
SELECT
user,
ARRAY_AGG(action) AS action_array
FROM table_1
GROUP BY user
HAVING '1' IN UNNEST(action_array)
)
SELECT
user,
action_array
FROM table_2, table_2.action_array
Is there a better/more efficient/more sophisticated way to do this? Thanks.
Below is the right way to do so
select *
from table_1
where true
qualify countif(action = '1') over(partition by user) > 0
If applied to sample data in your question - output is
Try this
Select user, action
From table where user in (select user from table group by user having sum(action) <> 0)
There are couple of ways to solve this.
Using a the qualify filter (Option 1) as per the docs you have significant speed gains over the sub query (Option 2) style and as stated by #MikhailBerlyant it is the correct way to do so.
WITH table_1 AS (
SELECT '1' AS user, '0' AS action
UNION ALL SELECT '1' AS user, '1' AS action
UNION ALL SELECT '2' AS user, '0' AS action
UNION ALL SELECT '2' AS user, '0' AS action
UNION ALL SELECT '3' AS user, '1' AS action
UNION ALL SELECT '3' AS user, '0' AS action
)
-- Option 1
SELECT *
FROM table_1
WHERE true
QUALIFY COUNTIF(action = '1') OVER (PARTITION BY user) > 0
-- Option 2
SELECT t1.*
FROM (
SELECT user, COUNTIF(action = '1') as has_action
FROM table_1
GROUP BY user
HAVING has_action > 0
) as h1
JOIN table_1 as t1
ON t1.user = h1.user

SQL, return select results with different where clauses

I have table whose column is just the length of a session and I would like to return the number of session that have zero length and the number of sessions that have length greater than zero.
I can do that with two separate commands
select count(session_length) from my_table where session_length=0
select count(session_length) from my_table where session_length>0
But I would like to see the results combined in one table
You can do it with one query using conditional aggregation.
select
count(case when session_length = 0 then 1 end),
count(case when session_length > 0 then 1 end)
from my_table
select 1 as QryNo, count(session_length) as SessLen
from my_table
where session_length=0
union
select 2 as QryNo, count(session_length) as SessLen
from my_table
where session_length>0
or
select
case
when session_length = 0 then 1
else 2
end as QryNo,
count(session_length) as SessLen
from my_table
This may be too simple so apologies if I have misread your query but Can you use
select count(session_length) from my_table where session_length >= 0
Again, Apologies if this is not what you're looking for.

SQL Server : do not Select all if true

I have these columns
Id Status
----------
1 pass
1 fail
2 pass
3 pass
How do I select all that only have a status of pass but if the Id has at least one fail it will not be selected as well.
If same id can have multiple passes
SELECT id
from table
WHERE status = 'pass'
and id NOT IN (SELECT id FROM table WHERE status = 'fail')
You need to use GROUP BY & HAVING clause
SELECT Id
FROM yourtable
GROUP BY Id
HAVING Sum(case when status ='pass' then 1 else 0 end) = count(status)
HAVING clause can be changed to
HAVING Count(case when status ='pass' then 1 end) = count(status)
I just hate chatty case statement, so
SELECT Id
FROM table1
GROUP BY Id
HAVING COUNT(DISTINCT [Status]) = 1 AND MIN([Status]) = 'pass'
or
SELECT Id
FROM table1
GROUP BY Id
HAVING COUNT(NULLIF([Status], 'fail')) = 1 AND COUNT(NULLIF([Status], 'pass')) = 0
The second query only works when status has two values 'pass' and 'fail'.

Select Distinct Attribute and Print out Count of another even when the count is 0

I don't quite know how I should describe the problem for title, but here's my question.
I have a table named hello with two columns named time and state.
Time | State
Here's an example of the data I have
1 DC
1 VA
1 VA
2 DC
2 MD
3 MD
3 MD
3 VA
3 DC
I would like to get all the possible time and the count of "VA" (0 if "VA" doesn't appear at the time)
The output would look like this
Time Number
1 2
2 0
3 1
I tried to do
SELECT DISTINCT time,
COUNT(state) as Number
FROM hello
WHERE state = 'VA'
GROUP BY time
but it doesn't seem to work.
This is a conditional aggregation:
select time, sum(case when state = 'VA' then 1 else 0 end) as NumVA
from hello
group by time
I want to add that you should never use distinct when you have a group by. The two are redundant. Distinct as a keyword is not even needed in the SQL language; semantically, it is just shorthand for grouping by all the columns.
SELECT TIME,
SUM(CASE WHEN State = 'VA' THEN 1 ELSE 0 END)
FROm tableName
GROUP BY Time
SQLFiddle Demo
One rule of thumb is to get your counts first and put them into a temp for use later.
See below:
Create table temp(Num int, [state] varchar(2))
Insert into temp(Num,[state])
Select 1,'DC'
UNION ALL
Select 1,'VA'
UNION ALL
Select 1,'VA'
UNION ALL
Select 2,'DC'
UNION ALL
Select 2,'MD'
UNION ALL
Select 3,'MD'
UNION All
Select 3,'MD'
UNION ALL
Select 3,'VA'
UNION ALL
Select 3,'DC'
Select t.Num [Time],t.[State]
, CASE WHEN t.[state] = 'VA' THEN Count(t.[State]) ELSE 0 END [Number]
INTO #temp2
From temp t
Group by t.Num, t.[state]
--drop table #temp2
Select
t2.[time]
,SUM(t2.[Number])
From #temp2 t2
group by t2.[time]

Using two select in one query

I have two SQL queries for counting the rows from the table tContentNode, depending on the type 2 or 3.
Query 1:
SELECT count(*) _countA FROM TCONTENTNODE WHERE type = '2'
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
Query 2:
SELECT count(*) _countB FROM TCONTENTNODE WHERE type = '3'
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
Now I want to get the values of _countA and _countB using only one query. How can I get the counts using one query. Is there any way to do that. I am using SQLite Database.
EDIT:
I want values of _countA and _countB separately, not both together(Not using IN).
I solved the problem using the following query:-
SELECT
COUNT(case TCONTENTNODE.type when '2' then 1 end) as _countA,
COUNT(case TCONTENTNODE.type when '3' then 1 end) as _countB
FROM TCONTENTNODE
WHERE TCONTENTNODE.parentid ="02b3abc2-4983-485a-ab09-1a8cb328b9b5";
Dont have SQLite to confirm but
select count(*), type FROM TCONTENTNODE WHERE (type = '2' or type = '3' )
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5' group by type
Just to clarify this will return
count type
23 2
23888 3
Try to use group by
SELECT type,count(*) as cnt
FROM TCONTENTNODE
WHERE parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
group by type
for two variables:
SELECT
COUNT(case TCONTENTNODE.type when '2' then 1 end) as _countA,
COUNT(case TCONTENTNODE.type when '3' then 1 end) as _countB
FROM TCONTENTNODE
WHERE TCONTENTNODE.parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
Use IN KeyWord
SELECT count(*) _countB FROM TCONTENTNODE WHERE (type IN (3,2) AND
parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5');