How to check for continuous column count via datatable? - vb.net

I have a datatable below and I wanted to check the condition if the latest columns have total count of more then 7 then the function will do something but my current solution is only able to count the number of 1 in the datatable but unable to identify the value 1 is consecutive from column 7 to column 14.
-------------------------------------------------------------------
Id | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
-------------------------------------------------------------------
kek | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Solution
Sub CheckExtraday()
Dim count1 As Integer = 0
Dim sql As String = "Select * from LeaveSystem WHERE EmpID ='" + Session("empID") + "' and Year='" + Session("year") + "' and Month='" + Session("month") + "'"
Dim dt As DataTable = getDTms(sql)
For I As Integer = 0 To dt.Rows.Count - 1
For k As Integer = 2 To dt.Columns.Count - 1
If dt.Rows(I).Item(k).Equals("1") = True Then
count1 = count1 + 1
End If
Next k
Next I
If count1 > 7 Then 'output = 10
'do something
End If
End Sub
Edit :
consecutive count from month 5 column 11 to month 6 column 1-5
----------------------------------------------------------------------------
Id | month | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
----------------------------------------------------------------------------
kek | 5 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
kek | 6 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

If I understand you correctly, you seem to be trying to get the number of consecutive cells that have a value of 1. If so, you can simply reset the value of count1 whenever the cell's value is not 1:
If dt.Rows(I).Item(k).ToString() = "1" Then
count1 += 1
Else
count1 = 0
End If
If you want to keep the latest consecutive 1 values even if followed by zeros, you can do something like this:
Dim latestConsecutiveCount As Integer
If dt.Rows(I).Item(k).ToString() = "1" Then
count1 += 1
Else
latestConsecutiveCount = count1
count1 = 0
End If
If latestConsecutiveCount > someValue Then
' Do something
End If

Related

Update column according max value from other column SQL [duplicate]

This question already has an answer here:
SQL Set column with Key and max value from other colum
(1 answer)
Closed 1 year ago.
Taking the max value of column 'time', I need update the column 'register' group by Identification column, set register = 1 and the rest register = 0
Initial table:
Identification | time | register
1 | 0 | 0
1 | 7 | 0
1 | 3 | 0
2 | 10 | 0
2 | 5 | 0
2 | 0 | 0
3 | 6 | 0
3 | 5 | 0
3 | 0 | 0
Ending table:
Identification | time | register
1 | 0 | 0
1 | 7 | 1
1 | 3 | 0
2 | 10 | 1
2 | 5 | 0
2 | 0 | 0
3 | 6 | 1
3 | 5 | 0
3 | 0 | 0
In SQL 2017, the table contains thousand of registers
Seems like you want a CASE expression and a windowed MAX:
SELECT Identification,
[time],
CASE [time] WHEN MAX([time]) OVER (PARTITION BY Identification) THEN 1 ELSE 0 END AS register
FROM dbo.YourTable;

Combining 3 columns with boolean values into one

I've stumbled upon a table with three columns
id | isRed | isBlue | is Green
==============================
1 | 1 | 0 | 0
2 | 1 | 0 | 0
3 | 1 | 0 | 0
4 | 0 | 1 | 0
5 | 0 | 1 | 0
6 | 0 | 1 | 0
7 | 0 | 0 | 1
8 | 0 | 1 | 0
9 | 0 | 0 | 1
10 | 0 | 0 | 0
I want to create query as simple as possible to transform it into something like that:
id | Color
==============================
1 | red
2 | red
3 | red
4 | blue
5 | blue
6 | blue
7 | green
8 | blue
9 | green
10 | 0
The values can't be true in two different columns and I can't alter the database. I need it to append it to quite long query with as little complication as possible. Any ideas?
With a select and case:
select id,
(case when isRed = 1 then 'red'
when isBlue = 1 then 'blue'
when isGreen = 1 then 'green'
else '0'
end) as color
from t;

Postgres crosstab query

