Updating one column based on parameter from another column in mssl1 - sql-server

Please I want to update my client database based on the job type
id Job_type Meal_Ticket
---------------------------
1 x 20
2 2x 12
Meaning if I click on add 20 meal tickets on button click, it should update to this:
id Job_type Meal_Ticket
----------------------------
1 x 40
2 2x 52
I tried
UPDATE Staff
SET Rticket = CASE
WHEN Jobtype = 'x' THEN Rticket = SUM(Rticket + 20)
WHEN Jobtype = '2x' THEN Rticket = SUM(Rticket + 2*20)
ELSE Rticket
END

I think you want this:
UPDATE Staff
SET Rticket = CASE WHEN Jobtype = 'x' THEN Rticket + 20
WHEN Jobtype = '2x' THEN Rticket + 40 END
WHERE Jobtype IN ('x', '2x');
The only problem I see with your logic is that you are using SUM to add two quantities, when you should just be using the + operator.

Related

SQL Update statement updates one row multiple times, but is inconsistent

The first question ever on here.
I have an update statement, which seems to have been working perfectly fine, previously.
Out of nowhere, the update statement seems to update a single row multiple times, but not for all rows, if that makes sense?
The statement is run via SQL agent and scheduled every 10 seconds (along with other steps)
Sorry if it's big or messy, I'm self-trained!
The first step, is to insert data into a table that needs to be updated on the main table, using a view.
For the audit trail, I then insert IDs into another table, to track what's being updated.
The problematic part of this statement is the update main table (DT_V_POTATTENDANCE).
Below that is an insert into the main table where an ID cannot be found in the view.
The rest of the script again is all part of the audit trail which is how I've found it updating a single row multiple times for whatever reason (but not every time if that makes sense?) and setting the records as updated so they don't keep updating.
Thanks for your help, really appreciate it.
SELECT DISTINCT * INTO _TEMPTABLEAPPROVEDATTENDANCE
from BE_RPT_PA_ATTENDANCE_TO_UPDATE
SET IDENTITY_INSERT _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS ON
INSERT INTO _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS (SIGNID)
SELECT SIGNID FROM DT_PA_POTATTENDANCE WHERE APPROVED = 1 AND UPDATED IS NULL
SET IDENTITY_INSERT _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS OFF
declare #time datetime
set #time = (select getdate())
UPDATE DT_V_POTATTENDANCE
SET
DT_V_POTATTENDANCE.CB_H_MON = DT_V_POTATTENDANCE.CB_H_MON + Y.CB_H_MON,
DT_V_POTATTENDANCE.CB_M_MON = DT_V_POTATTENDANCE.CB_M_MON + Y.CB_M_MON,
DT_V_POTATTENDANCE.CB_H_TUE = DT_V_POTATTENDANCE.CB_H_TUE + Y.CB_H_TUE,
DT_V_POTATTENDANCE.CB_M_TUE = DT_V_POTATTENDANCE.CB_M_TUE + Y.CB_M_TUE,
DT_V_POTATTENDANCE.CB_H_WED = DT_V_POTATTENDANCE.CB_H_WED + Y.CB_H_WED,
DT_V_POTATTENDANCE.CB_M_WED = DT_V_POTATTENDANCE.CB_M_WED + Y.CB_M_WED,
DT_V_POTATTENDANCE.CB_H_THU = DT_V_POTATTENDANCE.CB_H_THU + Y.CB_H_THU,
DT_V_POTATTENDANCE.CB_M_THU = DT_V_POTATTENDANCE.CB_M_THU + Y.CB_M_THU,
DT_V_POTATTENDANCE.CB_H_FRI = DT_V_POTATTENDANCE.CB_H_FRI + Y.CB_H_FRI,
DT_V_POTATTENDANCE.CB_M_FRI = DT_V_POTATTENDANCE.CB_M_FRI + Y.CB_M_FRI,
DT_V_POTATTENDANCE.H_H_MON = DT_V_POTATTENDANCE.H_H_MON + Y.H_H_MON,
DT_V_POTATTENDANCE.H_M_MON = DT_V_POTATTENDANCE.H_M_MON + Y.H_M_MON,
DT_V_POTATTENDANCE.H_H_TUE = DT_V_POTATTENDANCE.H_H_TUE + Y.H_H_TUE,
DT_V_POTATTENDANCE.H_M_TUE = DT_V_POTATTENDANCE.H_M_TUE + Y.H_M_TUE,
DT_V_POTATTENDANCE.H_H_WED = DT_V_POTATTENDANCE.H_H_WED + Y.H_H_WED,
DT_V_POTATTENDANCE.H_M_WED = DT_V_POTATTENDANCE.H_M_WED + Y.H_M_WED,
DT_V_POTATTENDANCE.H_H_THU = DT_V_POTATTENDANCE.H_H_THU + Y.H_H_THU,
DT_V_POTATTENDANCE.H_M_THU = DT_V_POTATTENDANCE.H_M_THU + Y.H_M_THU,
DT_V_POTATTENDANCE.H_H_FRI = DT_V_POTATTENDANCE.H_H_FRI + Y.H_H_FRI,
DT_V_POTATTENDANCE.H_M_FRI = DT_V_POTATTENDANCE.H_M_FRI + Y.H_M_FRI,
DT_V_POTATTENDANCE.AA_H_MON = DT_V_POTATTENDANCE.AA_H_MON + Y.AA_H_MON,
DT_V_POTATTENDANCE.AA_M_MON = DT_V_POTATTENDANCE.AA_M_MON + Y.AA_M_MON,
DT_V_POTATTENDANCE.AA_H_TUE = DT_V_POTATTENDANCE.AA_H_TUE + Y.AA_H_TUE,
DT_V_POTATTENDANCE.AA_M_TUE = DT_V_POTATTENDANCE.AA_M_TUE + Y.AA_M_TUE,
DT_V_POTATTENDANCE.AA_H_WED = DT_V_POTATTENDANCE.AA_H_WED + Y.AA_H_WED,
DT_V_POTATTENDANCE.AA_M_WED = DT_V_POTATTENDANCE.AA_M_WED + Y.AA_M_WED,
DT_V_POTATTENDANCE.AA_H_THU = DT_V_POTATTENDANCE.AA_H_THU + Y.AA_H_THU,
DT_V_POTATTENDANCE.AA_M_THU = DT_V_POTATTENDANCE.AA_M_THU + Y.AA_M_THU,
DT_V_POTATTENDANCE.AA_H_FRI = DT_V_POTATTENDANCE.AA_H_FRI + Y.AA_H_FRI,
DT_V_POTATTENDANCE.AA_M_FRI = DT_V_POTATTENDANCE.AA_M_FRI + Y.AA_M_FRI
FROM _TEMPTABLEAPPROVEDATTENDANCE Y
WHERE DT_V_POTATTENDANCE.ATTENDANCEWEEKID = Y.ATTENDANCEWEEKID
AND Y.ATTENDANCEWEEKID IS NOT NULL
AND Y.TRAINEEID <> '0683-0001-107827'
INSERT INTO DT_V_POTATTENDANCE
([TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI])
SELECT [TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI]
FROM _TEMPTABLEAPPROVEDATTENDANCE
WHERE ATTENDANCEWEEKID IS NULL
AND TRAINEEID <> '0683-0001-107827'
UPDATE DT_PA_POTATTENDANCE
SET UPDATED = 1
FROM DT_PA_POTATTENDANCE
WHERE APPROVEDTIMESTAMP < #time and
TRAINEEID <> '0683-0001-107827' AND
APPROVED = 1 AND UPDATED IS NULL AND SIGNID IN (SELECT SIGNID FROM _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS)
INSERT INTO _TEMPTABLEAPPROVEDATTENDANCEUPDATED
([ATTENDANCEWEEKID],[TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI])
SELECT [ATTENDANCEWEEKID],[TRAINEEID]
,[POT]
,[WEEKSTARTDATE]
,[CB_H_MON]
,[CB_M_MON]
,[CB_H_TUE]
,[CB_M_TUE]
,[CB_H_WED]
,[CB_M_WED]
,[CB_H_THU]
,[CB_M_THU]
,[CB_H_FRI]
,[CB_M_FRI]
,[H_H_MON]
,[H_M_MON]
,[H_H_TUE]
,[H_M_TUE]
,[H_H_WED]
,[H_M_WED]
,[H_H_THU]
,[H_M_THU]
,[H_H_FRI]
,[H_M_FRI]
,[AA_H_MON]
,[AA_M_MON]
,[AA_H_TUE]
,[AA_M_TUE]
,[AA_H_WED]
,[AA_M_WED]
,[AA_H_THU]
,[AA_M_THU]
,[AA_H_FRI]
,[AA_M_FRI]
FROM _TEMPTABLEAPPROVEDATTENDANCE
WHERE TRAINEEID <> '0683-0001-107827'
drop table _TEMPTABLEAPPROVEDATTENDANCE
I found the reason why it was doing it!
Below part of my script was making it run and run again.
The user in question, we have an MIS that sets the field "Approved" to 1, the script above then updates the master table DT_V_POTATTENDANCE, but it wasn't setting the field "Updated" to 1 because the users time was 3 minutes out!
UPDATE DT_PA_POTATTENDANCE
SET UPDATED = 1
FROM DT_PA_POTATTENDANCE
WHERE APPROVEDTIMESTAMP < #time and
TRAINEEID <> '0683-0001-107827' AND
APPROVED = 1 AND UPDATED IS NULL AND SIGNID IN
(SELECT SIGNID FROM _TEMPTABLEAPPROVEDATTENDANCEUPDATEDSIGNIDS)
Thanks for all your help guys, appreciate it!

