I'm forced to work with a table with the following structure (ScheduledEmails) and am looking to find the most efficient way to deal with multiple variables being set to 1 (true)
The script below will only return the correct value if only one is selected.
Could anybody recommend a way to modify the bottom query to accomodate multiple conditions.
e.g. where #IsMonday and #IsTuesday is true etc.?
I have tried multiple combinations of AND/OR but none seem to yield the desired result.
DECLARE #IsMonday bit
DECLARE #IsTuesday bit
DECLARE #IsWednesday bit
DECLARE #IsThursday bit
DECLARE #IsFriday bit
DECLARE #IsSaturday bit
DECLARE #IsSunday bit
DECLARE #Is0000 bit
DECLARE #Is0100 bit
DECLARE #Is0200 bit
DECLARE #Is0300 bit
DECLARE #Is0400 bit
DECLARE #Is0500 bit
DECLARE #Is0600 bit
DECLARE #Is0700 bit
DECLARE #Is0800 bit
DECLARE #Is0900 bit
DECLARE #Is1000 bit
DECLARE #Is1100 bit
DECLARE #Is1200 bit
DECLARE #Is1300 bit
DECLARE #Is1400 bit
DECLARE #Is1500 bit
DECLARE #Is1600 bit
DECLARE #Is1700 bit
DECLARE #Is1800 bit
DECLARE #Is1900 bit
DECLARE #Is2000 bit
DECLARE #Is2100 bit
DECLARE #Is2200 bit
DECLARE #Is2300 bit
DECLARE #dayOfWeek VARCHAR(20)
DECLARE #hourOfDay int
SET #dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET #hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET #IsMonday = CASE WHEN #dayOfWeek = 'Monday' THEN 1 ELSE 0 END
SET #IsTuesday = CASE WHEN #dayOfWeek = 'Tuesday' THEN 1 ELSE 0 END
SET #IsWednesday = CASE WHEN #dayOfWeek = 'Wednesday' THEN 1 ELSE 0 END
SET #IsThursday = CASE WHEN #dayOfWeek = 'Thursday' THEN 1 ELSE 0 END
SET #IsFriday = CASE WHEN #dayOfWeek = 'Friday' THEN 1 ELSE 0 END
SET #IsSaturday = CASE WHEN #dayOfWeek = 'Saturday' THEN 1 ELSE 0 END
SET #IsSunday = CASE WHEN #dayOfWeek = 'Sunday' THEN 1 ELSE 0 END
SET #Is0000 = CASE WHEN #hourOfDay = 0 THEN 1 ELSE 0 END
SET #Is0100 = CASE WHEN #hourOfDay = 1 THEN 1 ELSE 0 END
SET #Is0200 = CASE WHEN #hourOfDay = 2 THEN 1 ELSE 0 END
SET #Is0300 = CASE WHEN #hourOfDay = 3 THEN 1 ELSE 0 END
SET #Is0400 = CASE WHEN #hourOfDay = 4 THEN 1 ELSE 0 END
SET #Is0500 = CASE WHEN #hourOfDay = 5 THEN 1 ELSE 0 END
SET #Is0600 = CASE WHEN #hourOfDay = 6 THEN 1 ELSE 0 END
SET #Is0700 = CASE WHEN #hourOfDay = 7 THEN 1 ELSE 0 END
SET #Is0800 = CASE WHEN #hourOfDay = 8 THEN 1 ELSE 0 END
SET #Is0900 = CASE WHEN #hourOfDay = 9 THEN 1 ELSE 0 END
SET #Is1000 = CASE WHEN #hourOfDay = 10 THEN 1 ELSE 0 END
SET #Is1100 = CASE WHEN #hourOfDay = 11 THEN 1 ELSE 0 END
SET #Is1200 = CASE WHEN #hourOfDay = 12 THEN 1 ELSE 0 END
SET #Is1300 = CASE WHEN #hourOfDay = 13 THEN 1 ELSE 0 END
SET #Is1400 = CASE WHEN #hourOfDay = 14 THEN 1 ELSE 0 END
SET #Is1500 = CASE WHEN #hourOfDay = 15 THEN 1 ELSE 0 END
SET #Is1600 = CASE WHEN #hourOfDay = 16 THEN 1 ELSE 0 END
SET #Is1700 = CASE WHEN #hourOfDay = 17 THEN 1 ELSE 0 END
SET #Is1800 = CASE WHEN #hourOfDay = 18 THEN 1 ELSE 0 END
SET #Is1900 = CASE WHEN #hourOfDay = 19 THEN 1 ELSE 0 END
SET #Is2000 = CASE WHEN #hourOfDay = 20 THEN 1 ELSE 0 END
SET #Is2100 = CASE WHEN #hourOfDay = 21 THEN 1 ELSE 0 END
SET #Is2200 = CASE WHEN #hourOfDay = 22 THEN 1 ELSE 0 END
SET #Is2300 = CASE WHEN #hourOfDay = 23 THEN 1 ELSE 0 END
INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
(SendMonday = #IsMonday AND
SendTuesday = #IsTuesday AND
SendWednesday = #IsWednesday AND
SendThursday= #IsThursday AND
SendFriday = #IsFriday AND
SendSaturday = #IsSaturday AND
SendSunday = #IsSunday AND
Send0000 = #Is0000 AND
Send0100 = #Is0100 AND
Send0200 = #Is0200 AND
Send0300 = #Is0300 AND
Send0400 = #Is0400 AND
Send0500 = #Is0500 AND
Send0600 = #Is0600 AND
Send0700 = #Is0700 AND
Send0800 = #Is0800 AND
Send0900 = #Is0900 AND
Send1000 = #Is1000 AND
Send1100 = #Is1100 AND
Send1200 = #Is1200 AND
Send1300 = #Is1300 AND
Send1400 = #Is1400 AND
Send1500 = #Is1500 AND
Send1600 = #Is1600 AND
Send1700 = #Is1700 AND
Send1800 = #Is1800 AND
Send1900 = #Is1900 AND
Send2000 = #Is2000 AND
Send2100 = #Is2100 AND
Send2200 = #Is2200 AND
Send2300 = #Is2300)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
SendMonday= #IsMonday AND
SendTuesday = #IsTuesday AND
SendWednesday = #IsWednesday AND
SendThursday= #IsThursday AND
SendFriday = #IsFriday AND
SendSaturday = #IsSaturday AND
SendSunday = #IsSunday AND
Send0000 = #Is0000 AND
Send0100 = #Is0100 AND
Send0200 = #Is0200 AND
Send0300 = #Is0300 AND
Send0400 = #Is0400 AND
Send0500 = #Is0500 AND
Send0600 = #Is0600 AND
Send0700 = #Is0700 AND
Send0800 = #Is0800 AND
Send0900 = #Is0900 AND
Send1000 = #Is1000 AND
Send1100 = #Is1100 AND
Send1200 = #Is1200 AND
Send1300 = #Is1300 AND
Send1400 = #Is1400 AND
Send1500 = #Is1500 AND
Send1600 = #Is1600 AND
Send1700 = #Is1700 AND
Send1800 = #Is1800 AND
Send1900 = #Is1900 AND
Send2000 = #Is2000 AND
Send2100 = #Is2100 AND
Send2200 = #Is2200 AND
Send2300 = #Is2300
If I have understood your requirement correctly, you just want to select the records for current day of week and hour from ScheduledEmails table, and insert them into ScheduledEmailQueue table. If that is indeed the case, you can create a dynamic sql query and execute it. If I have understood your requirement incorrectly, then I apologise in advance :)
DECLARE #dayOfWeek VARCHAR(20)
DECLARE #hourOfDay INT
DECLARE #hourOfDayColumnName VARCHAR(20)
DECLARE #dayOfWeekColumnName VARCHAR(20)
DECLARE #sql VARCHAR(MAX)
SET #dayOfWeek = datename(dw,GETDATE()) -- Monday to Sunday
SET #hourOfDay = datepart(hh, GETDATE()) -- Single digit 0 - 23
SET #hourOfDayColumnName = 'Send' + CASE WHEN LEN(#hourOfDay) = 1 THEN '0' + CONVERT(VARCHAR, #hourOfDay) ELSE CONVERT(VARCHAR, #hourOfDay) END + '00'
SET #dayOfWeekColumnName = 'Send' + #dayOfWeek
SET #sql = 'INSERT INTO ScheduledEmailQueue (ScheduledEmailId, Created)
SELECT Id, GETDATE() FROM ScheduledEmails WHERE ' + #dayOfWeekColumnName + ' = 1 AND ' + #hourOfDayColumnName + ' = 1'
PRINT #hourOfDayColumnName
PRINT #dayOfWeekColumnName
PRINT #sql
EXEC (#sql)
Sorry, I misread your question, but I am still not clear about your requirements. If you use DayOfWeek and Hour, they will always return you singular values (e.g. Monday and 22). If you want to select values based on multiple criteria, then you will have to do a simple hardcoded query like:
SELECT Id, GETDATE() FROM ScheduledEmails WHERE
SendMonday= 1 AND
SendTuesday = 1 AND
Send0000 = 1 AND
Send0100 = 1
If you do not know the values for required days and hours beforehand, then you can just pass them as parameters to a stored proc and use them for filtering.
Related
In a stored procedure I am trying to add two numeric values as shown
DECLARE #tradeamt1 NUMERIC(23,2)
DECLARE #tradeamt3 NUMERIC(23,2)
DECLARE #value NUMERIC(23,2)
if (#retVal1 = 0)
BEGIN
SELECT #row_count = count(1),
#tradeamt1=Sum(trade_amt) ,
#units= Sum(curr_shrs_num)
FROM [csr_staging].[dbo].[fi_impact_source] WITH(NOLOCK)
Where acct_id = 'BANKFEES'
and SD_ID >= EFF_DT
print #tradeamt1
END
if(#retVal3 > 0)
BEGIN
select #row_count = count(1) - #retVal3 + #row_count,#tradeamt3=Sum(trade_amt),#units= #units +Sum(curr_shrs_num) - #currshares
FROM [CSR_Staging].[dbo].[fi_impact_source] WITH(NOLOCK)
where (clearing_code = 'MBS'or clearing_code = 'CNS') and (SD_ID >= EFF_DT)
print #tradeamt3
END
set #value = #tradeamt1 +#tradeamt3
this value gives null instead of adding tradeamount1 =1.00 and tradeamount3 = 191432650.13
Maybe this could work for your SP:
declare #tradeamt1 numeric(23, 2) = 0
, #tradeamt3 numeric(23, 2) = 0
, #value numeric(23, 2) = 0
select #row_count = 0, #units = 0
if #retVal1 = 0
select #row_count = count(1)
, #tradeamt1 = isnull(sum(trade_amt), 0)
, #units = isnull(sum(curr_shrs_num), 0)
from [CSR_Staging].[dbo].[fi_impact_source] with (nolock)
where acct_id = 'BANKFEES'
and SD_ID >= EFF_DT
if #retVal3 > 0
select #row_count = #row_count + count(1) - #retVal3
, #tradeamt3 = isnull(sum(trade_amt), 0)
, #units = #units + isnull(sum(curr_shrs_num), 0) - #currshares
from [CSR_Staging].[dbo].[fi_impact_source] with (nolock)
where clearing_code in ('MBS', 'CNS')
and SD_ID >= EFF_DT
set #value = #tradeamt1 + #tradeamt3
Using isNull( ensures that both #tradeamt1 and #tradeamt3 are not null
This is most probably because when tradeamt1 and tradeamt3 are not initialised and for some reason also not assigned values.
You could initialise them with:
DECLARE #tradeamt1 NUMERIC(23,2) = 0
DECLARE #tradeamt3 NUMERIC(23,2) = 0
DECLARE #value NUMERIC(23,2) = 0
Let me know if that helps.
:)
Below is the code snippet in which MIN function using. When execute below code it is giving an error.
CREATE FUNCTION [dbo].[FN_TempCalcTransportExemp]
(
#EmployeeID varchar(20),
#PayElement varchar(20),
#Month varchar(10),
#FinYear varchar(10)
)
RETURNS decimal
AS
BEGIN
DECLARE #TarnsportExemption decimal(18,2) = 0
DECLARE #TDSIsFullExemption bit
DECLARE #PermanentPhysicalDisability decimal(18,2) = 0
DECLARE #UsingComapnyCar bit
DECLARE #Conveyance decimal(18,2) = 0
DECLARE #TransYes decimal(18,2) = 0
DECLARE #TransNo decimal(18,2) = 0
DECLARE #MinConveyance decimal(18,2) = 0
DECLARE #MinTransport decimal(18,2) = 0
SELECT
#TDSIsFullExemption = TDSDetailsFullExemption,
#TransYes = TDSDetailsYes,
#TransNo = TDSDetailsNo
FROM
tbl_TDSSettingDetails
WHERE
TDSSettingsDetailID = 2
SELECT
#Conveyance = #Month
FROM
tbl_Income
WHERE
EmployeeID = #EmployeeID
AND Element = #PayElement
AND FinancialYear = #FinYear
SELECT
#UsingComapnyCar = UsingCompanyCar,
#PermanentPhysicalDisability = PermanentPhysicalDisability
FROM
tbl_Employee_TDS
WHERE
EmployeeID = #EmployeeID
AND TDSFinancialYear = #FinYear
IF (#TDSIsFullExemption = 1)
BEGIN
SET #TarnsportExemption = #Conveyance
END
ELSE
BEGIN
IF (#UsingComapnyCar = 1)
BEGIN
IF (#Conveyance = 0)
BEGIN
SET #MinConveyance = 0
END
ELSE
BEGIN
SET #MinConveyance = #Conveyance
END
IF (#PermanentPhysicalDisability = 1)
BEGIN
SET #MinTransport = #TransYes
END
ELSE
BEGIN
SET #MinTransport = #TransNo
END
SET #TarnsportExemption = MIN(#MinConveyance, #MinTransport)
END
ELSE
BEGIN
SET #TarnsportExemption = 0
END
END
RETURN #TarnsportExemption
END
Error Message:
Msg 174, Level 15, State 1, Procedure FN_TempCalcTransportExemp, Line
66
The MIN function requires 1 argument(s).
set #TarnsportExemption = MIN(#MinConveyance,#MinTransport) - The MIN function is not what you think it is.
You probably want to do something like this:
set #TarnsportExemption = CASE WHEN #MinConveyance < #MinTransport THEN
#MinConveyance
ELSE
#MinTransport
END
Another option is this:
SELECT #TarnsportExemption = MIN(val)
FROM
(
SELECT #MinConveyance as val
UNION ALL
SELECT #MinTransport as val
)
And one more option is to use the values clause:
SELECT #TarnsportExemption = MIN(val)
FROM (VALUES ( #MinConveyance), (#MinTransport)) AS value(val)
Change your min statement like below MIN. Please refer MIN (Transact-SQL)
--fROM
set #TarnsportExemption = MIN(#MinConveyance,#MinTransport)
--To
SELECT #TarnsportExemption = MIN(A) FROM (
SELECT #MinConveyance A
UNION ALL
SELECT #MinTransport
)AS AA
In SQL, MIN function will return you minimum value of a column from selected list; so you can use MIN only in inline queries.
e.g. Select min(Salary) from Tbl_Employee
So, in your case either you can use case when then or union all to get minimum value from two variables as:-
SET #TarnsportExemption = CASE WHEN #MinConveyance < #MinTransport THEN #MinConveyance ELSE #MinTransport END
OR
SELECT #TarnsportExemption = MIN(TEMPS.[VALUE])
FROM (
SELECT #MinConveyance AS VALUE
UNION ALL
SELECT #MinTransport AS VALUE
) AS TEMPS
I need to find how many true bit exists in my binary value.
example:
input: 0001101 output:3
input: 1111001 output:5
While both answers work, both have issues. A loop is not optimal and destructs the value. Both solutions can not be used in a select statement.
Possible better solution is by masking together as follows
select #counter = 0
+ case when #BinaryVariable2 & 1 = 1 then 1 else 0 end
+ case when #BinaryVariable2 & 2 = 2 then 1 else 0 end
+ case when #BinaryVariable2 & 4 = 4 then 1 else 0 end
+ case when #BinaryVariable2 & 8 = 8 then 1 else 0 end
+ case when #BinaryVariable2 & 16 = 16 then 1 else 0 end
+ case when #BinaryVariable2 & 32 = 32 then 1 else 0 end
+ case when #BinaryVariable2 & 64 = 64 then 1 else 0 end
+ case when #BinaryVariable2 & 128 = 128 then 1 else 0 end
+ case when #BinaryVariable2 & 256 = 256 then 1 else 0 end
+ case when #BinaryVariable2 & 512 = 512 then 1 else 0 end
This can be used in a select and update statement. It is also an order of magnitude faster. (on my server about 50 times)
To help you might want to use the following generator code
declare #x int = 1, #c int = 0
print ' #counter = 0 ' /*CHANGE field/parameter name */
while #c < 10 /* change to how many bits you want to see */
begin
print ' + case when #BinaryVariable2 & ' + cast(#x as varchar) + ' = ' + cast(#x as varchar) + ' then 1 else 0 end ' /* CHANGE the variable/field name */
select #x *=2, #c +=1
end
Also as further note: if you use a bigint or go beyond 32 bits it is necessary to cast like follows
print ' + case when #Missing & cast(' + cast(#x as varchar) + ' as bigint) = ' + cast(#x as varchar) + ' then 1 else 0 end '
Enjoy
DECLARE #BinaryVariable2 VARBINARY(10);
SET #BinaryVariable2 = 60; -- binary value is 111100
DECLARE #counter int = 0
WHILE #BinaryVariable2 > 0
SELECT #counter +=#BinaryVariable2 % 2, #BinaryVariable2 /= 2
SELECT #counter
Result:
4
I've left various debug selects in.
begin
declare #bin as varbinary(20);
declare #bitsSet as int;
set #bitsSet = 0;
set #bin = convert(varbinary(20), 876876876876);
declare #i as int;
set #i = 0
select LEN(#bin), 'Len';
while #i < LEN(#bin)
begin
declare #bit as varbinary(1);
set #bit = SUBSTRING(#bin, #i, 1);
select #bit, 'Bit';
declare #power as int
set #power = 0;
while #power < 8
begin
declare #powerOf2 as int;
set #powerOf2 = POWER(2, #power);
if #powerOf2 <> 0
set #bitsSet = #bitsSet + (#bit & #powerOf2) / #powerOf2; -- edited to add the divisor
select #power, #powerOf2;
set #power = #power + 1;
end;
select #bitsSet;
set #i = #i + 1;
end;
select #bitsSet, 'End'
end;
Cheers -
You can handle an arbitrary length binary value by using a recursive CTE to split the data into a table of 1-byte values and counting all of the bits that are true in each byte of that table...
DECLARE #data Varbinary(MAX) = Convert(Varbinary(MAX), N'We can count bits of very large varbinary values without a loop or number table if you like...');
WITH each ( byte, pos ) AS (
SELECT Substring(#data, Len(#data), 1), Len(#data)-1 WHERE Len(#data) > 0
UNION ALL
SELECT Substring(#data, pos, 1), pos-1 FROM each WHERE pos > 0
)
SELECT Count(*) AS [True Bits]
FROM each
CROSS JOIN (VALUES (1),(2),(4),(8), (16),(32),(64),(128)) [bit](flag)
WHERE each.byte & [bit].flag = [bit].flag
OPTION (MAXRECURSION 0);
From SQL Server 2022 you can just use SELECT BIT_COUNT(input)
expression_value can be
Any integer or binary expression that isn't a large object (LOB).
For integer expressions the result can depend on the datatype. e.g. -1 as smallint has a binary representation of 1111111111111111 (two's complement) and will have more bits set for int datatype.
i want to get only the top x number of the query. where the x will be send by parameter how can i do this?
I know i can take it as string and exec it later but i find it very cumbersome.
This the query i have and #ArticleNo is the parameter i want to take as x.
Create proc [dbo].[GW2_Report_SlowFastMovingArticle]
#ArtKey bigint = null,
#ArtCatKey int= null,
#ArtGroupKey int= null,
#ArtTypeKey int= null,
#MaterialKey int= null,
#ColorKey int= null,
#VendorTypeKey int = null,
#VendorKey bigint = null,
#FromDate datetime = null,
#ToDate datetime = null,
#MovingType int = 0,
#PerformanceType int = 0,
#ArticleNo int = 10,
#OutletKey int = null
AS
BEGIN
SELECT
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
if(#PerformanceType=0 and #MovingType=0)
begin
select * from #temp
order by Pair asc
end
if(#PerformanceType=0 and #MovingType=1)
begin
select * from #temp
order by Pair desc
end
if(#PerformanceType=1 and #MovingType=0)
begin
select * from #temp
order by turnover asc
end
if(#PerformanceType=1 and #MovingType=1)
begin
select * from #temp
order by turnover desc
end
END
Try this...
select TOP(#ArticleNo) *
from #temp
Use:
SELECT TOP(#ArticleNo)
Therefore:
SELECT TOP(#ArticleNo)
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
Alternatively, add the following before your SELECT query:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT #ArticleNo
END
Then after your SELECT query you need to reset the ROWCOUNT by doing:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT 0
END
Therefore, overall it will be something like:
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT #ArticleNo
END
SELECT
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName,
Sum(isnull(dbo.Sal_POSDet.Qty,0))as Pair,
Sum(isnull(dbo.Sal_POSDet.Total,0)) as TurnOver
into #temp FROM
dbo.Sal_POS INNER JOIN
dbo.Sal_POSDet ON
dbo.Sal_POS.SalesKey = dbo.Sal_POSDet.SalesKey INNER JOIN
dbo.ArtWithVendor ON
dbo.Sal_POSDet.ArtKey = dbo.ArtWithVendor.ArtKey
WHERE
Sal_POS.IsHold=0 and
Sal_POS.SalesDate between #FromDate and #ToDate and
CASE WHEN #ArtKey is null THEN 1 WHEN ArtWithVendor.ArtKey =#ArtKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtCatKey is null THEN 1 WHEN ArtWithVendor.ArtCatKey =#ArtCatKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtGroupKey is null THEN 1 WHEN ArtWithVendor.ArtGroupKey = #ArtGroupKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ArtTypeKey is null THEN 1 WHEN ArtWithVendor.ArtTypeKey = #ArtTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #MaterialKey is null THEN 1 WHEN ArtWithVendor.MaterialKey = #MaterialKey THEN 1 ELSE 0 END = 1
and CASE WHEN #ColorKey is null THEN 1 WHEN ArtWithVendor.ColorKey = #ColorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorKey is null THEN 1 WHEN ArtWithVendor.VendorKey = #VendorKey THEN 1 ELSE 0 END = 1
and CASE WHEN #VendorTypeKey is null THEN 1 WHEN ArtWithVendor.VendorTypeKey = #VendorTypeKey THEN 1 ELSE 0 END = 1
and CASE WHEN #OutLetKey is null THEN 1 WHEN Sal_POS.OutLetKey = #OutLetKey THEN 1 ELSE 0 END = 1
Group by
dbo.Sal_POSDet.ArtKey,
dbo.ArtWithVendor.ArtCode,
dbo.ArtWithVendor.ArtName
IF #ArticleNo IS NOT NULL
BEGIN
SET ROWCOUNT 0
END
However using ROWCOUNT is not ideal as it will mess with your sub-query results.
I have three integer variables: firstCount, secondCount, thirdCount. I need to compare it and to output the result. I'm bad at SQL, but C# code is something like this:
if(firstCount == secondCount == thirdCount)
return true;
else
return false;
One way (2008 syntax).
SELECT CAST(CASE
WHEN COUNT(DISTINCT C) = 1 THEN 1
ELSE 0
END AS BIT)
FROM (VALUES (#firstCount),
(#secondCount),
(#thirdCount)) t (C)
declare #first int
, #second int
, #third int
select #first = 0
, #second = 0
, #third = 0
select case when (#first = #second) AND (#second = #third) THEN 1 ELSE 0 END
Following is one way to do it but after reading #meagar's comment, I find that solution to be more elegant. Just waiting for him to turn it into an answer...
DECLARE #firstcount INTEGER
DECLARE #secondcount INTEGER
DECLARE #thirdcount INTEGER
SET #firstcount = 1
SET #secondcount = 2
SET #thirdcount = 3
IF #firstcount <> #secondcount SELECT 0
ELSE IF #secondcount <> #thirdcount SELECT 0
ELSE SELECT 1
You are wanting to say If Firstcount is equal to second count, and secondcount is equal to third count return true.
You can just do
DECLARE #a INT = 2
DECLARE #b INT = 2
DECLARE #c INT = 2
IF (#a = #b AND #a = #c)
BEGIN
Print('true')
END
ELSE
BEGIN
Print('false')
END
Although it makes no difference you dont need to test B = C because A = C is exactly the same thing since all 3 values have to be the same.
Try the code ,
if ( (firstCount = secondCount) and (secondCount = thirdCount) and (firstCount = ThirdCount) )
return true;
else
return false;