How do I select rows which don't equal a value and also include nulls in the returned data?
I've tried:
SET ANSI_NULLS OFF
SELECT TOP 30 FROM Mails
WHERE assignedByTeam <> 'team01'
I want to return rows which don't have 'team01' in column assignedByTeam but I also want results containing nulls. Unfortunately, the above code doesn't work (doesn't return the nulls).
I'm using MS SQL Server 2008 Express.
Try checking for NULL explicitly:
SELECT TOP 30 col1, col2, ..., coln
FROM Mails
WHERE (assignedByTeam <> 'team01' OR assignedByTeam IS NULL)
SELECT TOP 30 FROM Mails
WHERE ISNULL(AssignedByTeam,'') <> 'team01'
I saw a coalesce statement version but ISNULL() is more efficient.
When you have a lot of conditions, typing everything twice stinks. Here are two better alternatives:
SELECT TOP 30 FROM Mails
WHERE COALESCE(assignedByTeam,'') <> 'team01'
The COALESCE operator returns the first non-null value in the list. If assignedByTeam is NOT null, it will compare the assignedByTeam value to 'team01'. But if assignedByTeam IS null, it will compare a blank '' to 'team01'. It's basically shorthand for the following:
SELECT TOP 30 FROM Mails
WHERE (CASE WHEN assignedByTeam IS NULL THEN '' ELSE assignedByTeam END) <> 'team01'
The second way is to make your condition conditional, for example:
SELECT TOP 30 FROM Mails
WHERE 1 = CASE WHEN assignedByTeam = 'team01' THEN 0 ELSE 1 END
In this example, the ELSE value will include all null rows, since they aren't equal to 'team01'.
SELECT TOP 30 FROM Mails
WHERE assignedByTeam <> 'team01'
OR assignedByTeam is null
where column != 'value' or column is null
Related
when executing this MSSQL statement it forces me to add the CASE WHEN field History.Email in the group by field, however I need to group only on a date field.
Any idea how this can be solved? (sql 2008)
SELECT TOP (100)
convert(varchar, VisitDate, 111) as Date,
SUM(AmountLoaded) AS Loaded,
CASE when History.Email is null then
SUM(AmountCash)
else SUM(AmountToPay)
end AS Total,
CASE when History.Email is null then
0
else SUM(AmountToPay)
end AS App,
SUM(AmountToPay) AS Consumed
FROM dbo.History
GROUP BY convert(varchar, VisitDate, 111)
order by 1 desc
When performing conditional aggregation you need to put the entire CASE expression in an aggregate, not just the expression(s) you want to aggregate:
SELECT TOP (100)
CONVERT(varchar(10), H.VisitDate, 111) AS [Date], --Always specify a length/precision/scale for your data types
SUM(H.AmountLoaded) AS Loaded,
SUM(CASE WHEN H.Email IS NULL THEN H.AmountCash ELSE H.AmountToPay END) AS Total,
SUM(CASE WHEN H..Email IS NULL THEN 0 ELSE H.AmountToPay END) AS App,
SUM(H.AmountToPay) AS Consumed
FROM dbo.History H
GROUP BY CONVERT(varchar(10), H.VisitDate, 111) --Always specify a length/precision/scale for your data types
ORDER BY [Date] DESC; --Be specific, don't use ordinal positions
Note, as well, I suggest not converting your column VisitDate to a varchar in your query. If you want to display the data in a specific format then that's something for your presentation layer to do, not the RDBMS. Fortunately, as style 111 is yyyy/MM/dd then the order of the data isn't affected. If, however, you want to simply truncate the time portion of your value, convert it to a date so that the value continues to remain strongly typed.
By definition any nonaggregate fields should be in the group by clause. You can write that as:
SELECT TOP (100)
convert(varchar, VisitDate, 111) as Date,
SUM(AmountLoaded) AS Loaded,
SUM(CASE when History.Email is null
then AmountCash
else AmountToPay end) AS Total,
SUM(CASE when History.Email is null
then 0
else AmountToPay
end) AS App,
SUM(AmountToPay) AS Consumed
FROM dbo.History
GROUP BY convert(varchar, VisitDate, 111)
order by 1 desc
I have a table PROCESS . Currently it doesnt not have any records in it. I need to return one hardcoded row if the table doesnt have any record .
I am doing a select when the primary key column "id" is null then i hard code the values and return it as below
SELECT CASE WHEN p.ID IS NULL THEN 1 ELSE p.ID END ,
CASE WHEN p.COMPANY IS NULL THEN 'COMP1' ELSE p.COMPANY END
FROM PROCESS p
I took reference from the below link
If-else statement in DB2/400
But it always returns me an empty row in DB2 database and not the hardcoded values used in select statement.
08:50:27 SUCCESS SELECT 0.307 0.301 0 Empty result set fetched
08:50:29 FINISHED 0.307 0.301 0 Success: 1 Failed: 0
Please help me on this
no way to do in this way, since a primary key could never be null. and select * from empty table return no row (0 row) it do not return null.
you can do it like that:
select ID, COMPANY from PROCESS
UNION ALL
select 1 as ID, 'COMP1' as COMPANY from sysibm.sysdummy1 where (select count(*) from PROCESS) = 0;
There are various ways you could achieve what (I think) you want. This is one
SELECT
COALESCE(ID,1) AS ID
, COALESCE(COMPANY,'COMP1') AS COMPANY
FROM
TABLE(VALUES 1) AS DUMMY(D)
LEFT JOIN
PROCESS
ON
1=1
I am trying to get select from a table and return row based on values of a column. Below is data and desired output. If column EmpRecord has multiple values not null to be returned, if it has only null then it should be returned.
Data Table
EmployeeNo EmpRecord
1 A
1 NULL
2 a
3 NULL
4 NULL
4 A
4 aa
Output
EmployeeNo EmpRecord
1 A
2 a
3 NULL
4 A
4 aa
Any advice on how to go ahead with it would be great?
Regards,
Sid
The first half of the UNION query below simply strips off records for which the EmpRecord be NULL. This almost gets the job done, except that for employees who have only one more NULL records, this would remove them from the result set as well. So the second part of the UNION adds these employees back as a single record with their employee number and NULL placeholder for the record.
SELECT t1.EmployeeNo,
t1.EmpRecord
FROM yourTable t1
WHERE t1.EmpRecord IS NOT NULL
UNION ALL
SELECT t2.EmployeeNo,
NULL AS EmpRecord
FROM yourTable t2.
GROUP BY t2.EmployeeNo
HAVING SUM(CASE WHEN t2.EmpRecord IS NULL THEN 1 ELSE 0 END) = COUNT(*)
I have a table MyTable that is populated by BizTalk from two outside agencies (AGC1 and AGC2) every day. I have recently run into a problem where AGC2 has been sending a blank file, so no records get updated.
I need to run a query like below, that would return
AGC1 01/28/2016
AGC2 NULL
But since I have no records for AGC2 I only get
AGC1 01/28/2016
How can I get AGC2 NULL as in the first resultset?
SELECT Agency, MAX(CONVERT(nvarchar(30), DATEENTERED, 101)), Case when MAX(CONVERT(nvarchar(30), DATEENTERED, 101)) = CONVERT(nvarchar(30), GETDATE(), 101) THEN 'True' ELSE 'False' end
FROM DBO.MYTABLE
GROUP BY AGENCY
Have another table containing:
NameTable
Name
-----
AGC1
AGC2
Then use this query:
Select n.Name, max(m.dateentered)
from MyTable m right join NameTable n
on m.Agency = n.Name
group by n.Name
A RIGHT JOIN with a table containing all agencies should do the trick. This version uses a derived table with VALUES:
SELECT
ag.Agency,
MAX(CONVERT(nvarchar(30), mt.DATEENTERED, 101)),
Case when MAX(CONVERT(nvarchar(30), mt.DATEENTERED, 101)) = CONVERT(nvarchar(30), GETDATE(), 101) THEN 'True' ELSE 'False' end
FROM
DBO.MYTABLE AS mt
RIGHT JOIN ( VALUES('AGC1'),('AGC2') ) AS ag(Agency) ON
ag.Agency=mt.Agency
GROUP BY
ag.AGENCY
I have this table:
ID Value
------------
1 car
1 moto
2 car
2 moto
3 moto
3 apple
4 gel
4 moto
5 NULL
note that moto is common to all IDs.
I would to obtain a single row with this result
car*, moto, apple*, gel*
i.e.
car, apple, gel with an asterisk because is present but NOT in all IDs
moto without an asterisk because is COMMON to all IDs
If ID + Value are Unique
SELECT Value, CASE WHEN COUNT(*) <> (SELECT COUNT(DISTINCT ID) FROM MyTable) THEN '*' ELSE '' END AS Asterisk FROM MyTable WHERE Value IS NOT NULL GROUP BY Value
Note that this won't group in a single line. And note that your question is wrong. ID 5 is an ID, so moto isn't common to all the IDs. It's common to all the IDs that have at least a value.
If we filter these IDs as written,
SELECT Value, CASE WHEN COUNT(*) <> (SELECT COUNT(DISTINCT ID) FROM MyTable WHERE Value IS NOT NULL) THEN '*' ELSE '' END FROM MyTable WHERE Value IS NOT NULL GROUP BY Value
To "merge" the * with Value, simply replace the , with a +, like:
SELECT Value + CASE WHEN COUNT(*) <> (SELECT COUNT(DISTINCT ID) FROM MyTable WHERE Value IS NOT NULL) THEN '*' ELSE '' END Value FROM MyTable WHERE Value IS NOT NULL GROUP BY Value
To make a single line use one of https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/ I'll add that, sadly, tsql doesn't have any native method to do it, and all the alternatives are a little ugly :-)
In general, the string aggregation part is quite common on SO (and outside of it) Concatenate row values T-SQL, tsql aggregate string for group by, Implode type function in SQL Server 2000?, How to return multiple values in one column (T-SQL)? and too many others to count :-)