how can i get true\false output in sqlserver - sql-server-2005

i have this values in my table:
id
-----
1
2
null
3
4
null
5
Now I want to run a command similar to the following command:
select id==null from table
And the output should be as follows:
id
-----
false
false
true
false
false
true
false
How do?

You can use CASE expressions:
SELECT id,
CASE WHEN id IS NULL THEN 'true' ELSE 'false' END AS id_Description
FROM dbo.Table1

Related

SQL To count values of multiple columns

I have a table, with columns like this:
name1,name2,name_thesame,adress1,adress2,adress_thesame,city1,city2,city_thesame
In all columns ending with _thesame, there is a true or false depending if name1 and name2 are the same, same with adress etc etc.
I now need a query that returns a count how many true and false i got for each of the _thesame columns.
Cant wrap my head around how to do this query - any body got some ideas or pointers? Thanks
For a single property you can do:
select name_thesame, count(*)
from table
group by name_thesame
This will give you results like:
true 10
false 15
If you want to have it as a list for multiple columns, you can just union the queries:
select 'Name', name_thesame, count(*)
from table
group by name_thesame
union
select 'Address', adress_thesame, count(*)
from table
group by adress_thesame
getting:
Name true 10
Name false 15
Address true 20
Address false 5
This is another option:
SELECT SUM(CASE WHEN name_thesame = true THEN 1 ELSE 0 END) as nametrue,
SUM(CASE WHEN name_thesame = false THEN 1 ELSE 0 END) as namefalse,
SUM(CASE WHEN adress_thesame = true THEN 1 ELSE 0 END) as adresstrue,
SUM(CASE WHEN adress_thesame = false THEN 1 ELSE 0 END) as adressfalse,
SUM(CASE WHEN city_thesame = true THEN 1 ELSE 0 END) as citytrue,
SUM(CASE WHEN city_thesame = false THEN 1 ELSE 0 END) as cityfalse
FROM yourTable
You can tweak it to deal with NULLs as well if relevant:
...
CASE WHEN name_thesame = false OR name_thesame IS NULL THEN 1 ELSE 0 END
...
or either NVL(), ISNULL(), IFNULL() or COALESCE(), depending on the DBMS you're using (syntax is always the same):
...
CASE WHEN COALESCE(name_thesame, false) = false THEN 1 ELSE 0 END
...
Result:
nametrue | namefalse | adresstrue | adressfalse | citytrue | cityfalse
5 | 7 | 2 | 1 | 10 | 8

SQL Grouping with a logic check on another column?

Lets say that I have the following table:
id param result
1 AAA TRUE
1 AAA FALSE
1 AAA FALSE
1 BBB TRUE
1 CCC TRUE
2 AAA TRUE
2 CCC TRUE
3 AAA FALSE
3 AAA FALSE
3 AAA FALSE
3 CCC TRUE
3 BBB TRUE
Is there a way to group by the [param] column so that the [result] will show TRUE if any of the corresponding results are TRUE and show FALSE if none are TRUE? Below is an example of what I am aiming for:
id param result
1 AAA TRUE
1 BBB TRUE
1 CCC TRUE
2 AAA TRUE
2 CCC TRUE
3 AAA FALSE
3 CCC TRUE
3 BBB TRUE
Here is a sketch of code that will work. You just need to put in the right constants for result:
select id, param,
(case when sum(case when result = TRUE then 1 else 0 end) > 0
then TRUE else FALSE
end)
from table t
group by id, param;
SQL Server does not have a native boolean type and doesn't understand "true" and "false". For some representations (such as 'TRUE' and 'FALSE'), the following will work:
select id, param, max(result) as result
from table t
group by id, param;

Extract data between two tables

i have two tables. one table is request types and another is request rights. based on employee rights i need to show all the request type with employee assigned request type to true. i want to get reqtype for a particular employee.
for eg:-
Request type table
------------------------
REQID REqName ISACTIVE
---------------------------------
1 a true
2 b true
3 c true
REquest Rights table
-------------------------
ID ReTYpeId EmpId ISActive
----------------------------------
1 1 21 true
2 2 21 true
3 1 22 true
RESult Table
-----------------------
REQID REqName ISACTIVE
---------------------------------
1 a true
2 b true
3 c false
How to query this one.
so far i tried this much
SELECT tt.TransactionTypeName,tt.TransactionTypeId,tt.IsActive FROM sTransactionType tt JOIN sTransactionRights tr
ON tr.TransTypeID=tt.TransactionTypeId
WHERE tt.Division=1 AND tt.IsActive=1. i tried with case in isactive .but not working at all.
Try this
SELECT REQID, REqName, ISNULL(r.ISActive, 'false') AS ISACTIVE
FROM [Request type table] t
LEFT JOIN [REquest Rights table] r ON r.ReTYpeId = t.REQID AND EmpId = 21
WHERE t.ISACTIVE = 'true'

SQL Server - SELECT DISTINCT depending on another column value

