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;
Related
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
I am looking to count the number of times set of values occurred in a table. These values could occur in up to 10 different columns. I need to increment the count regardless of which column it is in. I know how I could count if they were all in the same column but not spanning multiple columns.
Values can be added in any order. I have about a thousand
Cpt1 Cpt2 Cpt3 Cpt4 Cpt5
63047 63048 63048 NULL NULL
I would want to for this row I'd expect this as the result
63047 1
63048 2
You could use a union all call to treat them as one column:
SELECT col, COUNT(*)
FROM (SELECT col1 FROM mytable
UNION ALL
SELECT col2 FROM mytable
UNION ALL
SELECT col3 FROM mytable
-- etc...
) t
GROUP BY col
It's not entirely clear what your table exactly looks like, but I'm guessing that what you're looking for is:
SELECT row_count = COUNT(*),
row_count_with_given_value = SUM ( CASE WHEN field1 = 'myValue' THEN 1
WHEN field2 = 'myValue' THEN 1
WHEN field3 = 'myValue' THEN 1
WHEN field4 = 'myValue' THEN 1 ELSE 0 END)
FROM myTable
Assuming the fieldx columns are not NULL-able, you could write it like this too:
SELECT row_count = COUNT(*),
row_count_with_given_value = SUM ( CASE WHEN 'myValue' IN (field1, field2, field3, field4) THEN 1 ELSE 0 END)
FROM myTable
Something like this might work (after adapting to your value domain and data types):
create table t1
(i1 int,
i2 int,
i3 int);
insert into t1 values (1,0,0);
insert into t1 values (1,1,1);
insert into t1 values (1,0,0);
declare #i int = 0;
select #i = #i + i1 + i2 + i3 from t1;
print #i;
drop table t1;
Output is: 5
Many databases support lateral joins, of one type of another. These can be used to simplify this operation. Using the SQL Server/Oracle 12C syntax:
select v.cpt, count(*)
from t cross apply
(values (cpt1), (cpt2), . . .
) v(cpt)
where cpt is not null
group by v.cpt;
I have three tables:
Table-1: Column1 | Column2 | Column 3
Table-2: Column4 | Column5 | ColumnUpdate
Table-3: Column7 | Column8
I need to copy some rows from Table-2 to Table-3 based on some conditions with Table-1:
My insert-statement looks like this:
INSERT INTO Table-3 (
Column7,
Column8)
SELECT Table-2.COLUMN4, Table-2.COLUMN5
FROM Table-2 INNER JOIN Table-1
ON Table-2.COLUMN4 = TABLE-1.Column1;
However I want to update column: ColumnUpdate" (Table-2) of the selecting row to "1".
So I select some rows and immediately want to update a process column in that row as '1'.
I don't know how to do that. I saw some examples with "OUTPUT" clause or "UPDATE FOR" but I dont exactly know how to use them in my statement.
MERGE
INTO target_table t1
USING (SELECT col1, col2
FROM source_table
WHERE //conditions here) s1
ON (t1.id = s1.id)
WHEN MATCHED THEN
UPDATE SET column_update = '1'
WHEN NOT MATCHED THEN
INSERT (col1, col2)
VALUES (s1.col1, s1.col2)
WHERE (// condition here);
UPDATE
BEGIN
FOR temp_var IN (
SELECT * from table_a
WHERE table_a.col1 = table_b.col1)
LOOP
// INSERTING INTO TARGET TABLE
INSERT INTO table_b
VALUES(temp_var.col1, temp_var.col2);
// UPDATING SOURCE TABLE
UPDATE table_a
SET status = 'COPIED'
WHERE col1 = temp_var.col1;
END LOOP;
END;
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 ....
I want to be able to compare 2 records in the same SQL table and tell if they are different. I do not need to tell what is different, just that they are different.
Also, I only need to compare 7 of 10 columns in the records. ie.) each record has 10 columns but I only care about 7 of these columns.
Can this be done through SQL or should I get the records in C# and hash them to see if they are different values?
You can write a group by query like this:
SELECT field1, field2, field3, .... field7, COUNT(*)
FROM table
[WHERE primary_key = key1 OR primary_key = key2]
GROUP BY field1, field2, field3, .... field7
HAVING COUNT(*) > 1
That way you get all records with same values for field 1 to 7, along with the number of occurrences.
Add the part between brackets to limit your search for duplicates, either with OR, or with IN (...).
IF EXISTS (SELECT Col1, Col2, ColEtc...
from MyTable
where condition1
EXCEPT SELECT Col1, Col2, ColEtc...
from MyTable
where condition2)
BEGIN
-- Query returns all rows from first set that are not column for column
-- also in the second (EXCEPT) set. So if there are any, there will be
-- rows returned, which meets the EXISTS criteria. Since you're only
-- checking EXISTS, SQL doesn't actually need to return columns.
END
No hash is necessary. Normal equality comparison is enough:
select isEqual = case when t1.a <> t2.a or t1.b <> t2.b bbb then 1 else 0 end
SELECT
CASE WHEN (a.column1, a.column2, ..., a.column7)
= (b.column1, b.column2, ..., b.column7)
THEN 'all 7 columns same'
ELSE 'one or more of the 7 columns differ'
END AS result
FROM tableX AS a
JOIN tableX AS b
ON t1.PK = #PK_of_row_one
AND t2.PK = #PK_of_row_two
Can't you just use the DISTINCT keyword? All duplicates will not be returned, so each row you receive is unique (and different from the others).
http://www.mysqlfaqs.net/mysql-faqs/SQL-Statements/Select-Statement/How-does-DISTINCT-work-in-MySQL
So you could make this query:
SELECT DISTINCT x,y,z FROM RandomTable WHERE x = something
Which will only return one row for each unique x,y,z combination.