I have a table which has 7 different classes with an area value.
pid | class| area |
----+------+------+
2 | 1 | 10 |
2 | 2 | 10 |
2 | 6 | 20 |
4 | 1 | 30 |
4 | 2 | 40 |
4 | 3 | 50 |
4 | 4 | 60 |
4 | 5 | 70 |
9 | 6 | 80 |
11 | 1 | 90 |
11 | 4 | 10 |
11 | 7 | 20 |
However I want to present this data in a format that has each distinct pid as a column heading and then have each row correspond to a class area (i.e. first row is the area of class 1 for each pid).
2 | 4 | 9 | 11 |
---+-----+-----+----+
10 | 30 | 0 | 90 |
10 | 40 | 0 | 0 |
0 | 50 | 0 | 0 |
0 | 60 | 0 | 10 |
0 | 70 | 0 | 0 |
20 | 0 | 60 | 0 |
0 | 0 | 0 | 20 |
Is it possible to create an output like this in PostgreSQL?
Try this:
SELECT
SUM(CASE WHEN pid = 2 THEN area ELSE 0 END) As "2",
SUM(CASE WHEN pid = 4 THEN area ELSE 0 END) As "4",
SUM(CASE WHEN pid = 9 THEN area ELSE 0 END) As "9",
SUM(CASE WHEN pid = 11 THEN area ELSE 0 END) As "11"
FROM t
GROUP BY class
ORDER BY class

dynamic html table with column span and row span in vb.net