Django sum boolean fields horizontally

I have a django model that records multiple user preferences horizontally.
class Preferences(models.Model):
user = models.ForeignKey(CustomUser,on_delete='CASCADE')
choice1 = models.BooleanField()
choice2 = models.BooleanField()
choice3 = models.BooleanField()
choice4 = models.BooleanField()
choice5 = models.BooleanField()
I'm trying to achieve the SQL query of:
(select
(case when choice1 = True then 1 else 0 end) +
(case when choice2 = True then 1 else 0 end) +
(case when choice3 = True then 1 else 0 end) +
(case when choice4 = True then 1 else 0 end) +
(case when choice5 = True then 1 else 0 end) + as choice_sum
from Preferences)
How should I go about doing this in Django?
In case you want to know, I'm storing them horizontally as each user will have to record preferences for all choices which would increase in the future and I don't want multiple rows of unnecessary user FK.
Edit:
I realised my question might seem a bit weird. My objective is to eventually run a query that selects records where there is at least one True for any of the choices fields.
My objective is to eventually run a query that selects records where
there is at least one True for any of the choices fields.
Using Django filter + Q objects
from django.db.models import Q
.....
.....
choices = Preferences.objects.filter(Q(choice1=True) | Q(choice2=True) | Q(choice3=True) | Q(choice4=True) | Q(choice5=True))