I got a table like this, and there's on input parameter RevParam:
Key Rev IsCurrent
=====================
abc 0 TRUE
def 0 TRUE
ghj 2 FALSE
ghj 0 TRUE
klm 0 TRUE
def 1 FALSE
abc 1 FALSE
abc 2 FALSE
Where I want Key to be a unique value. If RevParam is set all rows with that Rev value should be viewed, the missing Key should have: revision 0. Like this:
Result if RevParam = 1
abc 1 FALSE
def 1 FALSE
ghj 0 TRUE
klm 0 TRUE
Result if revisionParameter = 0
abc 0 TRUE
def 0 TRUE
ghj 0 TRUE
klm 0 TRUE
This has been buggering me all day, please help me out!
WITH records
AS
(
SELECT [key], [rev], [IsCurrent],
ROW_NUMBER() OVER(PARTITION BY [key]
ORDER BY CASE WHEN [rev] = 1 -- <<== change REV value here
THEN 0 ELSE 1 END) rn
FROM tableName
WHERE [Rev] IN (1,0)
)
SELECT [key], [rev], [IsCurrent]
FROM records
WHERE rn = 1
SQLFiddle Demo for 1
SQLFiddle Demo for 0
Result if RevParam = 1
abc 1 FALSE
def 1 FALSE
ghj 0 TRUE
klm 0 TRUE
it has rev value 0 as well as 1. Please specify clearly

Value carry forword

I have below SQL Query..
SELECT dbo.O3.[s.no] AS [O3_Sn.NO], dbo.O5.[s.no] AS [O5_Sn.NO], dbo.O3.O3, dbo.O5.O5,
case when dbo.O3.[s.no] > dbo.O5.[s.no] then 'true' else 'false' end as ComparisonColumn
FROM dbo.O3 INNER JOIN
dbo.O5 ON dbo.O3.[s.no] = dbo.O5.[s.no]
When I run I am getting below output..
O3_Sn.NO O5_Sn.NO O3 O5 ComparisonColumn
1 1 10 11 TRUE
2 2 12 13 TRUE
3 3 11 10 FALSE
4 4 13 11 FALSE
5 5 15 16 TRUE
6 6 10 11 TRUE
7 7 12 13 TRUE
I want to remember the value of TRUE / False and should ignore if it is repeated untill i get a reverse case i.e., for TRUE , False.. and FOR False .. TRUE
Below is the out i should get it..
O3_Sn.NO O5_Sn.NO O3 O5 ComparisonColumn New_Case_Carry_value
1 1 10 11 TRUE TRUE
2 2 12 13 TRUE NULL
3 3 11 10 FALSE FALSE
4 4 13 11 FALSE NULL
5 5 15 16 TRUE TRUE
6 6 10 11 TRUE NULL
7 7 12 13 TRUE NULL
You can order your output using row_number() to compare a value with a value of the previous record:
with cIntermediate as (
SELECT dbo.O3.[s.no] AS [O3_Sn.NO], dbo.O5.[s.no] AS [O5_Sn.NO], dbo.O3.O3, dbo.O5.O5,
case when dbo.O3.[s.no] > dbo.O5.[s.no] then 'true' else 'false' end
as ComparisonColumn,
rowno = row_number() over
(order by dbo.O3.[s.no], dbo.O5.[s.no], dbo.O3.O3, dbo.O5.O5)
FROM dbo.O3 INNER JOIN dbo.O5 ON dbo.O3.[s.no] = dbo.O5.[s.no]
)
select i1.*,
case when i1.ComparisonColumn = i2.ComparisonColumn then null
else i1.ComparisonColumn end as NewCaseCarryValue
from cIntermediate i1
left join cIntermediate i2 on i2.rowno=i1.rowno-1
Ok, I will explain the concept to you, and I am sure you will be able to figure it out for yourself.
Your final result you published with all the true and false columns, that needs to become a temporary table, like this, but with some kind of identity column:
SELECT dbo.o3.[s.no] AS [O3_Sn.NO]
,dbo.o5.[s.no] AS [O5_Sn.NO]
,dbo.o3.o3
,dbo.o5.o5
,CASE
WHEN dbo.o3.[s.no] > dbo.o5.[s.no] THEN 'true'
ELSE 'false'
END AS comparisoncolumn
, ROW_NUMBER() OVER(ORDER BY dbo.o3.[s.no]) AS ident_col
INTO #temp
FROM dbo.o3
INNER JOIN dbo.o5
ON dbo.o3.[s.no] = dbo.o5.[s.no]
Then what you need to do is to select from #temp, and self join to #temp in a similar manner as here:
SELECT a.*
, CASE WHEN a.comparisoncolumn = b.comparisoncolumn THEN NULL ELSE a.comparisoncolumn END AS final_comparisoncolumn
FROM #temp a
LEFT JOIN #temp b ON a.ident_col = b.ident_col - 1
Then do a case statement to figure out if you need to print null, or true or false.
Play around with this concept, I am sure you will be able to work it out from here.
You can do an left join on the table itself (SN = SN-1) and compare the ComparisonColumn