If I have a value from sqlDataReader as below:
| YEAR1 | NAM1 | CRDT1 | SEMER1 | YEAR2 | NAM2 | CRDT2 | SEMER2 |
-----------------------------------------------------------------------
| 1 | Name1 | 1 | 1 | 1 | Name10 | 1 | 2 |
| 1 | Name2 | 4 | 1 | 1 | Name5 | 4 | 2 |
| 1 | Name3 | 2 | 1 | 1 | Name6 | 3 | 2 |
| 1 | Name4 | 7 | 1 | 1 | Name7 | 6 | 2 |
| (null) | (null) | (null) | (null) | 1 | Name8 | 1 | 2 |
| (null) | (null) | (null) | (null) | 1 | Name9 | 1 | 2 |
| 2 | Name11 | 3 | 1 | 2 | Name14 | 2 | 2 |
| 2 | Name12 | 6 | 1 | 2 | Name15 | 1 | 2 |
| 2 | Name13 | 4 | 1 | 2 | Name16 | 1 | 2 |
| (null) | (null) | (null) | (null) | 2 | Name17 | 1 | 2 |
| 3 | Name18 | 5 | 1 | 3 | Name18 | 5 | 2 |
| 3 | Name19 | 1 | 1 | 3 | Name19 | 1 | 2 |
| 3 | Name20 | 1 | 1 | 3 | Name20 | 1 | 2 |
I like to merge year column to become only one column. If any columns is null then it will merge into one column and If any the current rows and the next rows is null then it will merge.
I want to output like this:
| YEAR1 | NAM1 | CRDT1 | SEMER1 | YEAR2 | NAM2 | CRDT2 | SEMER2 |
-----------------------------------------------------------------------
| | Name1 | 1 | 1 | | Name10 | 1 | 2 |
| 1 | Name2 | 4 | 1 | 1 | Name5 | 4 | 2 |
| | Name3 | 2 | 1 | | Name6 | 3 | 2 |
| | Name4 | 7 | 1 | | Name7 | 6 | 2 |
| (null) | | Name8 | 1 | 2 |
| | | Name9 | 1 | 2 |
| | Name11 | 3 | 1 | | Name14 | 2 | 2 |
| 2 | Name12 | 6 | 1 | 2 | Name15 | 1 | 2 |
| | Name13 | 4 | 1 | | Name16 | 1 | 2 |
| (null) | | Name17 | 1 | 2 |
| | Name18 | 5 | 1 | | Name18 | 5 | 2 |
| 3 | Name19 | 1 | 1 | 3 | Name19 | 1 | 2 |
| | Name20 | 1 | 1 | | Name20 | 1 | 2 |
How can I create a html table which can output the result as above in vb.net?
Spin through the result set adding a new table row for each record. Use variables to keep track of when you need to add all of your cells (some with rowspans) or just the ones displayed on every row. Here's 90% of the code for you. You should be able to figure out the rest.
'Convert DataReader into DataTable.
Dim dtResults As New DataTable
dtResults.Load(drResults)
'You can define this in your .Aspx
Dim tblStudents As New Table
'These will help you with your table building logic.
Dim intCurrentYear1 As Integer = 0
Dim intRowSpan As Integer = 0
Dim bolDetermineRowSpan = True
For intRowCursor As Integer = 0 To (dtResults.Rows.Count - 1)
If bolDetermineRowSpan Then
'First get the current year (Nulls will be set to 0, but not displayed as such.)
If dtResults.Rows(intRowCursor).Item("Year1") Is DBNull.Value Then
intCurrentYear1 = 0
Else
intCurrentYear1 = CInt(dtResults.Rows(intRowCursor).Item("Year1"))
End If
If intCurrentYear1 > 0 Then
'Get the total number of records with this year, so we know how many cells to merge.
intRowSpan = (From d As DataRow In dtResults.Rows _
Where Not d.Item("Year1") Is DBNull.Value AndAlso _
CInt(d.Item("Year1")) = intCurrentYear1).Count()
Else
'Figure out how many null records until the next year, so we know how many to merge.
Dim bolNextYear As Boolean = False
Dim intTempCursor As Integer = intRowCursor + 1
intRowSpan = 1
Do Until bolNextYear = True OrElse intTempCursor = dtResults.Rows.Count
If Not dtResults.Rows(intTempCursor).Item("Year1") Is DBNull.Value Then
bolNextYear = True
End If
intTempCursor += 1
intRowSpan += 1
Loop
End If
End If
Dim tr As New TableRow
If intCurrentYear1 > 0 Then
If bolDetermineRowSpan = True Then
'Add all cells to this Table Row, using RowSpan property to merge desired fields.
Dim tdYear1 As New TableCell
tdYear1.RowSpan = intRowSpan
tdYear1.Text = intCurrentYear1
tdYear1.VerticalAlign = VerticalAlign.Middle
tr.Cells.Add(tdYear1)
Dim tdName1 As New TableCell
tdName1.Text = CStr(dtResults.Rows(intRowCursor).Item("NAM1"))
tr.Cells.Add(tdName1)
'Add the rest of your cells here (omitted).
'Update this variable to keep sound logic.
bolDetermineRowSpan = False
Else
'Do not add the Year1 Cell because of RowSpan/"Merging".
Dim tdName1 As New TableCell
tdName1.Text = CStr(dtResults.Rows(intRowCursor).Item("NAM1"))
tr.Cells.Add(tdName1)
'Do for rest of cells.
'Update this variable to keep sound logic.
bolDetermineRowSpan = False
End If
Else
'Same logic as other half of this If Statement block, just doing more
'cells with RowSpans. (These are the null records.)
If bolDetermineRowSpan = True Then
'Add all cells (with rowspans)
Else
'Add just cells that are displayed on every row.
End If
End If
'Add the row to the table.
tblStudents.Rows.Add(tr)
'Do we need to reset our boolean flag to determine row span?
If bolDetermineRowSpan = False AndAlso dtResults.Rows.Count < (intRowCursor + 1) AndAlso _
Not dtResults.Rows(intRowCursor + 1).Item("Year1") Is DBNull.Value AndAlso _
dtResults.Rows(intRowCursor + 1).Item("Year1") <> intCurrentYear1 Then
bolDetermineRowSpan = True
End If
Next

SSRS Remove Column from Report

i am including column 13 as a dummy column here:
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 2 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 4 | 1 | 0 | 0 | 20 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
+----+---+---+---+----+---+---+---+---+---+----+----+----+----+
the reason i am including a dummy column is so that IF columns 1 through 12 are all zero, i would still like to include an entry for that row.
as you can see row 1 would not have been included.
this report is generated by SSRS.
i am wondering if there is a way to HIDE column 13?
is there some kind of conditional formatting i can do?
to clarify here's my query:
select tat.*, tat.tat as tat2 from tat
it is organized in the report this way:
this data set [TAT] contains dummy data specifically for column 13
Specific columns in a column group can be hidden based on values with the following steps.
Right-click header of the column group you want to hide, Column Group -> Group Properties
Click on the Visibility pane and select Show or hide based on an expression radio button. Use an expression to determine when column is hidden.
True hides the column, False displays it. You will need to update the field name in my example to match your month field name.
Don't include column 13 in your select? If you are doing a select *, change it to Select col1, col2, ...., col12