I am using Microsoft SQL Server 2014 and created a SQL query in which I first split the value and that records display in second table, but the problem is when there is a null value in the column, that record is not displayed in second table.
But I want that record with null or 0 value in second table.
I uploaded this screenshot of what I require for you to understand my question:
Can anyone solve my problem and guide me?
Here is my Exactly Output
Exactly Output Image
change to OUTER APPLY and since the split is null, you will also need to use LEFT JOIN to MetaDetails
SELECT TDCWax.TDCNo,
TDCWax.MouldCode,
ISNULL(MetaDetails.MetalName, 'For All Metal') as MetalName,
TDCWax.Status
FROM TDCWax
OUTER APPLY dbo.split(TDCWax.TDCMCode, '|') split
LEFT JOIN MetaDetails ON MetaDetails.MetalCode = split.items
WHERE TDCWax.MouldCode = 'I0501'
Related
I'm new to SQL Server. The scenario is the following:
I have a csv with a bunch of Serial N0, which are unique.
Example:
Serial No
-----------
01561
21654
156416
89489
I also have a SQL Server database table, where are several rows which can be identified with the serial no. For example I have 6 rows in the SQL Server table with the serial no. 01561. Now I want to update a field in all these rows with "Yes". If it is only about this number, I know the solution it's
UPDATE dbo.Table1
SET DeleteFlag = 'Yes'
WHERE Serial No. = 01561;
Unfortunately I have more than 10,000 Serial No in the csv for what I have to do that. Can you help me to find a solution for that?
First you should use the TASK feature to import the CSV. You right click to do this on the database and select "TASK" and import data. Its a UI which is pretty self explanatory, so itll help you get the job done quickly and easily. Make note of the name you give the table, SQL Server will try and give it a defualt name with a "$" in the name. Change that to something like "MyTableImport". If the data is already in SQL Server, go to the next step.
Step 2 - You can do the UPDATE for the entire table via a join. All youre doing is matching the ID's to another table, right? Looping would be a bad idea here especially since itll take a minute to loop through 10000+ and run an update FOR EACH ONE. Thats against an idea known as "Set based approach" which to sum it up nicely is to do things all at once (google it though because im horribly over simplifying the idea for you). Here is a sample update join query for you:
UPDATE x
SET x.DeleteFlag='Yes'
FROM yourimportable y
INNER JOIN yourLocal x ON y.SerialNo=x.SerialNo
Assuming that you have created a temp table to load CSV with a bunch of serial number. Now you can update your permanent table with the temp table data using update join like this:
UPDATE t1
SET t1.DeleteFlag = 'Yes'
FROM dbo.Table1 AS t1
INNER JOIN #TempTable2 AS t2
ON t1.Serial_No = t2.Serial_No
I need to update an existing table (First) with a new column (Ticket_No_Calc). Basically, there are two columns (Doc_Header and Doc_No) that I have to lookup/refer to in the first table against the second table (Doc_No) column.
The desired column Ticket_No_Calc result is retrieved from the column Ticket_No.
Below is the screenshot of sample data of the first table
and this is the screenshot of sample data of the second table
Currently, in Excel, I use the following formula to get the desired column in the existing table
=IFERROR(VLOOKUP(FIRST!B2,'SECOND'!A:B,1,0),(VLOOKUP(FIRST!C2,'SECOND'!A:B,1,0)))
I am not sure how to do this in SQL. I know that I can get this done in SQL with the following formula if I only have to refer to one column Doc_Header:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
But I need to refer to two columns in the existing table. I have been researching for sometimes to get the correct command but I can't seem to be able to crack it.
Following statement may help
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
INNER JOIN Second AS B ON (A.Doc_Header = B.Doc_NO) OR (A.Doc_NO = B.Doc_NO) ;
As per the sample data provided this is query seems incorrect. But as per the comments mentioned in your question it is better to perform this in 2 steps.
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.DOC_NO=B.DOC_NO
WHERE A.Doc_Header <> B.Doc_NO
Try this:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A ,Second AS B where A.Doc_Header = B.Doc_NO or A.Doc_NO = B.Doc_NO
I'm quite new to SQL and hope you can help me with my problem.
I have a table called Order_Status_Form_3 with the columns Order_ID (KEY), Customer_ID, Customer_Unique_ID, Status(KEY) and Date.
The table is filled, except for the Customer_Unique_ID Column.
To fill this Column I need to reference the Customer table where the Customer_ID is linked to the Customer_Unique_ID, so the right IDs cover the right places. When the Customer_ID in Order_Status_Form_3 equals the Customer_ID in the Customer table the given Customer_Unique_ID shall be inserted into the Customer_Unique_ID column in Order_Status_Form_3.
I tried to combine an INSERT INTO with a SELECT and INNER JOIN, but received an error message that says:
"cannot insert NULL or update to NULL: Order_ID".
I guess it's not clear for the program where to insert the values found and it tries to insert into all columns. I searched for similar problems but could not find any satisfying answers for my specific problem.
Here's the code I used:
Insert Into "HXE_109"."Order_Status_Form_3" ("Customer_Unique_ID")
Select customer."customer_unique_id"
From "HXE_109"."Customer" As customer
Inner Join "HXE_109"."Order_Status_Form_3" As OrderStatus3
On OrderStatus3."Customer_ID" = customer."customer_id"
I tried to specify the place to insert the values found by attaching a WHERE at the end, but received the same error.
Where OrderStatus3."Customer_ID" = customer."customer_id"
Does anyone know how to solve this issue and can tell me where my mistake is?
Thanks in advance for reading this long question and leaving an answer.
Edit
I tried using update but it seems like I cannot get it right.
Update "HXE_109"."Order_Status_Form_3"
Set "Customer_Unique_ID" = (Select customer."customer_unique_id"
From "HXE_109"."Customer" As customer
Inner Join "HXE_109"."Order_Status_Form_3" As OrderStatus3
On OrderStatus3."Customer_ID" = customer."customer_id")
Now I get the following error:
single row query returns more than one row
Do I need to use a Where condition here?
Sorry for my stupidity. :(
As I could follow the comments, what you want to do is running an UPDATE statement
Please check following DML command
Update "HXE_109"."Order_Status_Form_3"
Set
"Customer_Unique_ID" = customer."customer_unique_id"
From "HXE_109"."Order_Status_Form_3" As OrderStatus3
Inner Join "HXE_109"."Customer" As customer
On OrderStatus3."Customer_ID" = customer."customer_id"
If I try to explain the error messages:
"cannot insert NULL or update to NULL: Order_ID".
That is related with a field defined as NOT NULL. So in your target table ORDER_ID is defined as "not null", so in INSERT you have to provide a value for it or define it as an identity field in your HANA table definition
The second error: single row query returns more than one row
This is related with the case where SQL Engine expects a value not a set of values.
So the SELECT statement that you assign the results to Customer_Unique_ID field returns more than 1 value. In this case SQL engine raises an exception
My goal is to suppress DEC from JAN using a unique id and export the results into a txt file. The query runs fine except for the fact that it returns a ton of null values (41 fields worth). I'm assuming these are from the DEC table due to the left join. This is my first attempt at a suppression like this. I am sure I am missing a simple statement to cure my ills but I am at a loss for what it might be. Or is there a more efficient technique than left join?
SELECT *
FROM [dbo].[Jan]
LEFT JOIN [dbo].[DEC]
ON [DEC].[ID] = [Jan].[ID]
WHERE [DEC].[SSN] IS NULL
Because you are using *, it is fetching everything for you.
Change SELECT * to SELECT JAN.columnName, DEC.columnName, etc. If you declare what columns you want from both tables, it should give you the right results.
I have a query where I'm selecting more than 15 things, and thus getting (more than) 15 columns in my results. Some of the things I've selected are big CASE statements. T-SQL of course displays your result in the order than you list things in the SELECT statement.
Is there a way to have the result columns displayed in a different order to help with my review of them, without
a) re-ordering how they were selected (because I'll need to do this every time I want to compare two other columns side-by-side)
i.e. Don't want to change: SELECT a,b,c,d,e,f to SELECT f,d,a,b,c,e
since a-f can each be 5-10 lines of code
b) drag-n-drop column in the results next to each other, because if I want column 2 to be next to column 9 and column 14, and all three to be at the end of the result table, then that'd a lot of dragging to do.
c) knowing the column number of what was selected as opposed to the column name
What I'm looking for is something that resembles:
Select
vi.Name
,vi.Height
,vi.Power
,case when tt.losses < 3
then 'Y'
else 'N'
end as MasteryKen
,tt.blahnum
,vi.blahtext
,vi.blahdate
,vi.blahcalc
,tt.blahflag
,vi.blahflag
,vi.blahcompare
From SenshiVitalInfo vi
Join TatakauTable tt
on vi.namecd=tt.namecd
OrderOutputResults by tt.blahflag, vi.blahflag, *
Does a function to do the last line exist in T-SQL (SQL Server 2008)?
There is no functionality in TSQL to "move" the columns around, other than editing the SELECT list order, this is the best you can do:
SELECT
d.Name
,d.Height
,d.Power
,d.MasteryKen --<<can now move around single lines
,d.blahnum
,d.blahtext
,d.blahdate
,d.blahcalc
,d.blahflag
,d.blahflag
,d.blahcompare
FROM (Select
vi.Name
,vi.Height
,vi.Power
,case when tt.losses < 3
then 'Y'
else 'N'
end as MasteryKen
,tt.blahnum
,vi.blahtext
,vi.blahdate
,vi.blahcalc
,tt.blahflag
,vi.blahflag
,vi.blahcompare
From SenshiVitalInfo vi
Join TatakauTable tt
on vi.namecd=tt.namecd
) d
--ORDER BY ....
You can wrap your existing query inside a derived table, where you can then move the single line columns names all you want. Just make sure that any ORDER BY is moved out of the derived table.
If You are using SSMS, you can view your result set in "results to grid" mode and just drag and drop the column headings to slide the columns around.
No this does not exist.
If you don't like moving the case statements you could put the query into a view that you select from and then you select list will be simplified, but that seems like a waste of effort to me.
I don't think this functionality is part of any SQL variant.
If you are using a SQL IDE to view the result set you might be able to drag the columns you want together, I used to do this in SSMS.
Otherwise Abe's answer is all you can do.