Insert value refer to other column - sql

Here is original table A
Currency
DM_LS
ProductID
TimeID
TWD
1
26
559
TWD
1
26
560
TWD
1
27
561
TWD
2
27
562
TWD
2
28
563
TWD
2
28
564
I would like to generate serial number from table A above. So I add new column named SerialNum
Now I have no idea how to generate and insert the value. Value is Currency + DM_LS + ProductID + TimeID
Does it work through SQL?
Desired Result
Currency
DM_LS
ProductID
TimeID
SerialNum
TWD
1
26
559
TWD126559
TWD
1
26
560
TWD126560
TWD
1
27
561
TWD127561
TWD
2
27
562
TWD227562
TWD
2
28
563
TWD228563
TWD
2
28
564
TWD228564
Thanks so much.

You can do it two ways:
Add computed column
ALTER TABLE TABLEA ADD SerialNum AS CONCAT(Currency, DM_LS, Productid, TimeID)
Add separate column
ALTER TABLE TABLEA ADD SerialNum VARCHAR(200);
UPDATE TABLEA
SET SerialNum = CONCAT(Currency, DM_LS, Productid, TimeID)

It is simply an UPDATE statement with concatenation on the columns.
You need to esnure each column is CAST to a common datatype - VARCHAR in this case - and then just use the format ``col1 + col2 + ...etc```

Related

SUM column values based on two rows in the same tables in SQL

I have one table like below in my SQL server.
Trans_id br_code bill_no amount
1 22 111 10
2 22 111 20
3 22 111 30
4 22 111 40
5 22 111 10
6 23 112 20
7 23 112 20
8 23 112 20
9 23 112 30
and I want desired output like below table
s.no br_code bill_no amount
1 22 111 110
2 23 112 90
try this:
select br_code, bill_no, sum(amount)
from TABLE
group by br_code, bill_no

How to make a start writing this CTE in SQL Server?

I have a table which contains data of different elements in a database. This is a count of all elements in the database (which is restored daily, so no DDL/DML triggers possible)
Table looks like this:
LogDate SYSTEM_TABLE VIEW SQL_TABLE_VALUED_FUNCTION DEFAULT_CONSTRAINT SQL_STORED_PROCEDURE RULE FOREIGN_KEY_CONSTRAINT SERVICE_QUEUE SQL_INLINE_TABLE_VALUED_FUNCTION CHECK_CONSTRAINT USER_TABLE PRIMARY_KEY_CONSTRAINT INTERNAL_TABLE TYPE_TABLE SQL_TRIGGER SQL_SCALAR_FUNCTION UNIQUE_CONSTRAINT
20150204 45 253 60 1401 5259 2 784 3 4 95 2141 1604 26 4 16 195 33
20150203 45 253 60 1401 5259 2 784 3 4 95 2141 1604 16 4 16 195 33
20150202 45 253 60 1401 5259 2 784 3 4 95 2141 1604 21 4 16 195 33
20150201 45 253 60 1401 5259 2 784 3 4 95 2141 1604 25 4 16 195 33
20150131 45 253 60 1401 5259 2 784 3 4 95 2141 1604 21 4 16 195 33
What I would like to do is compare the most recent log date (20150204) with the previous logdate (20150203) and see if there are any changes between the elements. This will then fire off an email to the relevant developer for them to investigate (but this section isn't important at hte moment, just highlighting the changes between the logdates for now)
ETA:
It's part of a much larger query that uses temp tables etc:
IF OBJECT_ID('tempdb..#DBTotsTEMP') is not null drop table #DBTotsTEMP
--declare variables
DECLARE #DynamicPivotQuery as NVARCHAR(MAX)
DECLARE #ColumnName as NVARCHAR(MAX)
SELECT RIGHT(date, 4) + RIGHT(LEFT(date, 5), 2) + LEFT(date, 2) AS LogDate, [count] as CNT, type_desc
INTO [#DBTotsTEMP]
FROM BI_STG.SSRS.MH_DB_Totals
ORDER BY LogDate DESC
SELECT #ColumnName= ISNULL(#ColumnName + ',', '') + QUOTENAME([type_desc])
FROM (
SELECT DISTINCT [type_desc] FROM #DBTotsTEMP) as TypeDescs
SET #DynamicPivotQuery =
N'SELECT LogDate, ' + #ColumnName + '
INTO #MH_DB_Totals
FROM #DBTotsTEMP
PIVOT (SUM([CNT])
FOR [type_desc] in (' + #ColumnName + ')) as PVTTable
ORDER BY LogDate desc
select * from #MH_DB_Totals'
EXEC sp_executesql #DynamicPivotQuery
and I've got no idea where the CTE part should go, or how to highlight changes in the figures!
Sorted it, used a SELECT INTO with the IDENTITY function to add in row numbers then joined temp table back to itself on rowid = rowid-1.

How to query for non-matching records

I would like to create a query that finds those members with no reviews.
Table 1 - items selected
member/audno
733 12
733 13
733 14
844 12
844 13
844 14
955 12
955 13
955 14
Table 2 - reviews
member/audno/reviewno
733 12 111
844 13 112
955 14 113
Create new query with results of members with items with no reviews:
member/audno
733 13
733 14
844 12
844 14
955 12
955 13
How can I do this?
Try this
SELECT *
FROM
items_selected I
WHERE
NOT EXISTS (
SELECT *
FROM
reviews R
WHERE
R.member = I.member AND R.audno = I.audno
)
Another, equivalent query is based on a left join
SELECT I.*
FROM
items_selected I
LEFT JOIN reviews R
ON I.member = R.member AND I.audno = R.audno
WHERE
R.member IS NULL
You might test both of them and look which one is more performant.

Microsoft access Query rolling total every 5 lines not using dates

I am new to Microsoft access.
I need a query that will allow me to sum a rolling total for every 5 lines of data. So on the sixth day I need a line to drop off the total and the new line to be added.
Fields:
ID, Daily_SUM
The results should be
ID Daily sum Weekly Sum
1 12
2 41
3 46
4 125
5 120 344
6 42 374
7 41 374
8 57 385
9 207 467
10 215 562
11 187 707
12 -43 623
13 45 611
14 56 460
15 40 285
16 8 106
17 95 244
18 580 779
19 360 1083
20 337 1380
You can do this with a correlated subquery. The challenge is actually getting NULL values on the first few rows:
select t.id, t.daily,
(select iif(count(*) = 7, sum(t3.daily), NULL)
from (select top 7 t2.daily
from table t2
where t2.id <= t.id
order by t2.id desc
) t3
) as weekly
from table t;
EDIT:
If we assume that the ids are assigned sequentially with no gaps, then you can use an explicit join:
select t.id, t.daily,
iif(count(*) = 7, sum(t2.daily), NULL) as weekly
from table t inner join
table t2
on t2.id between t.id - 6 and t.id
group by t.id, t.daily;

Exclude the specific kind of record

I am using SQL Server 2008 R2. I do have records as below in a table :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 156 25 Average 891011
8 163 35 last 789521
9 145 25 Average 789521
10 156 20 first 963215
11 150 15 last 963215
12 160 17 Average 963215
13 156 20 first 456878
14 157 20 first 456878
15 150 15 last 456878
16 160 17 Average 456878
17 150 15 last 246977
18 160 17 Average 246977
19 150 15 additional 246977
Regarding this data, these records are kind of groups that have common UniqueId. The records can be of type "first, last, average and additional". Now, from these records I want to select "average" type of records only if they have "first" or "additional" kind of reading in group. Else I want to exclude them from selection..
The expected result is :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 163 35 last 789521
8 156 20 first 963215
9 150 15 last 963215
10 160 17 Average 963215
11 156 20 first 456878
12 157 20 first 456878
13 150 15 last 456878
14 160 17 Average 456878
15 150 15 last 246977
16 160 17 Average 246977
17 150 15 additional 246977
In short, I don't want to select the record that have type="Average" and have only "last" type of record with same UniqueId. Any solution?
Using EXISTS operator along correlated sub-query:
SELECT * FROM dbo.Table1 t1
WHERE [Type] != 'Average'
OR EXISTS (SELECT * FROM Table1 t2
WHERE t1.UniqueId = t2.UniqueId
AND t1.[Type] = 'Average'
AND t2.[Type] IN ('first','additional'))
SQLFiddle DEMO
Try something like this:
SELECT * FROM MyTable WHERE [Type] <> 'Average'
UNION ALL
SELECT * FROM MyTable T WHERE [Type] = 'Average'
AND EXISTS (SELECT * FROM MyTable
WHERE [Type] IN ('first', 'additional')
AND UniqueId = T.UniqueId)
The first SELECT statement gets all records except the ones with Type = 'Average'. The second SELECT statement gets only the Type = 'Average' records that have at least one record with the same UniqueId, that is of type 'first' or 'additional'.