I have view:
SELECT MIN(v.DOBA_VYSETRENI), v.ID_PACIENTA, v.ID_DOKTORA, d.MIN_DOBA
FROM vysetreni v,
doktor d
WHERE v.ID_DOKTORA = d.ID_DOKTORA
AND v.ID_VYSETRENI = 1
GROUP BY v.ID_DOKTORA, v.ID_PACIENTA, d.MIN_DOBA;
I can call view using:
select * from pohled;
It possible set some parameter (v.ID_VYSETRENI)?
Some like this:
select * from pohled WHERE v.ID_VYSETRENI = 2;
Thank you for your help
ID_VYSETRENI is not declared in your view specification, so add the field to the view specification, something like:
SELECT MIN(v.DOBA_VYSETRENI), v.ID_PACIENTA, v.ID_DOKTORA, d.MIN_DOBA, v.ID_VYSETRENI
FROM vysetreni v, doktor d
WHERE v.ID_DOKTORA = d.ID_DOKTORA
AND v.ID_VYSETRENI = 1
GROUP BY v.ID_DOKTORA, v.ID_PACIENTA, d.MIN_DOBA, v.ID_VYSETRENI;
And then you will be able to reference it when you query your view.
select * from pohled WHERE ID_VYSETRENI = 2;
Note that I included your field in your GROUP clause, however, as you may know already, could use MAX or MIN, you should know better the logic of your desired resulting data set to include the column, so I leave that to you.
And BTW OldProgrammer is right this should be tagged as SQL only.
Related
In myView in the database, I have:
SELECT orderhdr3_.id AS col_0_0_,
gnrlorg2_.id AS col_1_0_,
gnrlorg2_.name AS col_2_0_,
gnrlcustom1_.id AS col_3_0_,
gnrlcustom1_.name AS col_4_0_,
orderhdr3_.order_no AS col_5_0_,
orderhdr3_.cust_ref_no AS col_6_0_,
tripinfo4_.job_no AS col_7_0_,
orderhdr3_.order_type AS col_8_0_,
servicetyp5_.code AS col_9_0_,
tripinfo4_.service_provider_id AS col_10_0_,
tripinfo4_.service_personnel AS col_11_0_,
orderhdr3_.status AS col_12_0_,
orderhdr3_.completed_date AS col_13_0_,
orderhdr3_.reason_code AS col_14_0_,
orderhdr3_.remarks AS col_15_0_,
orderhdr3_.source_order_hdr_location_id AS col_16_0_,
orderhdrlo7_.unit_no AS col_17_0_,
orderhdrlo7_.name AS col_18_0_,
orderhdrlo7_.address AS col_19_0_,
orderhdrlo7_.postal_code AS col_20_0_,
orderhdrlo7_.zone AS col_21_0_,
orderhdrlo7_.contact AS col_22_0_,
orderhdrlo7_.phone AS col_23_0_,
orderhdrlo7_.email AS col_24_0_,
orderhdrlo7_.op_date AS col_25_0_,
orderhdrlo7_.instruction AS col_26_0_,
orderhdr3_.dest_order_hdr_location_id AS col_27_0_,
orderhdrlo6_.unit_no AS col_28_0_,
orderhdrlo6_.name AS col_29_0_,
orderhdrlo6_.address AS col_30_0_,
orderhdrlo6_.postal_code AS col_31_0_,
orderhdrlo6_.zone AS col_32_0_,
orderhdrlo6_.contact AS col_33_0_,
orderhdrlo6_.phone AS col_34_0_,
orderhdrlo6_.email AS col_35_0_,
orderhdrlo6_.op_date AS col_36_0_,
orderhdrlo6_.instruction AS col_37_0_,
orderdet11_.product_code AS col_39_0_,
orderdet11_.product_desc AS col_40_0_,
orderdet11_.order_qty AS col_41_0_,
orderdet11_.serviced_qty AS col_42_0_,
orderdet11_.reason_code AS col_43_0_,
orderhdr3_.active_ind AS col_44_0_,
tripinfo4_.active_ind AS col_45_0_,
gnrlorg2_.active_ind AS col_46_0_,
gnrlcustom1_.active_ind AS col_47_0_,
commusersc0_.comm_user_id AS col_48_0_,
orderhdr3_.org_id AS col_49_0_,
orderhdr3_.customer_id AS col_50_0_,
orderhdrlo6_.op_date AS col_51_0_,
orderhdrlo6_.op_date AS col_52_0_,
orderhdr3_.id AS col_53_0_,
orderdet11_.order_hdr_id AS col_54_0_,
orderhdrlo6_.op_date AS col_55_0_,
tripinfo4_.job_no AS col_56_0_,
orderhdr3_.seq AS col_57_0_,
orderhdr3_.order_no AS col_58_0_
FROM comm_users_customers commusersc0_
JOIN gnrl_customer gnrlcustom1_ ON commusersc0_.gnrl_customer_id = gnrlcustom1_.id
JOIN gnrl_org gnrlorg2_ ON gnrlcustom1_.gnrl_org_id = gnrlorg2_.id
CROSS JOIN order_det orderdet11_
JOIN order_hdr orderhdr3_ ON gnrlcustom1_.id = orderhdr3_.customer_id
JOIN trip_info tripinfo4_ ON orderhdr3_.trip_info_id = tripinfo4_.id
JOIN service_type servicetyp5_ ON orderhdr3_.service_type_id = servicetyp5_.id
JOIN order_hdr_location orderhdrlo6_ ON orderhdr3_.dest_order_hdr_location_id = orderhdrlo6_.id
JOIN order_hdr_location orderhdrlo7_ ON orderhdr3_.source_order_hdr_location_id = orderhdrlo7_.id
JOIN order_hdr_location_time_windows timewindow8_ ON timewindow8_.order_hdr_location_id = orderhdr3_.dest_order_hdr_location_id
JOIN order_hdr_location orderhdrlo9_ ON timewindow8_.order_hdr_location_id = orderhdrlo9_.id;
In my code, I would like to filter using WHERE
SELECT * FROM myView view WHERE view.order_no = ?
but Java/PostgreSQL give error:
ERROR: column view.order_no does not exist
Please help as this is quite complicated as it involves multiple tables and a lot of columns. I am not sure why view.order_no does not exist because it is selected in col_5_0
You can reference the view by its name:
SELECT * FROM myView WHERE id = ?
Also view is a keyword in sql.
You have renamed the column order_no as col_5_0_. So you need to use that:
SELECT *
FROM myView v
WHERE v.col_5_0_ = ?;
That is, you can only reference the columns that are returned by the view.
Renaming columns to meaningless strings seems like a really bad idea -- at least if the views are exposed to human beings. It is not clear to me why anyone would want to obfuscate them.
Also, don't use view as a table alias (or any other identifier). It is a SQL keyword, and it is best to avoid those as identifiers.
Create your view like this way and use view reference in where clause
CREATE VIEW view_name
AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
You can also filter rows during view creation.
SELECT *
FROM view_name
WHERE view_name.col_name = ?
I am looking to pass declared variables to build my string. I think I want to set my variable via a case expression but I have not done this before. Here is what I have done thus far.
DECLARE #stu_conv AS VARCHAR(5)
-- I think I need a select here.
set #stu_conv = CASE WHEN ITMMASTER.STU_0 ='KG' THEN'2.2'END
SELECT
YPRIMAT.YCROPYR_0
,ITMMASTER.TCLCOD_0
,SPRICLIST.DCGVAL_3
,ITMMASTER.TSICOD_2
,ITMMASTER.ACCCOD_0
,(BASPRI_0*#stu_conv) AS ImportstringAS Importstring
FROM LIVE.YPRIMAT
INNER JOIN LIVE.ITMMASTER ON YPRIMAT.ITMREF_0 = ITMMASTER.ITMREF_0
LEFT OUTER JOIN LIVE.SPRICLIST ON ITMMASTER.TCLCOD_0 = SPRICLIST.PLICRI1_0
WHERE SPRICLIST.PLICRD_0 = 'SPL000020'
I don't see the point for using a variable here, and trying to set it outside the query does not make sense, since you most likely want the value to reset for each row.
I would suggest moving the case expression into the query, as follows:
select
y.ycropyr_0,
i.tclcod_0,
s.dcgval_3,
i.tsicod_2,
i.acccod_0,
baspri_0 * case when i.stu_0 = 'KG' then 2.2 else 1 end as importstringas importstring
from live.yprimat y
inner join live.itmmaster i on y.itmref_0 = i.itmref_0
left outer join live.spriclist s on i.tclcod_0 = s.plicri1_0
where s.plicrd_0 = 'SPL000020'
I assumed that you want a value of 1 when stu_0 is not 'KG', but you can change this as needed.
Side note:
I modified your query to use table aliases. This makes the query shorter to write and somehow easier to read
you would need to prefix column baspri_0 with the table it belongs to (as your query is, it is not possible to tell)
I'm not sure why you're declaring a string and then multiplying it, but I would just inline the case (and add a default case?):
,(BASPRI_0 * CASE
WHEN ITMMASTER.STU_0 ='KG'
THEN 2.2
ELSE ???
END) AS Importstring
I am working on a web app and there are some long winded stored procedures and just trying to figure something out, I have extracted this part of the stored proc, but cant get it to work. The guy who did this is creating alias after alias.. and I just want to get a section to work it out. Its complaining about the ending but all the curly brackets seem to match. Thanks in advance..
FInputs is another stored procedure.. the whole thing is referred to as BASE.. the result of this was being put in a temp table where its all referred to as U. I am trying to break it down into separate sections.
;WITH Base AS
(
SELECT
*
FROM F_Inputs(1,1,100021)
),
U AS
(
SELECT
ISNULL(q.CoverPK,r.CoverPK) AS CoverPK,
OneLine,
InputPK,
ISNULL(q.InputName,r.InputName) AS InputName,
InputOrdinal,
InputType,
ParentPK,
InputTriggerFK,
ISNULL(q.InputString,r.InputString) AS InputString,
PageNo,
r.RatePK,
RateName,
Rate,
Threshold,
ISNULL(q.Excess,r.Excess) AS Excess,
RateLabel,
RateTip,
Refer,
DivBy,
RateOrdinal,
RateBW,
ngRequired,
ISNULL(q.RateValue,r.RateValue) AS RateValue,
ngClass,
ngPattern,
UnitType,
TableChildren,
TableFirstColumn,
parentRatePK,
listRatePK,
NewParentBW,
NewChildBW,
ISNULL(q.SumInsured,0) AS SumInsured,
ISNULL(q.NoItems,0) AS NoItems,
DisplayBW,
ReturnBW,
StringBW,
r.lblSumInsured,
lblNumber,
SubRateHeading,
TrigSubHeadings,
ISNULL(q.RateTypeFK,r.RateTypeFK) AS RateTypeFK,
0 AS ListNo,
0 AS ListOrdinal,
InputSelectedPK,
InputVis,
CASE
WHEN ISNULL(NewChildBW,0) = 0
THEN 1
WHEN q.RatePK is NOT null
THEN 1
ELSE RateVis
END AS RateVis,
RateStatus,
DiscountFirstRate,
DiscountSubsequentRate,
CoverCalcFK,
TradeFilter,
ngDisabled,
RateGroup,
SectionNo
FROM BASE R
LEFT JOIN QuoteInputs Q
ON q.RatePK = r.RatePK
AND q.ListNo = 0
AND q.QuoteId = 100021 )
Well, I explained the issue in the comments section already. I'm doing it here again, so future readers find the answer more easily.
A WITH clause is part of a query. It creates a view on-the-fly, e.g.:
with toys as (select * from products where type = 'toys') select * from toys;
Without the query at the end, the statement is invalid (and would not make much sense anyhow; if one wanted a permanent view for later use, one would use CREATE VIEW instead).
I created the column frfpost_short but now I am having trouble making the column usable in a query.
select t.corridor,
s.corridor_code_rb,t.roadway,s.SVYLENG2012,
round(cast(t.frfpost as float),3)) as frfpost_short,
s.FRFPOST,s.BEG_GN
from SEC_FILE_IMPORT_2014 t,NORTH_VAN_DATA_VIEW_MOD_032015 s,
where frfpost_short = s.FRFPOST -- This line is causing problems
-- I would like to make frfpost_short queryable
and t.corridor = s.CORRIDOR_CODE
order by 1
Use a subquery or repeat the expression. Aliases defined in the select are not available in the where:
select t.corridor,
s.corridor_code_rb,t.roadway, s.SVYLENG2012,
round(cast(t.frfpost as float), 3) as frfpost_short,
s.FRFPOST, s.BEG_GN
from SEC_FILE_IMPORT_2014 t join
NORTH_VAN_DATA_VIEW_MOD_032015 s
on round(cast(t.frfpost as float), 3) = s.FRFPOST and
t.corridor = s.CORRIDOR_CODE
order by 1
I fixed your query. In particular, learn to use explicit join syntax.
You can't use aliases in where clause, you need to use below solution
select t.corridor,
s.corridor_code_rb,t.roadway,s.SVYLENG2012,
round(cast(t.frfpost as float),3)) as frfpost_short,
s.FRFPOST,s.BEG_GN
from SEC_FILE_IMPORT_2014 t,NORTH_VAN_DATA_VIEW_MOD_032015 s,
where round(cast(t.frfpost as float),3)) = s.FRFPOST
and t.corridor = s.CORRIDOR_CODE
order by 1
This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.