I'm trying to create a new row when values between two columns are different.
Data sample:
SELECT * FROM SOURCE_TABLE
Info Date Observed Boolean
QWE 2020-03-08 2020-03-08 true
QWE 2020-03-14 2020-03-16 true
QWE 2020-03-20 2020-03-20 false
I'm trying to write a statement that would make a new row when the Date and Observed columns have a different value. In the final table I'd keep only the Date column, without Observed, and have a new Boolean_date and Boolean_observed columns with true/false values.
Other columns would stay the same value (keep the value from the row where the two columns differ).
So the new result would look like this:
Info Date Observed Boolean_date Boolean_observed
QWE 2020-03-08 2020-03-08 true true
QWE 2020-03-14 2020-03-16 true false
QWE 2020-03-16 2020-03-16 false true
QWE 2020-03-20 2020-03-20 false false
In SQL Server I would use CROSS APPLY in this case, but that is not available in Snowflake.
I was thinking something along the lines of:
SELECT
...
CASE
WHEN Boolean = true AND "Date" = Observed THEN Boolean_date = true AND Boolean_observed = true
WHEN Boolean = false AND "Date" = Observed THEN Boolean_date = false AND Boolean_observed = false
WHEN Boolean = true AND "Date" <> Observed THEN (INSERT NEW ROW)
END
FROM SOURCE_TABLE
If you have any other approach that would work better, please suggest it.
Not sure this exactly represents what you need, but here goes
with st(info, date, observed, boolean) as
(select * from values
('QWE','2020-03-08'::date, '2020-03-08'::date, True),
('QWE','2020-03-14'::date, '2020-03-16'::date, True),
('QWE','2020-03-20'::date, '2020-03-20'::date, False))
select
coalesce(t0.info, t1.info) info,
coalesce(t0.date, t1.observed) date,
coalesce(t0.observed, t1.observed) observed,
coalesce(t0.boolean, False) as boolean_date,
case
when coalesce(t0.boolean, True)
and coalesce(t0.date, t1.observed) = coalesce(t0.observed, t1.observed) then True
else False
end as boolean_observed
from st t0
full outer join st t1
on t0.date = t1.observed
order by 2
INFO
DATE
OBSERVED
BOOLEAN_DATE
BOOLEAN_OBSERVED
QWE
2020-03-08
2020-03-08
True
True
QWE
2020-03-14
2020-03-16
True
False
QWE
2020-03-16
2020-03-16
False
True
QWE
2020-03-20
2020-03-20
False
False
Related
New to the Jetbrains Exposed Framework here. I have an oracle table with 3 fields in it.
ID
TIMESTAMP
FLAG
1234
May1 12AM
Y
1234
May1 3PM
N
I want to query the max timestamp row where FLAG = Y, but I'm not sure how to do this.
Employee.run {
val results = select {(id eq employeeId) and (flag eq "Y")}
// If no row with Flag = Y then I guess I can just return if results are empty
!results.empty()
}
I'm not sure how to check for a max timestamp in this.
Ended up figuring out a way to do it. I'm sure there's a better way with both Kotlin and Expose.
fun Person.hasFlag(employeeId: String): Boolean {
return transaction(db) {
Employee.run {
val results = slice(id, timeStamp, flag).select {(id eq employeeId)}.groupBy(id, timeStamp, flag)
if (results.empty()) false else results.map {
Pair(it[timeStamp], it[flag])
}.maxByOrNull { m -> m.first }?.second == "Y"
}
}
}
I need to compare the values of 2 tables in SQL Server. I have created script but the script I've made is only comparing the whole table as one. The one I needed is comparing each row of 2 tables. Here is my script I've made
import groovy.sql.Sql
import java.sql.ResultSet
import com.eviware.soapui.support.GroovyUtils
import groovy.sql.DataSet
def sql = Sql.newInstance("jdbc:sqlserver://MSSQLSERVER01:1433",
"User", "password", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
resultsA = sql.eachRow("SELECT * FROM Bikestores.production.brands"){
temp1=it.toRowResult().values().join(", ")
}
resultsB = sql.eachRow("SELECT * FROM Bikestores.dbo.sample"){
temp2=it.toRowResult().values().join(", ")
}
while(temp1 != null && temp2 != null){
brandA = temp1
brandB = temp2
if(brandA==brandB){
log.info "True"
break
}
else{
log.info "False"
break
}
}
sql.close()
The result of this is False since there is a difference in values.
I need to compare each row, like I will get 9 results since I have a 9 rows. If you have better scripts please let me know. Thank you.
Edit:
Sample data
Table 1
id|name | check
1 | abc | yes
2 | def | yes
3 | ghi | yes
4 | jkl | yes
Table 2
id|name | check
1 | abc | yes
2 | def | yes
3 | pqr | yes
4 | mno | yes
Expected Result
[table1_name][table2_name]: true
[table1_name][table2_name]: true
[table1_name][table2_name]: false
[table1_name][table2_name]: false
Somewhat straight-forward way:
...
List<String> tableA = sql.rows("SELECT * FROM Bikestores.production.brands")*.join(',')
sql.rows("SELECT brand_id FROM Bikestores.dbo.sample")*.join(',').each{
log.info "$it equals -> ${tableA.contains( it )}"
}
My Thoughts :
- The right and suggestible way is filtering by sql query itself like select * from table1,table2 where field_1 == field_2. u don't need outside computation.
Or If u really want to do it by code. please try to share the sample table data and expected output format. then only we can help u.
Answer:
ur code not collecting the results, it just replaces the latest line of eachRow(). This is the modified code :
temp1 = []
temp2 = []
resultsA = sql.eachRow("SELECT id FROM Bikestores.production.brands"){
temp1.append(it[0])
}
resultsB = sql.eachRow("SELECT brand_id FROM Bikestores.dbo.sample"){
temp2.append(it[0]) // If u having one column why u getting values list
}
temp1.each{ brandA ->
temp2.each{ brandB ->
if(brandA==brandB){
log.info "$brandA : $brandB : True"
}
else{
log.info "$brandA : $brandB : False"
}
}
sql.close()
Note :
It seems the first query select * from not correct. It will provide all column values. (r u aware of this ?)
I don't know why u r joining all values on a row using .values().join(", ") when u r going to compare with brand_id. If i guess right brands table should have a column as id (intuitive).
I think u r not sure what u r getting. I hope this notes will clarify and encourage u to ask better question.
Update 1:
After seeing ur sample, i guess this is what u r expecting....
def getResult(query, sql) // method changed
{
temp = []
sql.eachRow(query){
temp.add(it.toRowResult()) // changed
}
temp
}
resultsA = getResult("SELECT id, name, check FROM Bikestores.production.brands", sql)
resultsB = getResult("SELECT id, name, check FROM Bikestores.dbo.sample", sql)
resultsA.each{ brandA ->
resultsB.each{ brandB ->
if (brandA.id.trim() == brandB.id.trim()) // condition only for id match
log.info "[${brandA.name}][${brandB.name}] : ${brandA.name == brandB.name}"
}
sql.close()
I cant seem to find a answer to this. I am using GUI to create a table and a column in there is called DuesPaid. I need this column boolan value to be false and it has the datatype of bit. How might one do this am I just missing where its at?
BIT is the type to store boolean values. 1 will mean true, and 0 will mean false. You can also compare it to strings 'true' and 'false'.
If you run this query:
SELECT IIF(CAST(1 AS BIT) = 'true', 'Yes', 'No') as [1 = true?]
, IIF(CAST(0 AS BIT) = 'false', 'Yes', 'No') as [0 = false?]
, IIF(CAST(0 AS BIT) = 'true', 'Yes', 'No') as [0 = true?]
You'll get the following:
1 = true? 0 = false? 0 = true?
--------- ---------- ---------
Yes Yes No
I have a problem want to check if column is null or false then it return 0 otherwise it returns 1, here my column is bit type. Below is my statement where I want to check:
FROM
[dbo].[M_AttributeSet] AttributeSet
WHERE
(#AD_Org_ID IS NULL OR AttributeSet.AD_Org_ID IN (Select ID From fnSplitter(#AD_Org_ID)))
AND (#Atleastonevalue IS NULL OR AttributeSet.Atleastonevalue =#Atleastonevalue )
How can I do this? Thanks for your comments in advance
this should return 0 when false or 0 and 1 when true:
ISNULL(yourcolumn,0)
In a SQL View, I have 5 boolean variables and one int variable.
out of 5 boolean variables only one variable will be true for a single data row,
Task Type boolVerySmall Datestart TagName
Architecture Setup -- Doc Code True 1900-01-01 00:00:00.000 Design_09
idProject boolsmall boolMedium boolLarge boolVeryLarge intHours
4 False False False False 0
The above data is for one row... in this row, when I download the data from database to excel sheet, I have to display only the value which is true (boolVerySmall ,boolsmall, boolMedium,boolLarge, boolVeryLarge, intHours) should be displayed in a single column,
I have written a Stored procedure for this. I am finding it difficult to get a particular row which has TRUE of intHours>0.
I am adding the sql query below. Please help me
SELECT dbo.tblResourceTaskList.txtTask, dbo.tblIndividualRelativeData.txtProductType, dbo.tblResourceTaskList.boolVerySmall,
dbo.tblResourceTaskList.dtActualCompletionDate, dbo.tblEffortCodes.txtTagName, dbo.tblResourceTaskList.txtTaskNotes, dbo.tblResourceTaskList.idSubProject, dbo.tblResourceTaskList.idLaunch, dbo.tblResourceTaskList.idResource, dbo.tblResourceTaskList.boolSmall, dbo.tblResourceTaskList.boolMedium, dbo.tblResourceTaskList.boolLarge, dbo.tblResourceTaskList.boolVeryLarge,
dbo.tblResourceTaskList.intDirectHours
FROM dbo.tblResourceTaskList INNER JOIN dbo.tblProjectUsers ON dbo.tblResourceTaskList.idSubProject = dbo.tblProjectUsers.idSubProject AND
dbo.tblResourceTaskList.idResource = dbo.tblProjectUsers.idUser INNER JOIN
dbo.tblLaunchInfo ON dbo.tblResourceTaskList.idLaunch = dbo.tblLaunchInfo.idLaunch INNER JOIN dbo.tblIndividualRelativeData ON dbo.tblResourceTaskList.idIndividualRelativeEffort = dbo.tblIndividualRelativeData.idIndividualRelativeEffort LEFT OUTER JOINdbo.tblEffortCodes ON dbo.tblResourceTaskList.idEffortCode = dbo.tblEffortCodes.idEffortCode
My stored procedure is
SELECT txtTask, txtProductType, boolVerySmall, txtTagName, txtTaskNotes,
dtActualCompletionDate
FROM tblResourceTaskList_View
WHERE (idSubProject = #idSubProjectIndex )
as the first row from db has TRUE for boolVerySmall, then SP should return this value and if its other than that it should give that value.
Instead of filling True in excel sheet, I , have to assign VS for boolVerySmall, S - Small, M - Medium, L - Large VL- VeryLarge in the excelsheet.
So please help me how to work on this.
1. Assigning shortnames (like VS... for the bool var's).
2. Returning only one value (boolverysmall or ... ) and assigning VS to that and fill it in excel.
3. if all the bools are false, then intHours should be assinged...
Kindly help me.
Thanks
Ramm
I am able to assign values to SP using CASE statements.
Now, its workign fine.
Thanks
Ramm