I'm trying to run a similar query:
sql = #"with t(id) as (select 1 )
select * from Project
where id > (select id from t)";
var projects = this.Db.Query<Project>(sql).ToArray();
For some reason the OrmLite decides to treat the sql as as "where" clause, so what ends up running is something like this:
select field1, field2 from project where with t(id) .....
Does it look for a "select" at a starting position of the query ?
Short of creating a view - is there a way to run query with CTE ?
Use the db.Sql* API's for raw SQL Queries, e.g:
var projects = db.SqlList<Project>(sql);
Related
I tried to convert an SQL query into Gosu Script ( Guidewire). My script is working only for limited number of records
This is the SQL query
select PolicyNumber,* from pc_policyperiod
where ID in ( Select ownerID from pc_PRActiveWorkflow
where ForeignEntityID in (Select id from pc_workflow where State=3))
This is my script
var workFlowIDQuery = Query.make(Workflow).compare(Workflow#State,Relop.Equals,WorkflowState.TC_COMPLETED).select({QuerySelectColumns.path(Paths.make(entity.Workflow#ID))}).transformQueryRow(\row ->row.getColumn(0)).toTypedArray()
var prActiveWorkFlowQuery = Query.make(PRActiveWorkflow).compareIn(PRActiveWorkflow#ForeignEntity, workFlowIDQuery).select({QuerySelectColumns.path(Paths.make(entity.PRActiveWorkflow#Owner))}).transformQueryRow(\row -> row.getColumn(0)).toTypedArray()
var periodQuery = Query.make(PolicyPeriod).compareIn(PolicyPeriod#ID,prActiveWorkFlowQuery).select()
for(period in periodQuery){
print(period.policynmber)
}
Can anyone find a cause; why the script results in limited records or suggest improvements?
I would suggest you to write a single Gosu Query to select policyPeriod and join 3 entities with a foreign key to other entity.
I am note sure if the PolicyPeriod ID is same as the PRActiveWorkflow ID. Can you elaborate the relation between PolicyPeriod and PRActiveWorkflow entity ?
Is it possible to use .QueryMultiple (or some other method) in Dapper, and use the results of each former query to be used in the where clause of the next query, without having to do each query individually, get the id, and then .Query again, get the id and so on.
For example,
string sqlString = #"select tableA_id from tableA where tableA_lastname = #lastname;
select tableB_id from tableB WHERE tableB_id = tableA_id";
db.QueryMultiple.(sqlString, new {lastname = "smith"});
Is something like this possible with Dapper or do I need a view or stored procedure to accomplish this? I can use multiple joins for one SQL statement, but in my real query there are 7 joins, and I didn't think I should return 7 objects.
Right now I'm just using object.
You can store every previous query in table parameter and then first perform select from the parameter and query for next, for example:
DECLARE #TableA AS Table(
tableA_id INT
-- ... all other columns you need..
)
INSERT #TableA
SELECT tableA_id
FROM tableA
WHERE tableA_lastname = #lastname
SELECT *
FROM #TableA
SELECT tableB_id
FROM tableB
JOIN tableA ON tableB_id = tableA_id
When I use LIKE operator in a Query in Access and I create a OLEDB connection in Excel to import this data to Excel, the data don't update even after change the query with another filter. Is like if I had not changed the query with the new filter.
I can solve this taking this query and adding a INTO TempTable and linking this temptable in a OLEDB connection in excel. In this case the changes are reflected, but the query directly not.
Query
SELECT tb_fechamento_ddd.PERIODO,
tb_fechamento_ddd.DDD,
tb_fechamento_ddd.metrica,
tb_fechamento_ddd.categoria,
tb_fechamento_ddd.Qtd
FROM tb_fechamento_ddd
WHERE (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Like "*GSM*"
And (tb_fechamento_ddd.metrica) Not Like "*CDMA*"
And (tb_fechamento_ddd.metrica) Not Like "*LTE*"))
AND ((tb_fechamento_ddd.categoria)="Pre"))
OR (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Like "*GSM*"
And (tb_fechamento_ddd.metrica) Not Like "*CDMA*"
And (tb_fechamento_ddd.metrica) Not Like "*LTE*")
And (tb_fechamento_ddd.metrica) Like "Migra*")
AND ((tb_fechamento_ddd.categoria) Like "*Pré*"));
P.S: Sorry by excessive parenthesis, and duplicated filters in Where clause, but the access don't work Appropriately if I don't use this.
Based on post cited by HansUp:
I change every LIKE to ALIKE operator and change * of Access pattern by % of ANSI pattern.
Then:
SELECT tb_fechamento_ddd.PERIODO,
tb_fechamento_ddd.DDD,
tb_fechamento_ddd.metrica,
tb_fechamento_ddd.categoria,
tb_fechamento_ddd.Qtd
FROM tb_fechamento_ddd
WHERE (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Alike "%GSM%"
And (tb_fechamento_ddd.metrica) Not Alike "%CDMA%"
And (tb_fechamento_ddd.metrica) Not Alike "%LTE%"))
AND ((tb_fechamento_ddd.categoria)="Pre"))
OR (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Alike "%GSM%"
And (tb_fechamento_ddd.metrica) Not Alike "%CDMA%"
And (tb_fechamento_ddd.metrica) Not Alike "%LTE%")
And (tb_fechamento_ddd.metrica) Alike "Migra%")
AND ((tb_fechamento_ddd.categoria) Alike "%Pré%"));
Another bugs of Access Query on Excel that I get solutions:
Queries made with UNION don't is showed on list of queries on create a connection in Excel:
Create a SubQuery to "shelter" the query with Unions:
Select * from table_A
UNION ALLL
Select * from table_B
to
Select * from
(Select * from table_A
UNION ALLL
Select * from table_B) as SubQryUnion
Another bug:
If you use on field with a aggregate function a alias that have a same name that a field of table and use this alias in a expression field, the query doesn't is showed.
Select field1, field2, sum(table.qtd) as qtd, qtd/12 as yearmean
from table
group by field1, field2
Choice another name to aggregate field:
Select field1, field2, sum(table.qtd) as quantity, quantity/12 as yearmean
from table
group by field1, field2
Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.
I want to perform a simple join on two tables (BusinessUnit and UserBusinessUnit), so I can get a list of all BusinessUnits allocated to a given user.
The first attempt works, but there's no override of Select which allows me to restrict the columns returned (I get all columns from both tables):
var db = new KensDB();
SqlQuery query = db.Select
.From<BusinessUnit>()
.InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
.Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
.And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );
The second attept allows the column name restriction, but the generated sql contains pluralised table names (?)
SqlQuery query = new Select( new string[] { BusinessUnitTable.IdColumn, BusinessUnitTable.NameColumn } )
.From<BusinessUnit>()
.InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
.Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
.And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );
Produces...
SELECT [BusinessUnits].[Id], [BusinessUnits].[Name]
FROM [BusinessUnits]
INNER JOIN [UserBusinessUnits]
ON [BusinessUnits].[Id] = [UserBusinessUnits].[BusinessUnitId]
WHERE [BusinessUnits].[RecordStatus] = #0
AND [UserBusinessUnits].[UserId] = #1
So, two questions:
- How do I restrict the columns returned in method 1?
- Why does method 2 pluralise the column names in the generated SQL (and can I get round this?)
I'm using 3.0.0.3...
So far my experience with 3.0.0.3 suggests that this is not possible yet with the query tool, although it is with version 2.
I think the preferred method (so far) with version 3 is to use a linq query with something like:
var busUnits = from b in BusinessUnit.All()
join u in UserBusinessUnit.All() on b.Id equals u.BusinessUnitId
select b;
I ran into the pluralized table names myself, but it was because I'd only re-run one template after making schema changes.
Once I re-ran all the templates, the plural table names went away.
Try re-running all 4 templates and see if that solves it for you.