Cannot add row to SQL Server View - sql

I have a View which is comprised of two tables, TABLE_ONE and TABLE_TWO.
These two tables are linked by their primary key: PKT_UID (see pic).
I have added two rows to the two tables and given them the same PKT_UID but the View is not displaying any rows with this PKT_UID when I search the view for that UID.
I don't know much about SQL views, but my understanding was that as long as the two rows from both tables are linked by the same primary key, it should display in the view. What am I missing?
VIEW CODE:
SELECT dbo.TABLE_ONE.PKT_UID, dbo.TABLE_ONE.IT_UID, dbo.TABLE_ONE.SKT_UID, dbo.TABLE_ONE.RMS_XSP,
dbo.TABLE_ONE.S_MT, dbo.TABLE_ONE.E_MT, dbo.TABLE_ONE.KILP, dbo.TABLE_ONE.KILT,
dbo.TABLE_ONE.IIC, dbo.TABLE_ONE.L_D, dbo.TABLE_ONE.S_D, dbo.TABLE_ONE.S_DAM,
dbo.TABLE_ONE.E_DF, dbo.TABLE_ONE.E_DFD, dbo.TABLE_ONE.H_U, dbo.TABLE_ONE.JK,
dbo.TABLE_ONE.JKT, dbo.TABLE_ONE.TRCE, dbo.TABLE_ONE.MOD, dbo.TABLE_ONE.TAK, dbo.TABLE_ONE.DG_AR,
dbo.TABLE_ONE.DG_LENGTH, dbo.TABLE_ONE.XSP_OFF, dbo.TABLE_ONE.JIK_UID, dbo.TABLE_ONE.FA_UID,
dbo.TABLE_ONE.JOB, dbo.TABLE_TWO.PKT_UID AS GULL_UID, dbo.TABLE_TWO.GULL_T_UID, dbo.TABLE_TWO.GULL_FRC_UID,
dbo.TABLE_TWO.GULLOPP_UID, dbo.TABLE_ONE.RMMS_XSP, dbo.TABLE_ONE.GANG, dbo.TABLE_ONE.TRM_BSD_DATE,
dbo.TABLE_ONE.DOLM, dbo.TABLE_ONE.DONM, dbo.TABLE_TWO.GULLM_UID, dbo.TABLE_TWO.AR_UID,
dbo.TABLE_TWO.PAR_UID, dbo.TABLE_TWO.RCT_UID, dbo.TABLE_TWO.PG_C, dbo.TABLE_TWO.RCT_YN_UID,
dbo.TABLE_TWO.SCHD_TM_UID, dbo.TABLE_ONE.NRS, dbo.TABLE_TWO.BS_M, dbo.TABLE_TWO.EMB_UID,
dbo.TABLE_TWO.MNG_REQ_UID, dbo.TABLE_TWO.FORM_R, dbo.TABLE_TWO.FORM_R_NAME, dbo.TABLE_TWO.SCHED_UID,
dbo.TABLE_ONE.SH_G, dbo.TABLE_ONE.SH_GT, dbo.TABLE_TWO.PKT_UID AS IT_N, CAST(dbo.TABLE_TWO.PKT_UID AS char(15))
AS GULL_NAME, dbo.TABLE_TWO.PACK
FROM dbo.TABLE_TWO INNER JOIN
dbo.TABLE_ONE ON dbo.TABLE_TWO.PKT_UID = dbo.TABLE_ONE.PKT_UID
WHERE (dbo.TABLE_ONE.IT_UID = 1160) AND (dbo.TABLE_ONE.E_DFD = 0)```

SELECT
rm_c.ITEM_UID
,rm_g.ITEM_UID
FROM
RM_COMMON_ATTRIBUTES rm_c
LEFT JOIN RM_GULLY_2010_A rm_g
ON rm_g.ITEM_UID = rm_c.ITEM_UID
If this shows both UIDs as matching then it's not the table
If you're using report builder (have you tried refreshing all fields)

Solved thanks to #BarneyL. There was a WHERE RM_COMMON_ATTRIBUTES.ITEM_TYPE_UID = 1160 condition in the view code that was not being fulfilled.

Related

Microsoft SQL Server generates two select queries and puts data in separate columns

I am looking for a query that separate the data with the condition WHERE in the same output but in separates columns.
Example: I have the table Product_2:
I have two separates queries (to separate the products by Produt_Tag):
SELECT
Product_Mark AS "PIT-10_Product_Mark",
Product_Model AS "PIT-10_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-10';
SELECT
Product_Mark AS "PIT-11_Product_Mark",
Product_Model AS "PIT-11_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-11';
And I get this output:
But I need the output to be like this:
Can someone tell me how I need to modify my query to have the four columns in the same table/ output?
Thank you
I forgot to tell that in the data I Have the “Porduct_Mark” that only appears one time. (in reality the data in “Product_Mark” is the name of the place where the instrument is located and one place can have one or two instruments “Product_Model”. At the end I’m looking for the result show in the image here below. I tried to use LEFT JOIN but that don’t work.
here is the new table "Product_2"
Result that I'm looking for:
Luis Ardila
I am assuming Product_PK is the primary key for the table and the repeated value 1002 shown in the question is a mistake. Considering this assumption, you can get the result set using self join as below.
SELECT pa.Product_Mark AS "PIT-10_Product_Mark", pa.Product_Model AS "PIT-10_Product_Model",
pb.Product_Mark AS "PIT-11_Product_Mark", pb.Product_Model AS "PIT-11_Product_Model"
FROM Product_2 pa
INNER JOIN Product_2 pb
ON pa.Product_Mark = pb.Product_Mark
WHERE pa.product_pk != pb.product_pk
and pa.Product_Tag = 'PIT-10'
and pb.Product_Tag = 'PIT-11';
verified same in https://dbfiddle.uk/NiOO8zc1

T-SQL / SQL Server : multi table join query

Find the below attachment:
Currently I am trying to learn how to perform a multi-table join where one of the tables do not have a direct relationship (Standard PK-FK relationship) with the original table. Further, the desired output should be filtered out based on a few WHERE conditions.
I was assuming that if I joined DealsFurtherInfo table I should be able to bridge the relationship between the Deals and Location Points tables (since they do not share the DealsUID factor).
Here are the tables (originally from the image link)
Table: Deals
DealUID
CounterPartyUID
DealTypeUID
TradeDate
InactiveFlag
Table: CParties
CounterPartyUID
CounterPartyName
Table: DealsFurtherInfo
DealUID
LocationPointUID
Table: LocationPoints
LocationPointUID
LocationName
Desired output
DealUID CounterPartyName LocationName
Below is my code - If anyone could explain what am I doing incorrectly and walk me through SQL logic step by step, it would be highly appreciated.
SELECT
tblD.DealUID,
tblCP.CounterpartyName,
tblPP.PointCode AS Location
FROM
Deals tblD
JOIN
CounterParties tblCP ON tblD.CpartyUID = tblCP.CpartyUID
JOIN
DealsFurtherInfo tblDPL ON tblD.DealUID = tblDPL.DealUID
JOIN
LocationPoints tblPP ON tblDPL.PointUID = tblPP.PointUID
WHERE
tblD.DealTypeUID = 20110
AND tblD.InactiveFlag = 'N'
AND tblD.TradeDate > '2019-01-01'

How to fix Column does not exist on this Inner Join of two tables with three conditions?

Currently, I am trying to create a new table based on the Inner Join query of two tables with three conditions. However, the SQL Error window always tells me the columns does not exist even when they clearly do.
So this is what has to happen an Inner Join has to happen when two specific values are equal to each other in the table of the columns and where are third value shares a similarity.
This is because while the Plotletter in the first table is actually only one letter like for say A.
The Application could be written like ABCD.
I have already also tried to make the clear the fields are referred to the right table by following the suggestion, but the error still happens.
CREATE TABLE testschema.FinalPlantenpaspoort
AS
SELECT PrimaryIndex, jaarpr, proefcode, plotleter, plotcijfer, plot, X, Y
FROM testschema.plantenpaspoortsjabloon
JOIN testschema.weegschaalproeven
ON plantenpaspoortsjabloon.proefcode = weegschaalproeven.Intern_Proef_Nr
AND plantenpaspoortsjabloon.plotcijfer = weegschaalproeven.Objectnr
WHERE plantenpaspoortsjabloon.plotletter LIKE weegschaalproeven.Application
;
This the error and suggestion they give me but no luck.
ERROR: column weegschaalproeven.intern_proef_nr does not exist
LINE 5: ON plantenpaspoortsjabloon.proefcode = weegschaalproeven.Int...
^
HINT: Perhaps you meant to reference the column "weegschaalproeven.Intern_Proef_Nr".
SQL state: 42703
Character: 237
**
Edit 2/07/2019: PROBLEM HAS BEEN SOLVED BUT 0 RECORDS Selected.
**
Okay the problem seems to be solved but there is a new kind of problem while the code does works 0 records are selected because of the JOIN. And this should not be the case. I know there are records that matches because this a test where I made sure the tables of the shapefiles in QGIS contains data that is relevant.
Create TABLE testschema.finalplantenpaspoort AS
SELECT jaarpr, proefcodet, plotletter, plotcijfer, plot, X, Y
FROM testschema.plantenpaspoortsjabloon
JOIN testschema.weegschaalproeven
ON plantenpaspoortsjabloon.proefcodet = weegschaalproeven.intern_proef_nr AND plantenpaspoortsjabloon.plotcijfer = weegschaalproeven.objectnr
WHERE plantenpaspoortsjabloon.plotletter LIKE weegschaalproeven.application
;
**SELECT 0**
Query returned successfully in 72 msec.
I have found the sollution to my zero select problem, apparently I needed to set a special Like circumstance because otherwise the records could not be matched here it goes:
Create TABLE testschema.finalplantenpaspoort AS
SELECT proefcodet, proefnaam, datumvernietiging, oogstvernietigingsmethode, objectnr, productcode, potnummer, dosis, oppervlakte, eenheid, luikb, oogstbestemming, application, opmerking, proefjaar, proefcode, plotletter, plotcijfer, plot, X, Y
FROM testschema.plantenpaspoort
JOIN testschema.weegschaalproeven
ON plantenpaspoort.proefcode = weegschaalproeven.internproefnr AND plantenpaspoort.plotcijfer = weegschaalproeven.objectnr AND plantenpaspoort.plotletter LIKE ANY (regexp_split_to_array(weegschaalproeven.application , '\s*'))
;
GRANT ALL ON TABLE testschema.finalplantenpaspoort TO test_admin_test WITH GRANT OPTION;

How do I run a SQL query against 2 views that are linked by an ID but has different column names?

I currently have 2 views that house some data.
View1: includes patientsPID and their billing/invoice information (high-level)
View2: includes patientsCID and their respective line items for their billing invoice information (granularity of View1)
I am trying to run a query where I will be able to validate some data. E.g. -> I want to see patientsPID = 1 and see the total amount paid and join it to View2 to see in more details on the invoice.
expected results
Thank you.
You need to join View1 and View2:
select v1.patientspid, v1.invoiceheader, v2.invoiceline, v2.amount
from view1 v1 inner join view2 v2
on v1.patientspid = v2.patientscid
order by v1.patientspid
If you want to get the results for a patientspid use
where v1.patientspid = 1
and no order by
In this case both PatientsPID and PatientsCID act as your Key. The column names don't have to be the same as long as the data contained in them are linked (Same Data).
Select View1.PatientsPID, View1.AmountPaid, View2.[SomeColumnName]
From View1 inner join View2 on View1.PatientsPID = View2.PatientsCID
Where View1.PatientsPID = 1
Use Join in the From line and you have to set which columns are equal between the two tables. These columns have to contain the same data in order to be able to link. Whatever column names you want to select just place the table name in front of it as show above, this prevents errors in the event of an ambiguous column name

SQL Query not searching data if record contains zero

I have two tables and with column paperNo and some data regarding that paper. I am trying to search all data based on paper no. from both the tables. I have successfully written the query and it is retrieving the data successfully. but I have noticed that. If my paperNo contains zero(0) then the query is not searching for that data. And for the non zero contains paperNo it is retrieving the same record twice.
I don't understand what is going wrong. tried every thing.
Here is my Query .-
SELECT PaperDate.paperNo,
PaperDate.RAW_PAPER,
PaperDate.EDGE_SEALED,
PaperDate.HYDRO_120,
PaperDate.HYDRO_350,
PaperDate.CATALYST_1ST,
PaperDate.CATALYST_2ND,
PaperDate.SIC_350,
tblThicknessPaperDate.rawThickness,
tblThicknessPaperDate.catThickness,
tblThicknessPaperDate.sicThickness,
tblThicknessPaperDate.rejectedThickness
FROM tblThicknessPaperDate
FULL OUTER JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (tblThicknessPaperDate.paperNo = #paperNo)
I would try:
FROM tblThicknessPaperDate
RIGHT JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (PaperDate.paperNo = #paperNo)
The two changes are: swapping to a right join so even if a record isn't in tblThicknessPaperDate we will still see the record in PaperDate. The other change is to use PapterDate.paperNo in the where clause. Since tblThicknessPaperDate.paperNo could be null we don't want to use that in the where if we can avoid it.
SELECT PaperDate.paperNo,
PaperDate.RAW_PAPER,
PaperDate.EDGE_SEALED,
PaperDate.HYDRO_120,
PaperDate.HYDRO_350,
PaperDate.CATALYST_1ST,
PaperDate.CATALYST_2ND,
PaperDate.SIC_350,
tblThicknessPaperDate.rawThickness,
tblThicknessPaperDate.catThickness,
tblThicknessPaperDate.sicThickness,
tblThicknessPaperDate.rejectedThickness
FROM tblThicknessPaperDate
FULL OUTER JOIN PaperDate ON PaperDate.paperNo =tblThicknessPaperDate.paperNo
WHERE (tblThicknessPaperDate.paperNo = #papNo | PaperDate.paperNo = #paperNo)