Add incremental number in duplicate records

I have SSIS package, which retrieves all records including duplicates. My question is how to add an incremental value for the duplicate records (only the ID and PropertyID).
Eg
Records from a Merge Join
ID Name PropertyID Value
1 A 1 123
1 A 1 223
2 B 2 334
3 C 1 22
3 C 1 45
Now I need to append an incremental value at the end of the each record as
ID Name PropertyID Value RID
1 A 1 123 1
1 A 1 223 2
2 B 2 334 1
3 C 1 22 1
3 C 1 45 2
Since ID 1 & 3 are returned twice, the first record has RID as 1 and the second record as 2.
ID and PropertyID need to be considered to generate the Repeating ID i.e RID.
How can I do it in SSIS or using SQL command?
Update #1:
Please correct me if I'm wrong, since the data is not stored in any table yet, I'm unable to use the select query using rownumber(). Any way I can do it from the Merge Join?
You could use ROW_NUMBER:
SELECT ID,
Name,
PropertyID,
Value,
ROW_NUMBER() OVER(PARTITION BY ID, PropertyID ORDER BY Value) As RID
FROM TableName
This will do the job for you: https://paultebraak.wordpress.com/2013/02/25/rank-partitioning-in-etl-using-ssis/
You will need to write a custom script, something like this:
public
class
ScriptMain : UserComponent
{
string _sub_category = “”;
int _row_rank = 1;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.subcategory != _sub_category)
{
_row_rank = 1;
Row.rowrank = _row_rank;
_sub_category = Row.subcategory;
}
else
{
_row_rank++;
Row.rowrank = _row_rank;
}
}
}

