I have one master table named table1 where i store or update mobileNo data daily for a month.
;WITH table1 AS (SELECT * FROM (VALUES
(9999999999, '01/10/2013', NULL, NULL, NULL, NULL),
(9999999999, NULL, '02/10/2013', NULL, NULL, NULL),
(9999999999, NULL, NULL, '03/10/2013', NULL, NULL),
(9999999999, NULL, NULL, NULL, '04/10/2013', NULL),
(9999999999, NULL, NULL, NULL, NULL, '30/10/2013'),
(9999999999, NULL, NULL, NULL, NULL, NULL),
(8888888888, '01/10/2013', NULL, NULL, NULL, NULL),
(8888888888, NULL, '02/10/2013', NULL, NULL, NULL),
(8888888888, NULL, NULL, '03/10/2013', NULL, NULL),
(8888888888, NULL, NULL, NULL, '04/10/2013', NULL),
(8888888888, NULL, NULL, NULL, NULL, '30/10/2013'))
as t(mobileno,date1,date2,date3,date4,date30))
And i have another table named table2 where i keep unique mobileNo against table1. Now i want to update table2 against table1 if any data exists in table1.
mobileno date1 date2 date3 date4 date30
--------------- ---------- ---------- ---------- ---------- ----------
8888888888 01/10/2013 02/10/2013 03/10/2013 04/10/2013 30/10/2013
9999999999 01/10/2013 02/10/2013 03/10/2013 04/10/2013 30/10/2013
However i tried the query like this
UPDATE table1
set table1.date1 =
(SELECT date1 from table2 where table2.mobileno = table1.mobileno)
Where table2.mobileno = table1.mobileno
How do i update in a single query without repeating to update the 30 nos. of date columns, please help me. Thanks in advance.
Perhaps NULL values from TABLE2 can be avoided to UPDATE NOT NULL values in TABLE1 using following UPDATE statement.
Only ISNULL function is added to previous post
update table1
set
date1 = ISNULL(t2.date1, date1),
date2 = ISNULL(t2.date2, date2)
from table2 t2
where table1.mobileno = t2.mobileno
Related
i am having issue with passing the data from one table to another due to data type.
I tried converting datetimeoffset into date, and inserting into table where i have it as date type and im still getting this error.
this is the format of date/time i have:
2018-12-12 13:00:00 -05:00 in one table, and i have to just pars time and insert it into new table. I tried with casting using ,
CAST([from] AS date) DATE_FROM
I can run the query as select and it works but the moment i try to insert the data into other table even if the other table is formatted and prepared as date type i still get the issue.
Here is the table that stored data with datetimeoffset:
[dbo].[tmp_count](
[elements_Id] [numeric](20, 0) NULL,
[content_Id] [numeric](20, 0) NULL,
[element_Id] [numeric](20, 0) NULL,
[element-name] [nvarchar](255) NULL,
[sensor-type] [nvarchar](255) NULL,
[data-type] [nvarchar](255) NULL,
[from] [datetimeoffset](0) NULL,
[to] [datetimeoffset](0) NULL,
[measurements_Id] [numeric](20, 0) NULL,
[measurement_Id] [numeric](20, 0) NULL,
[from (1)] [datetimeoffset](0) NULL,
[to (1)] [datetimeoffset](0) NULL,
[values_Id] [numeric](20, 0) NULL,
[label] [nvarchar](255) NULL,
[text] [tinyint] NULL
And I am trying to cast columns with datetimeoffset to date and time and push it to #tmp1 table with
SELECT [elements_Id]
,[content_Id]
,[element_Id]
,[element-name]
,[sensor-type]
,[data-type]
,CAST([from] AS date) DATE_FROM
,[to]
,[measurements_Id]
,[measurement_Id]
,CAST([from (1)] AS time (0)) TIME_FROM
,CAST([to (1)] AS TIME(0)) TIME_TO
,[values_Id]
,[label]
,[text]
INTO #Tmp1
FROM [VHA].[dbo].[tmp_count]
SELECT
FROM #tmp1
which gives me the time in format for DATE_FROM as 2018-12-12 and for the DATE_FROM and DATE_TO as 13:00:00 which is exactly what i need.
Now i am trying to splice this table with another table and push it in final table that looks like this:
[dbo].[tbl_ALL_DATA_N](
[serial-number] [nvarchar](255) NULL,
[ip-address] [nvarchar](255) NULL,
[name] [nvarchar](255) NULL,
[group] [nvarchar](255) NULL,
[device-type] [nvarchar](255) NULL,
[elements_Id] [numeric](20, 0) NULL,
[content_Id] [numeric](20, 0) NULL,
[element_Id] [numeric](20, 0) NULL,
[element-name] [nvarchar](255) NULL,
[sensor-type] [nvarchar](255) NULL,
[data-type] [nvarchar](255) NULL,
[DATE_FROM] [date] NULL,
[to] [datetimeoffset](0) NULL,
[measurements_Id] [numeric](20, 0) NULL,
[measurement_Id] [numeric](20, 0) NULL,
[TIME_FROM] [time](0) NULL,
[TIME_TO] [time](0) NULL,
[values_Id] [numeric](20, 0) NULL,
[label] [nvarchar](255) NULL,
[text] [tinyint] NULL
using query below:
INSERT INTO [dbo].[tbl_ALL_DATA_N]
([serial-number],
[ip-address],
[name],
[group],
[device-type],
[measurement_id],
TIME_FROM,
TIME_TO,
[content_id],
[elements_id],
[element-name],
[sensor-type],
[data-type],
DATE_FROM,
[to],
[element_id],
[measurements_id],
[values_id],
[label],
[text])
SELECT *
FROM [VHA].[dbo].[tmp_sensor_info] A
FULL OUTER JOIN #tmp1 B
ON 1 = 1
And here is another message im getting: Msg 206, Level 16, State 2, Line 25
Operand type clash: numeric is incompatible with time
Any ideas?
The solution, which #PanagiotisKanavos alluded to in the comments, is to explicitly list the columns in your final SELECT * FROM.... The order of the columns in that SELECT statement aren't lining up with the columns you're INSERTing into in the destination table.
You may need to run an ad hoc instance of the query to sort out the column order. And then do yourself a favor for future maintenance and be sure to include a table alias on all of the listed columns so you (or whoever has to look at the code next) can easily find out if data is coming from [VHA].[dbo].[tmp_sensor_info] or #tmp1.
This is just one of many dangers in using SELECT * in production code. There's a ton of discussion on the issue in this question: Why is SELECT * considered harmful?
Also, as long as you're in there fixing up the query, consider meaningful table aliases. See: Bad habits to kick : using table aliases like (a, b, c) or (t1, t2, t3).
I have data in this format. I want to merge multiple rows into one-row grouping by ID column. each row will have only one non null value and one non null value for each column when grouped by ID.
[ID], [foo], [foo1], [foo2], [foo3], [foo4], [foo5]
1, data1, null, null, null, null, null
1, null, data2, null, null, null, null
1, null, null, data3, null, null, null
1, null, null, null, data4, null, null
1, null, null, null, null, data5, null
1, null, null, null, null, null, data6
2, data1, null, null, null, null, null
2, null, data2, null, null, null, null
2, null, null, data3, null, null, null
2, null, null, null, data4, null, null
2, null, null, null, null, data5, null
2, null, null, null, null, null, data6
Desired Output:
[ID], [foo], [foo1], [foo2], [foo3], [foo4], [foo5]
1, data1, data2, data3, data4, data5, data6
2, data1, data2, data3, data4, data5, data6
The aggregate functions max and min (as well as most others) will just ignore nulls. You could group by the ID and query the max of the other columns, which would return the single non-null value this column has:
SELECT id, MAX(foo), MAX(foo1), MAX(foo2), MAX(foo3), MAX(foo4), MAX(foo5)
FROM mytable
GROUP BY id
In your case, you can use max():
select id, max(foo) as foo, max(foo1) as foo1, . . .
from t
group by id;
I should note that your original data structure is often produced by a query that is a bit awry. Sometimes it is easier to fix the code that generates that result.
I have table TABLE1 in a Firebird 2.5 database and want to insert multiple rows.
script:
INSERT INTO TABLE1 (ID, IDPREDEK, ICO, DIC, FIRMA, MISTO, ULICE, PSC, CISSML, CISEVID, PLATOD, PLATDO, JMENO, PRIJMENI, TITUL, FUNKCE, TELEFON, TELEFON2, FAX, EMAIL, ODP_JMENO, ODP_PRIJMENI, ODP_TITUL, ODP_FUNKCE, ODP_TELEFON, ODP_TELEFON2, ODP_FAX, ODP_EMAIL, D_INIDOP, D_INISETR, D_KATPRAC, POCETMUZI, POCETZENY, HASCHILD, HASCHILD1, HASCHILD2, POZNAMKA)
VALUES (91, 89, NULL, NULL, 'CLY0010702 - PHM-LPH_DEPO / PRG/RSM/FSB/PHM', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'N', 'N', 'N', NULL);
And problem is this error :
Error while importing to table TABLE1:
Engine Error (code = 335544665)
violation of PRIMARY or UNIGUE KEY constraint
"PK_TABLE1" on table "FIRMY".
Problematic key value is ("ID=95).
SQL Error (code= -803):
Invalid insert or update value(s): object columns are constrained - no 2 table rows can have duplicate column values.
In TABLE1 is last ID number 94, I don't have two same rows with ID 95.
Any ideas what to do?
Check is your ID identity. If so just leave it out of insert into() and values().
i found solution, replace ID :
... VALUES (GEN_ID(GEN_TABLE1 , 1), 91, NULL, NULL, ...
Background
I currently have a table named Parts_list that is structured as shown below:
_id is autoincrementing so I always provide NULL which works fine.
I would like to use a SELECT statement to populate this table but have difficulties how to write the INSERT SELECT statement.
What I have tried
For testing I have tried this SQL statement:
INSERT INTO Parts_list VALUES
(null, 'My Name',CURRENT_TIMESTAMP, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null,
null, null, null,null, null, null)
and it inserts a new line without any problem.
Problem
However when I try something like this:
INSERT INTO Parts_list VALUES
(null, 'My Name',CURRENT_TIMESTAMP, Part_ID, null, null, null,
null, null, null,null, null, null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, null)
SELECT Part_ID FROM tracking_vehicles
where I am selecting one column and trying to insert this column, nothing happens. Do I have something wrong with my syntax? In my example "Part_ID" is VARCHAR, which matches the data type that it is being inserted to. Any help or guidance is appreciated.
PS I also tried this:
INSERT INTO Parts_list
(null, 'My Name',CURRENT_TIMESTAMP, Part_ID, null,
null, null, null, null, null,null, null, null, null,
null, null, null, null, null, null, null, null, null,
null, null, null,null, null, null)
SELECT Part_ID FROM tracking_vehicles
without the word "VALUES" as I saw many examples not use that, but it doesn't work. Even when I try my first example without "VALUES" it doesn't work either so I am guessing that I need that.
Using the example provided here I suggest you use the following syntax:
INSERT INTO Parts_list (_id, table_owner, etc)
SELECT null, 'My Name', etc
FROM tracking_vehicles
List out all your columns as I've started to and then pass through all the values in the SELECT.
I have a table called customers which gets info populated from a form not all fields are required (this is because the form generates asf / xml with the inputted info) and I would like to be able to merge duplicates into one row then delete the duplicates.
Here is my table
CID | LastName | FirstName | Street | City | ZipCode | HomePhone | CellPhone | EmailAddr
1 Test NULL NULL NULL NULL NULL NULL NULL
2 NULL TEST NULL NULL NULL NULL NULL NULL
3 NULL NULL Test NULL NULL NULL NULL NULL
4 NULL NULL NULL Test NULL NULL NULL NULL
5 NULL NULL NULL NULL Test NULL NULL NULL
6 NULL NULL NULL NULL NULL Test NULL NULL
7 NULL NULL NULL NULL NULL NULL TEST NULL
8 NULL NULL NULL NULL NULL NULL NULL TEST
I want to merge the data from each field that isn't null into the Fist Instance of then update that record and delete the remaining 7 records.
I am still starting out in SQL but understand joins, inserts, updates deletes etc. Any advice or direction would be greatly appreciated. I have found multiple posts where I can merge this data in a report but not to many where I can actually truly merge the data and delete the duplicate rows.
I just found this post while searching so it may be what I am looking for
mysql-consolidate-duplicate-data-records-via-update-delete
Try this one -
SET NOCOUNT ON;
DECLARE #temp TABLE
(
CID INT PRIMARY KEY
, LastName NVARCHAR(10)
, FirstName NVARCHAR(10)
, Street NVARCHAR(10)
, City NVARCHAR(10)
, ZipCode NVARCHAR(10)
, HomePhone NVARCHAR(10)
, CellPhone NVARCHAR(10)
, EmailAddr NVARCHAR(10)
)
INSERT INTO #temp (CID, LastName, FirstName, Street, City, ZipCode, HomePhone, CellPhone, EmailAddr)
VALUES
(1, 'Test', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(2, NULL, 'TEST', NULL, NULL, NULL, NULL, NULL, NULL),
(3, NULL, NULL, 'Test', NULL, NULL, NULL, NULL, NULL),
(4, NULL, NULL, NULL, 'Test', NULL, NULL, NULL, NULL),
(5, NULL, NULL, NULL, NULL, 'Test', NULL, NULL, NULL),
(6, NULL, NULL, NULL, NULL, NULL, 'Test', NULL, NULL),
(7, NULL, NULL, NULL, NULL, NULL, NULL, 'TEST', NULL),
(8, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'TEST'),
(12, 'Tes2', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(14, NULL, 'TES2', NULL, NULL, NULL, NULL, NULL, NULL),
(17, NULL, NULL, 'Tes2', NULL, NULL, NULL, NULL, NULL),
(18, 'Tes3', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(19, NULL, 'TES3', NULL, NULL, NULL, NULL, NULL, NULL),
(20, NULL, NULL, 'Tes3', NULL, NULL, NULL, NULL, NULL),
(21, NULL, NULL, NULL, 'Test3', NULL, NULL, NULL, NULL)
DECLARE #buffer_temp TABLE
(
CID INT PRIMARY KEY
, LastName NVARCHAR(50)
, FirstName NVARCHAR(50)
, Street NVARCHAR(50)
, City NVARCHAR(50)
, ZipCode NVARCHAR(50)
, HomePhone NVARCHAR(50)
, CellPhone NVARCHAR(50)
, EmailAddr NVARCHAR(50)
)
;WITH cte AS
(
SELECT t.CID, NextCID = ISNULL(t2.CID, (SELECT MAX(y.CID) FROM #temp y))
FROM #temp t
OUTER APPLY (
SELECT TOP 1 CID = t1.CID - 1
FROM #temp t1
WHERE t1.CID > t.CID
AND t1.LastName IS NOT NULL
) t2
WHERE t.LastName IS NOT NULL
)
INSERT INTO #buffer_temp
SELECT
t2.CID
, LastName = MAX(LastName)
, FirstName = MAX(FirstName)
, Street = MAX(Street)
, City = MAX(City)
, ZipCode = MAX(ZipCode)
, HomePhone = MAX(HomePhone)
, CellPhone = MAX(CellPhone)
, EmailAddr = MAX(EmailAddr)
FROM #temp t
CROSS APPLY (
SELECT *
FROM cte t2
WHERE t.CID BETWEEN t2.CID AND t2.NextCID
) t2
GROUP BY t2.CID
DELETE FROM #temp
INSERT INTO #temp
SELECT *
FROM #buffer_temp
SELECT *
FROM #temp
Output:
CID LastName FirstName Street City ZipCode HomePhone CellPhone EmailAddr
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 Test TEST Test Test Test Test TEST TEST
12 Tes2 TES2 Tes2 NULL NULL NULL NULL NULL
18 Tes3 TES3 Tes3 Test3 NULL NULL NULL NULL
It looks like you want to merge records 1-8, then 9-16, then 17-24, and so on.
Fortunately, you have a CID field that you can use for identifying the groups. All you need is the group, and the formula (CID - 1)/8 does the trick (SQL Server does integer division when dividing integers so, say, 4/8 = 0 and not 0.5). Here is the query:
select (CID - 1) / 8 as NewCID,
max(LastName) as LastName, max(FirstName) as FirstName, . . .
from t
group by (CID - 1) / 8;