I have table called customers with CustomerID,CompanyName,Address,Phone
Now we inserted a new column called Remarks which is empty or null
I have text file to bulk insert into the column using the view Remarkinsert with the following code
bulk insert HRRegion.dbo.Remarksinsert
From 'C:\Users\SMSTECHLNG50\Documents\remarks..txt'
with
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
But its getting the error
Msg 515, Level 16, State 2, Line 9 Cannot insert the value NULL into
column 'CustomerID', table 'HRRegion.dbo.Customers'; column does not
allow nulls. INSERT fails. The statement has been terminated.
I think that also here, you can only insert a whole row or nothing.
If your customer table looks like this:
customer
custid|co_name |addr |phone |remarks
42|Laverda |Breganze, Italy |+39 6 233 84 81 |(NULL)
43|Benelli |Pesaro, Italy |+39 8 284 55 32 |(NULL)
44|Ural |Irbit, Russia |+7 14 526 22 2342|(NULL)
45|Dnepr |Kiew, Ukraine |+380 526 22 2342 |(NULL)
46|Harley Davidson|Milwaukee, US |+1 802 223 4444 |(NULL)
47|Honda |Tokyo, Japan |+81 82 555 4123 |(NULL)
48|Moto Guzzi |Mandello del Lario, Italy|+39 6 423 04 72 |(NULL)
49|Ducati |Bologna, Italy |+39 7 722 04 43 |(NULL)
50|Norton |Birmingham, UK |+44 7234 723 4423|(NULL)
51|Matchless |Plumstead, London, UK |+44 8021 612 0843|(NULL)
52|Brough |Nottingham, UK |+44 5812 512 4883|(NULL)
(well, you add the remarks column using an ALTER TABLE ...), then, I would expect the file you mentioned with the remarks to look somewhat like this:
remarks
custid|remarks
42|built also tractors, closed now
43|first series 6-cylinder motorbike
44|old style sidecar rigs with modern engine
45|old style sidecar rigs, permanent two-wheel drive
46|the american classic
47|builders of the CB 750 four and the gold wing
48|famous for horizontal singles and 90° V twins
49|90° V twin bikes with lateral crankshaft
50|english classic, still alive
51|english classic, closed now
52|probably the finest motorcycles ever built
So, you would build a remarks_stg table:
CREATE TABLE remarks_stg (
custid SMALLINT NOT NULL
, remarks VARCHAR(50) NOT NULL
);
Then, you load just that staging table with the data file as described above - and, at least if have SQL Server 2008 and above, you use a MERGE statement to update the customer table:
MERGE customer t
USING stg_customer s
ON t.custid = s. custid
WHEN MATCHED THEN UPDATE SET
remarks = s.remarks
;
Related
I have a dataframe of User IDs and Tags as shown below under 'Current Data' .
The Goal:
I want to be able to duplicate records per each value under the tags column. As you can see in the target output, user ID 21 is repeated 3x for each of the three tags that are in the source 'TAGS' - everything is duplicated except the Tag column - 1 Record per item in the comma separated list.
Issue:
I looked at using the SPLIT_TO_TABLE functionality in snowflake but it doesn't work in my use case as not all the tags are consistently in some kind of order and in some cases, the cell is also blank.
Current Data:
USER_ID CITY STATUS PPL TAGS
21 LA checked 6 bad ui/ux,dashboards/reporting,pricing
32 SD checked 9 buggy,laggy
21 ATL checked 9
234 MIA checked 5 glitchy, bad ui/ux, horrible
The target:
USER_ID CITY STATUS PPL TAGS
21 LA checked 6 bad ui/ux
21 LA checked 6 dashboards/reporting
21 LA checked 6 Pricing
32 SD checked 9 buggy
32 SD checked 9 laggy
21 ATL checked 9
234 MIA checked 5 glitchy
234 MIA checked 5 bad ui/ux
234 MIA checked 5 horrible
Sql:
select table1.value
from table(split_to_table('a.b', '.')) as table1
SPLIT_TO_TABLE works. Below is the query using your sample data:
select USER_ID, CITY, STATUS, PPL, VALUE
from (values
(21,'LA','checked',6,'bad ui/ux,dashboards/reporting,pricing')
,(32,'SD','checked',9,'buggy,laggy')
,(21,'ATL','checked',9,'')
,(234,'MIA','checked',5,'glitchy, bad ui/ux, horrible')
) as tbl (USER_ID,CITY,STATUS,PPL,TAGS)
, lateral split_to_table(tbl.tags,',');
Result:
USER_ID CITY STATUS PPL VALUE
21 LA checked 6 bad ui/ux
21 LA checked 6 dashboards/reporting
21 LA checked 6 pricing
32 SD checked 9 buggy
32 SD checked 9 laggy
21 ATL checked 9
234 MIA checked 5 glitchy
234 MIA checked 5 bad ui/ux
234 MIA checked 5 horrible
Hello everyone I am trying to convert a categorical variable which is a column named Educational Group and has values like
State | Educational Group | No of Persons |
-------+-----------------------+---------------+
A Below Metric 123
A metric/secondary 456
A diploma 789
A graduate and above 101112
A post graduate 131415
B Below Metric 145
B metric/secondary 467
B diploma 564
B graduate and above 987
B post graduate 875
I want this to be converted as
State | Below Metric_ NO of persons | Metric/Secondary_No of persons | Diploma_No of Persons| ...
-------+-------------------------------+--------------------------------+---------------------+
A 123 456 789
B 145 467 564
and so on for all states and all educational levels.
Is it possible to do in SQL? Actually I did the same in Python using pivot function and it worked pretty well and now I the same to be done in Microsoft SQL Server Management Studio.
I want to convert this
https://ibb.co/L15m2sS
into this https://ibb.co/9tLpk7V
As mentioned PIVOT should do the trick.
SELECT *
FROM
(
SELECT *
FROM mytable
) AS SourceTable PIVOT(AVG([No_of_Persons]) FOR [Educational_Group] IN([Below Metric],
[metric/secondary],
[graduate and above],
[post graduate])) AS PivotTable;
Online demonstration using your table on db<>iddle.
I have two tables in my MS Access database:
1) tblLines
LineID
Line
LineName
2) tblTripTimes
TripTimesID
Line
Time
The tblTripTimes times table was imported into MS Access from a transit software program, and tblLines I created in MS Access. The Line column in each table share identical values; however, I'd prefer the tblTripTimes.Line values be replaced with the primary key values in tblLines.LineID.
For example:
Before
tblLines tblTripTimes
--------------- ------------------------
LineID | Line TripId | Line | Time
1 1 234 3 13:00
2 2 235 1 09:00
3 2A 236 2 17:17
4 2B 237 2B 07:30
5 3 238 2A 21:36
After
tblLines tblTripTimes
--------------- ------------------------
LineID | Line TripId | Line | Time
1 1 234 5 13:00
2 2 235 1 09:00
3 2A 236 2 17:17
4 2B 237 4 07:30
5 3 238 3 21:36
I've tried creating an update query that would match tblTripTimes.Line with tblLines.Line, then replace the values in tblTripTimes.Line with the values in the tblLines.LineID column. Being a rookie I'm at a complete loss.
Can someone please help?!
You can use a simple update query:
UPDATE tblLines INNER JOIN tblTripTimes ON tblLines.Line = tblTripTimes.Line
SET tblTripTimes.Line = tblLines.LineID
I have been cleaning up a database table which was bit of a nightmare.
I want to ensure that the data being entered going forward is correct. So I read about foreign key constraints.
So below is a table where the data gets entered into.
tblSales
Country Seller Value
52 01 100
86 01 100
102 32 100
32 52 100
52 01 100
I want to ensure the values being entered into the Country & Seller fields are a certain set of values. There is also a table called tblMap which is used for reports to give the numbers a name which is easy to read.
tblMap
Factor Code Name
Country 52 US
Country 86 Germany
Country 102 Spain
Country 32 Italy
Seller 01 Bob
Seller 32 Sarah
Seller 52 Jim
So like I say I was going to use a foreign key constraint but I can't create a primary key on the Code field in tblMap as 52 is used for both a country and a seller. I am not able to change the code numbers either.
Am I still able to use a foreign key constrain to ensure any value entered into tblSales exists in tblMap?
May be you can replace tblMap by 2 tables tblMapCountry and tblMapSeller
tblMapCountry
Code Name
52 US
86 Germany
102 Spain
32 Italy
tblMapSeller
Code Name
01 Bob
32 Sarah
52 Jim
After you can base
a FK between tblSales.country and tblMapCountry
a FK between tblSales.seller and tblMapSeller
At the end you can build a view tblMap by union the 2 tables tblMapCountry and tblMapSeller
create view `tblMap`
as
select 'Country' as Factor,Code,Name from tblMapCountry
union all
select 'Seller' as Factor,Code,Name from tblMapSeller
The company I work for uses an AS400 (iSeries). There is some data in a system dictionary that I am trying to pluck out and turn into an associative table.
Here is what the data looks like
xtype | xdata
60 | 011111211 212
60 | 345
60 | 212312 169
xtype is the "key" that will allow me to return the relevant data.
212,345,169 are employee numbers and are in the left 3 characters of the 24 character xdata column.
011111211 is 3 "territories" (011, 111 and 211), likewise 212312 is 2 "territories" (212, 312)
What I would like to end up with is
empNum | territory
------------------
212 | 011
212 | 111
212 | 211
169 | 212
169 | 312
Here is what I have worked on so far:
SELECT
*
From
(
select
right(xdata,3) as empNum,
trim(coalesce(left(xdata,3),'')) as ter
from Table
where xtype=60 and xarg < 960
) as outerTable
where ter <> ''
and
trim(coalesce(substr(xdata,4,3),'')) as ter
where ter <> ''
would work for the second territory
and
trim(coalesce(substr(xdata,7,3),'')) as ter
where ter <> ''
would work for the third territory
What I don't know is how to take those 3 and join them into a result that looks like an associative table. Any thoughts?
So you've got one query that returns 212 | 011 / 169 | 212, another that returns 212 | 111 / 169 | 312, and a third that returns 212 | 211, is that correct?
The obvious answer to transform this to the results you're asking for is to use UNION ALL to combine the three queries. You were looking at ways to join the queries, but (simply put) joining would add columns, when what you want to add is rows.