procedure oracle inner join - oracle11g

CREATE OR replace PROCEDURE Proc_factura
IS
BEGIN
SELECT venta.rut_cliente,
cliente.nombre,
cliente.direccion,
cliente.telefono,
venta.fecha,
vendedor.nombre,
venta.codigo_vehiculo,
vehiculo_nuevo.marca,
vehiculo_nuevo.modelo,
vehiculo_nuevo.cilindrada,
vehiculo_nuevo.precio * 1.19
FROM venta
inner join cliente
ON venta.rut_cliente = cliente.rut
inner join vendedor
ON vendedor.rut = venta.rut_vendedor
inner join vehiculo_nuevo
ON venta.codigo_vehiculo = vehiculo_nuevo.codigo;
END;

Well, you need to SELECT those values INTO something in order to make it compile (at least). Will it work, that's another question.

Related

Two queries in one statment

I want in put two queries in one statement how can i do that in this state?
1
stmt = `SELECT Comments.*, Users.username,Users.avatar from Users
INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id= 1`
2
`SELECT COUNT(*) comment FROM Comments WHERE Comments.post_id= 1`;
Cross join the 1st query to the 2nd:
SELECT c.*, u.username, u.avatar, t.counter
FROM Users u INNER JOIN Comments c
ON c.users_id = u.users_id
CROSS JOIN (SELECT COUNT(*) counter FROM Comments WHERE post_id = 1) t
WHERE c.post_id = 1
I don't know much about SQLite, but in SQL Server you can use ";" to use multiple queries.
Maybe it does work for SQLite.
You can use GROUP BY on comments table in a way:
'SELECT COUNT(Comments.<id>), Comments.*, Users.username,Users.avatar
from Users INNER JOIN Comments ON Comments.users_id =Users.users_id
WHERE Comments.post_id = 1 GROUP BY Comments.<id>';
*This syntax of GROUP BY clause follows PostgreSQL. You might need to tweak according to the syntax followed by sqlite.

How do i join two tables and get data from both of the table to show to users on form field?

Table A (goradid) has a field which is a FK to Table B(Spriden). I need two sets of data (spriden_id and Goradid_additional_id) from Table A and Table B and insert it into one of the field called e.g. bannerid and partyid. please see my code below as i am also getting a error SQL command not properly ended in PLSQL.
SELECT spriden_id, goradid_additional_id
FROM goradid, spriden
INNER JOIN spriden ON spriden_pidm = goradid_pidm
INTO bannerid, partyid;
The correct syntax is SELECT <COLUMN LIST> INTO <VARIABLE LIST>. So the query should be:
SELECT spriden_id, goradid_additional_id
INTO bannerid, partyid
FROM goradid a INNER JOIN spriden b ON b.spriden_pidm = a.goradid_pidm;
you can use aliasing for this.
it's simple.
SELECT spriden_id as bannerid, goradid_additional_id as partyid FROM goradid INNER JOIN spriden ON spriden_pidm = goradid_pidm;
Note: if you want to use into then you have to initialize the variable.
we use into usually.
declare
v_author author%rowtype;
begin
select * into v_author from author where author_key = 'A103';
dbms_output.put_line('Name:'||v_author.author_first_name||' '|| v_author.author_last_name);
end;

Complex AX Query

i want to rebuild this SQL Query as AX Query.
I tried it in several ways, but I don't get it.
I am not completely new to AX queries, but I only have experience with some simple queries not with such complex SQL queries.
SELECT * FROM ( SELECT DH.[RECID] AS RECID_DIMENSIONHIERARCHY
,DH.[NAME] AS NAME__DIMENSIONHIERARCHY
,DH.[DESCRIPTION] AS DESC__DIMENSIONHIERARCHY
,DH.[PARTITION] AS PARTITION_DIMENSIONHIERARCHY
,DL.[DIMENSIONATTRIBUTE] AS RECID_DIMENSIONATTRIBUTE
,DA.[NAME] AS NAME_DIMENSIONATTRIBUTE
,DN.[RECID] AS RECID_DIMENSIONCONSTRAINTNODE
,DNC.[RECID] AS RECID_DIMENSIONCONSTRAINTNODECRITERIA
,DNC.[RANGETO] AS #Owner
,DNCR.[WILDCARDSTRING] AS #Agreement
FROM (SELECT * FROM [dbo].[DIMENSIONHIERARCHY]
WHERE [STRUCTURETYPE] = 1 AND [NAME] LIKE 'AG-OW%'
) AS DH
INNER JOIN [dbo].[DIMENSIONHIERARCHYLEVEL] AS DL
ON DH.[RECID] = DL.[DIMENSIONHIERARCHY]
AND DH.[PARTITION] = DL.[PARTITION]
INNER JOIN [dbo].[DIMENSIONATTRIBUTE] AS DA
ON DL.[DIMENSIONATTRIBUTE] = DA.[RECID]
AND DL.[PARTITION] = DA.[PARTITION]
INNER JOIN [dbo].[DIMENSIONCONSTRAINTNODE] AS DN
ON DL.[RECID] = DN.[DIMENSIONHIERARCHYLEVEL]
AND DL.[PARTITION] = DN.[PARTITION]
INNER JOIN [dbo].[DIMENSIONCONSTRAINTNODECRITERIA] AS DNC
ON DN.[RECID] = DNC.[DIMENSIONCONSTRAINTNODE]
AND DN.[PARTITION] = DNC.[PARTITION]
INNER JOIN [dbo].[DIMENSIONCONSTRAINTNODECRITERIA] AS DNCR
ON DN.[PARENTCONSTRAINTNODE] = DNCR.[DIMENSIONCONSTRAINTNODE]
AND DN.[PARTITION] = DNCR.[PARTITION]
) AS Sub
You need to break down your query and implement it in small chunks. Then combine all of it to get the desired result.
There are two ways to create query in X++.
Create query using select statement for example:
Select * from HcmWorker join * from DirPerson
where DirPerson.RecId == HcmWorker.Person
See this link : Select statement syntax
Create query with AOT structure. You might want to have a look at the following link:
Create query in AOT by using X++

Update with subselect and where

The following code works fine in T-SQL, but not in JET SQL, in Access:
UPDATE Superliste_Temp
SET [Plan-TGrp-Spanne_Stfl1] =
(SELECT [Plan-TGrp-Spanne_Stfl1]
FROM Superliste_Temp
INNER JOIN dbo_Common_preferences
ON Superliste_Temp.Teil = dbo_Common_preferences.AktivesTeil)
WHERE [Teilegruppe] =
(SELECT [Teilegruppe]
FROM Superliste_Temp
INNER JOIN dbo_Common_preferences
ON Superliste_Temp.Teil = dbo_Common_preferences.AktivesTeil);
Why does it not work!?
I have a hard time looking at that SQL to figure out what it's trying to do, but I know that Jet SQL needs the SET statement after the JOINS, so my best guess is:
UPDATE Superliste_Temp, Superliste_Temp AS ST1
INNER JOIN dbo_Common_preferences AS pref1
ON ST1.Teil = pref1.AktivesTeil
SET Superliste_Temp.[Plan-TGrp-Spanne_Stfl1] = [ST1]![Plan-TGrp-Spanne_Stfl1]
WHERE (((Superliste_Temp.Teilegruppe)=[ST1]![Teilegruppe]));

NHibernate Collection Left Outer Join Where Clause Issue

It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.

Resources