This is just a very simple version of my actual query and I'd like to know how I can write just one query instead of two for the following select?
If #ShowZero = 0
Select Value From Metrics
Else
Select Value From Metrics Where Value <> 0
I have tried something as
Select Value From Metrics Where Value = Case #ShowZero = 0 then Value Else (here I'm stuck).
#ShowZero is a flag of 0 and 1.
Thank you!
You don't need a case for this:
Select Value
From Metrics
Where #ShowZero = 0 or Value <> 0;
EDIT:
This works, because, uhh, that is how boolean logic works. In other words, if you did a logic table for your expression, it would look like:
#ShowZero Value Action
0 0 Show row
0 other Show row
other 0 Filter out row
other other Show row
There are multiple ways to express this. The if statement is one of them. You could say: "I'm going to show a row unless #ShowZero is not 0 and value is 0". The where clause would be:
where not (#ShowZero <> 0 and Value = 0)
Alternatively, you could say: "I'm going to show a row when either of the following is true:
#ShowZero = 0
Value <> 0
If both are true great! I'll still show the row." This is the where clause that I used.
Related
I need your help with this query.
My table CSO_EMP_ORG_DPM_VIE has a column with different keys. Column name is EXT_KEY.
When I receive the same key number in EXT_KEY, I want the SQL code to count the duplicates using this query:
select EXT_KEY
from CSO_EMP_ORG_DPM_VIE
group by EXT_KEY
having count(*) > 1
This is working so far, but when it has no duplicate keys (numbers) in the column, I want it to generate it with 0 zero, and not nothing.
My expected result is; when two keys are the same I want to generate a 1. When no keys are the same, I want to generate an 0. Right now i got no result at all like in the screenshot.
How can I fix this SQL query accordingly?
Thank you in advance.
Use a CASE expression like this:
SELECT EXT_KEY,
CASE WHEN COUNT(*) > 1 THEN 1 ELSE 0 END flag
FROM CSO_EMP_ORG_DPM_VIE
GROUP by EXT_KEY
or if you want 1 result for the table:
SELECT CASE WHEN COUNT(EXT_KEY) > COUNT(DISTINCT EXT_KEY) THEN 1 ELSE 0 END flag
FROM CSO_EMP_ORG_DPM_VIE
It's not blindingly obvious as to what you are asking for. To that end, this query gives a 1/0 result based on having a count greater than 0 for each key...
SELECT
p.EXT_KEY,
EXT_KEY_RESULT = ISNULL((SELECT 1
FROM CSO_EMP_ORG_DPM_VIE c
WHERE c.EXT_KEY = p.EXT_KEY
HAVING COUNT(EXT_KEY) > 0), 0)
FROM
CSO_EMP_ORG_DPM_VIE p
Alternatively, if you are looking to count each of the keys, you could try...
SELECT EXT_KEY, COUNT(EXT_KEY)
FROM CSO_EMP_ORG_DPM_VIE
GROUP BY EXT_KEY
It's always good practice to specify a particular field in the COUNT aggregate, particularly the primary key, as it's faster to reference.
You really need to give us an expected result for your requirements and be very clear about your expectations.
SELECT CASE WHEN COUNT(EXT_KEY) > 0 THEN 1 ELSE 0 AS dupes
FROM CSO_EMP_ORG_DPM_VIE
PLEASE NOTE: Credit here to forpas for providing a smoother answer which I have borrowed.
I want to hide rows in SSRS report having Zero Quantity.
There are following multiple Quantity Columns like Opening Stock, Gross Dispatched,Transfer Out, Qty Sold, Stock Adjustment and Closing Stock etc.
I am doing this task by using following expression:
=IIF(Fields!OpeningStock.Value=0 AND Fields!GrossDispatched.Value=0 AND
Fields!TransferOutToMW.Value=0 AND Fields!TransferOutToDW.Value=0 AND
Fields!TransferOutToOW.Value=0 AND Fields!NetDispatched.Value=0 AND Fields!QtySold.Value=0
AND Fields!StockAdjustment.Value=0 AND Fields!ClosingStock.Value=0,True,False)
But by using this expression in row visibility, report hides all the rows except Totals Row. Even though report should show rows having Quantities of above mentioned columns.
Total values are shown correct.
Note: I set this row visibility expression on Detail Row.
Without using expression result is as following.
For the first 2 rows all the quantities are 0 (ZERO), i want to hide these 2 rows.
How can I fix this problem, or which expression must I use to get required results?
Could you try this out?
=IIF((Fields!OpeningStock.Value=0) AND (Fields!GrossDispatched.Value=0) AND
(Fields!TransferOutToMW.Value=0) AND (Fields!TransferOutToDW.Value=0) AND
(Fields!TransferOutToOW.Value=0) AND (Fields!NetDispatched.Value=0) AND (Fields!QtySold.Value=0)
AND (Fields!StockAdjustment.Value=0) AND (Fields!ClosingStock.Value=0),True,False)
Note: Setting Hidden to False will make the row visible
You don't need an IIF() at all here. The comparisons return true or false anyway.
Also, since this row visibility is on a group row, make sure you use the same aggregate function on the fields as you use in the fields in the row. So if your group row shows sums, then you'd put this in the Hidden property.
=Sum(Fields!OpeningStock.Value) = 0 And
Sum(Fields!GrossDispatched.Value) = 0 And
Sum(Fields!TransferOutToMW.Value) = 0 And
Sum(Fields!TransferOutToDW.Value) = 0 And
Sum(Fields!TransferOutToOW.Value) = 0 And
Sum(Fields!NetDispatched.Value) = 0 And
Sum(Fields!QtySold.Value) = 0 And
Sum(Fields!StockAdjustment.Value) = 0 And
Sum(Fields!ClosingStock.Value) = 0
But with the above version, if one record has value 1 and one has value -1 and all others are zero then sum is also zero and the row could be hidden. If that's not what you want you could write a more complex expression:
=Sum(
IIF(
Fields!OpeningStock.Value=0 AND
Fields!GrossDispatched.Value=0 AND
Fields!TransferOutToMW.Value=0 AND
Fields!TransferOutToDW.Value=0 AND
Fields!TransferOutToOW.Value=0 AND
Fields!NetDispatched.Value=0 AND
Fields!QtySold.Value=0 AND
Fields!StockAdjustment.Value=0 AND
Fields!ClosingStock.Value=0,
0,
1
)
) = 0
This is essentially a fancy way of counting the number of rows in which any field is not zero. If every field is zero for every row in the group then the expression returns true and the row is hidden.
Here is an example that should give you some idea..
=IIF(First(Fields!Gender.Value,"vw_BrgyClearanceNew")="Female" and
(First(Fields!CivilStatus.Value,"vw_BrgyClearanceNew")="Married"),false,true)
I think you have to identify the datasource name or the table name where your data is coming from.
hi here is my small problem . im working on sql and i have some logical operations to get the values from the sql database . i have the screen shot . plz refer it
in that i have four query in different combinations. if you take 1 and 2 both gives me the same answer and 3 and 4 gives me different answer . Now my question in i have two operators 1.OR and 2.And Not and the filters while means the variable may be n . now my question is
i want to get the different combinations for the given variables
and have to eliminate the possibility which gives me same result
any algorithms coding are welcome
can anyone help me soon
update
for more clear
if i have four values namely a,b,c,d
then i have to frame the diff combinations like
1. (a or b) and not( c or d)
2. a or ( b and not c ) or d
i have updated my question .. like this i have to generate different combination and get the answer
If you want to compare multiple rows within a set you can't use the logic you showed in your example, because a single row can't have multiple values within a singe column.
A common solution is to use aggregation over this group of rows and move the conditions into CASEs in HAVING checking if there's any row paasing the check:
e.g. your 2nd select,
(code = 40660 or code = 40900) and not code = 41180
can be simplified to
(code in (40660, 40900)) and code <> 41180
Translated into HAVING:
SELECT grpcol
FROM tab
GROUP BY grpcol
HAVING
-- any row with a code 40660 or 40900 --> result > 0 --> TRUE
SUM(CASE WHEN code IN (40660, 40900) THEN 1 ELSE 0 END) > 0
AND
-- any row with code 41180 --> result > 0 --> FALSE
SUM(CASE WHEN code <> 41180 THEN 1 ELSE 0 END) = 0
I have this piece of code which I am not sure how would it work:
UPDATE Data
SET Processed = 1
FROM Data
JOIN Meters
ON Meters.ServiceAccount = serv_acct
where COALESCE(Processed, 0) = 0
My question is about the last line! Would that line ever be true in this case?
Since I am setting Processed to 1 then how would that work:
where COALESCE(Processed, 0) = 0?
Can anybody explain the logic of using Coalesce in this way?
This code is not written by me.
Thank you
Your query is:
UPDATE Data
SET Processed = 1
FROM Data JOIN
Meters
ON Meters.ServiceAccount = serv_acct
where COALESCE(Processed, 0) = 0;
An update query determines the population of rows it is acting on before any of the changes are made. So, the final line is taking rows where Processed is either NULL or 0. The update is then setting Processed to 1 for those rows. In other words, the where clause is acting as a filter on the rows to modify. The specific statement is to keep the rows where the value of Processed is NULL or 0.
The COALESCE function is described here:
http://technet.microsoft.com/en-us/library/ms190349.aspx
I think the reason behind using this predicate where COALESCE(Processed, 0) = 0 was to filter all rows which have column Processed IS NULL or equal to 0.
Instead, I would use use predicates:
UPDATE Data
SET Processed = 1
FROM Data JOIN
Meters
ON Meters.ServiceAccount = serv_acct
where Processed IS NULL OR Processed = 0;
because they are SARGable. This means Index Seek.
Applying an expression on Processed column will force SQL Server to choose an [Clustered] Index Scan.
I am tryin to write the following case statement in my SELECT list:
CASE
WHEN SUM(up.[Number_Of_Stops]) = 0 THEN 0
ELSE SUM(up.[Total_Trip_Time] / up.[Number_Of_Stops])
END
I keep getting divde by zero errors though. This is the whole point of thise case statement to avoid this. Any other ideas?
You're checking for a different case than the one that's causing the error. Note:
WHEN SUM(up.[Number_Of_Stops]) = 0
Will only be true when all records in the grouping have Number_Of_Stops = 0. When that isn't the case, but some records do have Number_Of_Stops = 0, you'll divide by zero.
Instead, try this:
SUM(CASE
WHEN up.[Number_Of_Stops] = 0 THEN 0
ELSE up.[Total_Trip_Time] / up.[Number_Of_Stops]
END)
The zero check is based on SUM, an aggregate function, which means it is not executing per row -- which is when the division is occurring.
You're going to have to review the GROUP BY clause, or run the division (and zero check) in a subquery before applying SUM to result. IE:
SELECT SUM(x.result)
FROM (SELECT CASE
WHEN up.[Number_Of_Stops]) > 0 THEN
up.[Total_Trip_Time] / up.[Number_Of_Stops]
ELSE
0
END AS result
FROM TABLE) x