SQL Server Replicate - sql

I have to SELECT data from a table using REPLICATE function in such a way that if the Field 4 has Numeric data then in the select statement that data should appear in 10th column. If the Field 4 is not numeric then that value should appear in 20th column and the 10th column should be blank.
The table has data as follows:
Field1 field2 Field3 Field4
1 a b 205
2 s t A25
Any advice on how to do this please.

Why are you using REPLICATE for this? Check if the value for Field4 is numeric, and if so add in Field10, if not then add in Field20.
SELECT Field1, Field2, Field3, Field4,
/*... other columns... */
CASE WHEN isnumeric(Field4) THEN Field4 ELSE null END AS Field10,
/*... other columns */
CASE WHEN isnumeric(Field4) THEN NULL ELSE Field4 END as Field20
FROM myTable
WHERE ....

Related

SQL: select 3 values and count how many times they are different

Say that you need to query some data and that there are three fields like the following (this is part of a larger query):
Field1, Field2, Field3.
So you select them like this:
SELECT Field1=MyTable.Field1, Field2=MyTable.Field2, Field3=MyTable.Field3
FROM MyTable
I need to compare these values and return the variable Result that is computed as follows:
0 if they are all the same
1/2 if two are the same and one is different
1 if they are all different.
How should I restructure my query? I think I need a subquery but I am not sure how to structure it.
You can use case:
select (case when field1 = field2 and field1 = field3 then 0
when field1 in (field2, field3) or field2 = field3 then 0.5
else 1
end) as result

BigQuery: Checking for null in conditional expression

Is it possible to perform a conditional if statement to collapse 3 fields into a single field? I am trying to achieve following. field1, field2 and field3 are are Int64 which is nullable. I could not find a way to do a null check so am checking if a positive value is assigned to any of the fields then set the a field to respective values. When syntax below I am getting error below:
case when field1 >= 0 then 0 end as field,
case when field2 >= 0 then 1 end as field,
case when field3 >= 0 then 2 end as field
Duplicate column names in the result are not supported. Found
duplicate(s): field
Is this what you want?
(case when field1 is not null then 0
when field2 is not null then 1
when field3 is not null then 2
end) as null_check
Or, if you want to turn this into a coded field:
concat(case when field1 is not null then 0 end,
case when field2 is not null then 1 end,
case when field3 is not null then 2 end,
) as null_check
This will list "0", "1", "2" in a string, depending on the values that are not NULL.
It sounds like perhaps you just want a CASE expression which can generate multiple values based on multiple conditions:
CASE WHEN field3 >= 0 THEN 2
WHEN field2 >= 0 THEN 1
WHEN field1 >= 0 THEN 0 END AS field
The logic here is that it will check field3 first, then field2, followed by field1, when determining which output to generate.

Always display a group of data first, and then order the rest by one of column

I have a table with 3 fields, let us say field1, field2, and field3. The value in field2 is either 0 or 1.
I am trying to fetch field1 in a way to always have all the rows where the value is 1 in field2 displayed first and arranged by field3, and then display the rest of data, also arranged by field3.
My research told me one can order by two fields, let us say 'Order by field2 Desc, field 3' but this really is not giving the expected result. Any idea?
One small change is required for MySQL:
ORDER BY field2 = 1 DESC, field3
Or for standard SQL:
ORDER BY CASE WHEN field2 = 1 THEN 1 ELSE 0 END DESC, field3
try this:
order by
case when field2=1 then 0 else 1 end,
field3

sql query using having clause with no results not returning null

If I insert the following values into my table
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 1, 100, 5, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 2, 100, 99, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 3, 100, 3, "5/10/2012")
What my query is going to try and get is the value from Field4 with the maximum value in Field2 by a certain date.
Example
select isnull(Field4,0) from Table1
where Field1 = 1 and Field3 = 100 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
Which works great. I get 3 which is expected. Now this is where my questions comes from. It is possible Field3 could have other values, such as 110. When I run that query
select isnull(Field4,0) from Table1
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
I don't get any results. It should be null, and the isnull(Field4,0) should then spit out 0. But it doesn't. I've tried replacing the selection with count(*) to see if it returns 0, but it doesn't return anything. I'm at a loss. I need it to return 0 as this will be going into a temp table and then summed up with values from another table. Thanks.
Edit - New question part
I understand that I may have been using the isnull for the wrong thing here. Which I can accept. However, if I wanted to write a case statement to handle nothing being returned, it doesn't return 0 if no rows are returned.
select count(*) from Table1
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
The above code doesn't return anything, instead of 0 like I would think it would.
You do not get any results because none of the 3 records you inserted has 110 for Field3. As a result, no rows are returned from the query. Your ISNULL would only be used if instead of 5, 99, or 3, those values were NULL for a row returned.
If this record was in your table:
INSERT INTO Table1 (Field1, Field2, Field3, Field4, DateField)
VALUES(1, 3, 110, NULL, "5/10/2012")
This record would match your Field3 = 110 requirement, and then have Field4 Null, being set to 0 by your SELECT logic, but since it is not, nothing will be returned by your query.
Part 2
It appears that the HAVING is removing the 0 record from the result set since no records are left after the WHERE clause to match against the HAVING criteria.
If you want to see whether any results match your criteria, you can use the EXISTS clause
IF EXISTS(
SELECT COUNT(*)
FROM Table1 AS t
WHERE t.Field1 = 1
AND t.Field3 = 110
AND DATEDIFF(day, t.DateField, "5/21/2012") > 0
HAVING MAX(Field2) = Field2
)
BEGIN
--Code for when the record Exists
END
ELSE
BEGIN
--No records logic here
END

How to SELECT a column composed by some other columns value

I have a daycode column that stores values like 1,2...7
then in a different table I have cols like field1,field2...field7
I can join them on a key, but how do I select the specific fieldX column based on values passed?
Table 1 has the following columns
-------------
id
prodno
field1
field2
field3
field4
field5
field6
field7
Where each fieldX represents a value for monday, tuesday and so on till sunday.
Table 2 has the following columns
-------------
id
prodno
dt
daycode
Update
t2 has columns like field1, field2 ... field7, and daycode values is 1,2 ... 7. We need to concat "field" with the value taken from daycode column.
select table1.id,select [concat('field',table2.daycode)] from table1 join
table2 on table1.id=table2.key
You can create the statement in string an then execute it using execute (#sql)
or You can add a case statement in the select where You will pick the proper column.
Im not sure aobut this but you can try
SELECT t1.id,
CASE
WHEN daycode = 1 THEN t2.field1
WHEN daycode = 2 THEN t2.field2
WHEN daycode = 3 THEN t2.field3
WHEN daycode = 4 THEN t2.field4
END
FROM t1 join t2 on t1.id=t2.key;