Datagridview contents from 8 different tables? Vb.net MS SQL

Is it possible to display selected data from 8 different tables to a datagridview in visual basic? I am using MS SQL. I have 1 table for president, 1 table for vice president, 1 table for secretary, 1 table for treasurer. 1 table for auditor, and 1 table for the business manager. Each table has five rows ID, PARTY LIST, NAME, GRADE, and VoteCount.
What I need to display to datagridview is party list, the pres. name, vice. pres. name, sec. name, tre. name and so on.
I would like to know if it is possible for datagridview.
Thanks.
You need to add your column manually. I took this sample code from one of my app. it queries the format for each column from SQL and stores it in xgrid1 and xgrid2. one is the name of the column the other one is the Display name.
xgrid3 is the size of my column and xgrid4 is the alignment of the column. use whatever suits you. Once you have your columns setup then just runs you query and add rows to the grid using a loop. You cannot use datasource in your case.
use this to setup your grid
DG_Clients.Columns.Clear()
DG_Clients.RowHeadersVisible = False
For xcount = 0 To xgrid1.Length - 1
DG_Clients.Columns.Add(xgrid2(xcount), xgrid1(xcount))
DG_Clients.Columns(DG_Clients.Columns.Count - 1).Width = xgrid3(xcount)
Select Case xgrid4(xcount)
Case "L"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft
Case "R"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
Case "C"
DG_Clients.Columns(DG_Clients.Columns.Count - 1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
DG_Clients.Columns(DG_Clients.Columns.Count - 1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End Select
Next
and use this to add to it
Dim xRow As DataRow
Dim xProvince As String
For Each dRow As DataRow In DBAcc.DBDT.Rows
Dim xlist(DG_Clients.Columns.Count - 1) As String
For xcount = 0 To xgrid1.Length - 1
Select Case xgrid2(xcount)
Case "Sex"
If dRow(xgrid2(xcount)) = 0 Then xlist(xcount) = "Male" Else xlist(xcount) = "Female"
Case "Province"
Select Case dRow(xgrid2(xcount))
Case 0
xProvince = "QC"
Case 1
xProvince = "On"
Case 2
xProvince = "NS"
Case 3
xProvince = "PEI"
Case 4
xProvince = "NF"
Case 5
xProvince = "MB"
Case 6
xProvince = "SK"
Case 7
xProvince = "AB"
Case 8
xProvince = "BC"
Case 9
xProvince = "NU"
End Select
xlist(xcount) = xProvince
Case Else
xlist(xcount) = dRow(xgrid2(xcount))
End Select
Next
DG_Clients.Rows.Add(xlist)
Next

using values in one table to update another

[1] SqlDataAdapter SDA2 = new SqlDataAdapter("
UPDATE ACCOUNT_T
SET ACCOUNT_T.ACCT_BALANCE = ACCOUNT_T.ACCT_BALANCE +
TRANSACTION_T.TRANS_AMNT
WHERE ACCOUNT_T.ACCT_NUMBER = TRANSACTION_T.ACCT_NUMBER", CON);
I am trying to update the ACCT_BALANCE column of the ACCOUNT_T using a the value entered in the TRANS_AMNT of the TRANSACTION_T where the ACCT_NUMBER in the ACCOUNT_T is equal to the ACCT_NUMBER in the TRANSACTION_T.
when I try to do this I get this error
The multi-part identifier "TRANSACTION_T.ACCT_NUMBER" could not be
bound.
like depositing and withdrawing in a bank application.
TRANSACTION_TABLE. ACCOUNT_TABLE
ACCT_NUMBER | 001 |002 ACCT_NUMBER | 001 |002
TRANS_TYPE | deposit|deposit ACCT_NAME | LAWAL|RAHMA
TRANS_AMOUNT| 200 | 200 ACCT_BALANCE| 2000 |4000
I want to the account balance to increase when a deposit is made. Assume account_number 001 makes a deposit of 200, his account balance will increase to 2200.
try this,
UPDATE AT
SET AT.ACCT_BALANCE = AT.ACCT_BALANCE +
TT.TRANS_AMNT
from ACCOUNT_T AT
inner join TRANSACTION_T TT on AT.ACCT_NUMBER = TT.ACCT_NUMBER
where TT.TRANS_TYPE = 'deposit'

Resources