Select EVENTPLAN.PLANNO, EVENTPLANLINE.LINENO, RESOURCETBL.RESNAME,
COUNT(EVENTPLANLINE.NUMBERFLD) AS NUMBEROFRESOURCES,
LOCATION.LOCNAME, EVENTPLANLINE.TIMESTART, EVENTPLANLINE.TIMEEND
FROM EVENTPLAN, RESOURCETBL, EVENTPLANLINE, LOCATION, FACILITY
WHERE EVENTPLAN.PLANNO = EVENTPLANLINE.PLANNO
AND EVENTPLANLINE.RESNO = RESOURCETBL.RESNO
AND EVENTPLANLINE.LOCNO = LOCATION.LOCNO
AND FACILITY.FACNO = LOCATION.FACNO
AND FACILITY.FACNAME = 'Basketball arena'
AND EVENTPLAN.ACTIVITY = 'Operation'
AND EVENTPLAN.WORKDATE BETWEEN '1-OCT-13' AND '31-DEC-13'
GROUP BY EVENTPLAN.PLANNO, EVENTPLANLINE.LINENO,
RESOURCETBL.RESNAME,EVENTPLANLINE.NUMBERFLD;
On running this query I am getting an error: Not a group by function. Can someone please tell me why am I getting this error? I have added all the fields in the GROUP BY function.
When you use an aggregate function ALL scalar fields must be in GROUP BY function.
You have missed these:
LOCATION.LOCNAME, EVENTPLANLINE.TIMESTART, EVENTPLANLINE.TIMEEND
So, the right query will be:
SELECT
EVENTPLAN.PLANNO, EVENTPLANLINE.LINENO, RESOURCETBL.RESNAME,
COUNT(EVENTPLANLINE.NUMBERFLD) AS NUMBEROFRESOURCES,
LOCATION.LOCNAME, EVENTPLANLINE.TIMESTART, EVENTPLANLINE.TIMEEND
FROM EVENTPLAN, RESOURCETBL, EVENTPLANLINE, LOCATION, FACILITY
WHERE EVENTPLAN.PLANNO = EVENTPLANLINE.PLANNO
AND EVENTPLANLINE.RESNO = RESOURCETBL.RESNO
AND EVENTPLANLINE.LOCNO = LOCATION.LOCNO
AND FACILITY.FACNO = LOCATION.FACNO
AND FACILITY.FACNAME = 'Basketball arena'
AND EVENTPLAN.ACTIVITY = 'Operation'
AND EVENTPLAN.WORKDATE BETWEEN '1-OCT-13' AND '31-DEC-13'
GROUP BY EVENTPLAN.PLANNO, EVENTPLANLINE.LINENO, RESOURCETBL.RESNAME,
LOCATION.LOCNAME, EVENTPLANLINE.TIMESTART, EVENTPLANLINE.TIMEEND
Related
So i try a make a view , actually this is my code :
drop table if exists computed_datum_hours_base;
create view computed_datum_hours_base
as select
toStartOfHour(datetime_value) as datetime_desc,
computed_id,
computed_kind,
computed_type,
case
when computed_type = 'intensive' then avg(value)
when computed_type = 'extensive.some' then sum(value)
when computed_type = 'extensive.differential' then
(
select value as value_f from ref_computed_datum
where ref_computed_id = computed_id
and ref_computed_kind = computed_kind
and ref_computed_type = computed_type
and ref_datetime_value = toStartOfHour(addHours(datetime_value, 1))
) - (
select value as value_f from ref_computed_datum
where ref_computed_id = computed_id
and ref_computed_kind = computed_kind
and ref_computed_type = computed_type
and ref_datetime_value = toStartOfHour(datetime_value)
)
end as value,
count(uuid) as nb_value
from computed_datum
join ref_computed_datum
on computed_id = ref_computed_id
and computed_kind = ref_computed_kind
and computed_type = ref_computed_type
where uuid = ref_uuid
group by
computed_id,
computed_kind,
computed_type,
toStartOfHour(datetime_value)
;
my issue is on the case for extensive.differential ...
clickhouse say he can found the column for computed_id ... like the subrequest is scoped and didn't have access to the colum from the main requeste ...
So this is another bug of clickhouse ?
Or there are a reel scope and i can't do this like this ...
( so How can do this ? )
Edit: full error
Code: 47, e.displayText() = DB::Exception: Missing columns: 'datetime_value' 'computed_kind' 'computed_type' 'computed_id' 'value' while processing query: 'SELECT value AS value_f FROM api_client.ref_computed_datum WHERE (ref_computed_id = computed_id) AND (ref_computed_kind = computed_kind) AND (ref_computed_type = computed_type) AND (ref_datetime_value = toStartOfHour(addHours(datetime_value, 1)))', required columns: 'value' 'computed_id' 'ref_computed_id' 'ref_computed_kind' 'computed_type' 'ref_computed_type' 'computed_kind' 'ref_datetime_value' 'datetime_value', source columns: 'ref_flags' 'ref_computed_kind' 'ref_computed_id' 'ref_datetime_value' 'ref_computed_type' 'ref_EventDateTime' 'ref_insert' 'ref_value' 'ref_uuid' (version 20.4.2.9 (official build))
computed_datum folow this structure :
EventDateTime DateTime default now(),
insert String,
uuid String default generateUUIDv4(),
datetime_value DateTime,
computed_id Int32,
computed_kind String,
computed_type String,
value Float64,
flags String
```
I make a ref view that only prefix all colum with ref_ for making a walkaround about the alias bug.
At this time clickhouse didn't support correlated query ...
cf: https://github.com/ClickHouse/ClickHouse/issues/6697
I'm trying to write the following SQL Server query, where I also need to fetch the count of vehicleId that is being referred in the child table,
select
BV.[BaseVehicleId], BV.[MakeId], MK.[MakeName], BV.[ModelId],
MD.[ModelName], V.[VehicleId], V.[SubModelId], SubMD.[SubModelName],
V.[RegionId], V.PublicationStageId,
V.[LastUpdateDate], V.[InsertDate], V2BedCount, V2BodyCount,
V2BrakeCount, V2DriveCount, V2EngineCount,V2MfrCount, V2SpringCount,
V2SteeringCount, V2TransmissionCount, V2WheelCount
from
[dbo].[BaseVehicle] BV,
[dbo].[Make] MK,
[dbo].[Model] MD,
[dbo].[SubModel] SubMD,
[dbo].[VehicleToBedConfig] V2Bed,
[dbo].[VehicleToBodyStyleConfig] V2Body,
[dbo].[VehicleToBrakeConfig] V2Brake,
[dbo].[VehicleToDriveType] V2Drive,
[dbo].[VehicleToEngineConfig] V2Engine,
[dbo].[VehicleToMfrBodyCode] V2Mfr,
[dbo].[VehicleToSpringTypeConfig] V2Spring,
[dbo].[VehicleToSteeringConfig] V2Steering,
[dbo].[VehicleToTransmission] V2Transmission,
[dbo].[VehicleToWheelBase] V2Wheel,
[dbo].[Vehicle] V
where
V.[PublicationStageId] = '4'
and V.[DeleteDate] IS NULL
and BV.[BaseVehicleId] = V.[BaseVehicleId]
and MK.[MakeId] = BV.[MakeId]
and MD.[ModelId] = BV.[ModelId]
and V.[SubModelId] = SubMD.[SubModelId]
and V.[VehicleId] = V2Bed.[VehicleId]
and V.[VehicleId] = V2Body.[VehicleId]
and V.[VehicleId] = V2Brake.[VehicleId]
and V.[VehicleId] = V2Drive.[VehicleId]
and V.[VehicleId] = V2Engine.[VehicleId]
and V.[VehicleId] = V2Mfr.[VehicleId]
and V.[VehicleId] = V2Spring.[VehicleId]
and V.[VehicleId] = V2Steering.[VehicleId]
and V.[VehicleId] = V2Transmission.[VehicleId]
and V.[VehicleId] = V2Wheel.[VehicleId]
I'm looking for a way to push the details on these columns from the above query:
V2BedCount, V2BodyCount, V2BrakeCount,
V2DriveCount, V2EngineCount,
V2MfrCount, V2SpringCount, V2SteeringCount, V2TransmissionCount,
V2WheelCount
Here V2BedCount is the count of Vehicle ID's that are mapped with VehicleToBedConfig table like
select COUNT(VehicleId) V2BedCount
from VehicleToBedConfig
group by VehicleId
Please let me know how do I insert the second query in the first to have one query populate the count details for all these columns
V2BedCount, V2BodyCount, V2BrakeCount,
V2DriveCount, V2EngineCount,
V2MfrCount, V2SpringCount, V2SteeringCount, V2TransmissionCount,
V2WheelCount
Assuming Id is a PK in VehicleToBedConfig try
COUNT(DISTINCT V2Bed.Id) OVER(PARTITION BY V.VehicleId) V2BedCount,
...
I advice you use an explicit JOIN syntax.
When I run the following query, I will get a result but it is not looking correct to me. So there must be a mistake somewhere in it.
SELECT *
FROM fe_users
INNER JOIN user_managementsg_user_table AS umut ON
fe_users.uid = umut.fe_id
AND fe_users.city = 'Bern'
OR fe_users.city = 'Basel'
OR fe_users.city = 'Solothurn'
OR fe_users.city = 'Aargau'
AND umut.funktion = 'Assistenzarzt'
OR umut.fachgebiet_rheumatologie = 1
OR umut.taetigkeitsfeld = 'Praxis'
This is what I want:
fe_users.city is either Bern OR Basel OR Solothurn OR Aargau
AND
Either umut.funktion is = 'Assistenzarzt' OR umut.fachgebiet_rheumatologie = 1 OR umut.taetigkeitsfeld = 'Praxis'
When I run my query above, some values of the column city are just different than the 4 cities that I have defined. What is wrong with my query?
SELECT * FROM fe_users
INNER JOIN user_managementsg_user_table AS umut
ON fe_users.uid = umut.fe_id
AND (fe_users.city in ('Bern','Basel','Solothurn','Aargau')
AND (umut.funktion = 'Assistenzarzt' OR umut.fachgebiet_rheumatologie = 1 OR umut.taetigkeitsfeld = 'Praxis')
Try this.
SELECT *
FROM fe_users
INNER JOIN user_managementsg_user_table AS umut ON fe_users.uid = umut.fe_id
Where fe_users.city IN('Bern'
,'Basel'
,'Solothurn'
, 'Aargau')
AND (umut.funktion in('Assistenzarzt') OR umut.fachgebiet_rheumatologie = 1 OR umut.taetigkeitsfeld = 'Praxis'))
I have problem writing group by sql into django app. Can any of you django users help me how to write this sql into django-friendly code? This is my model:
class Stock(models.Model):
name = models.CharField("Stock's name", max_length=200)
symbol = models.CharField("Stock's symbol", max_length=20)
class Dividend(models.Model):
amount = models.FloatField(default=0)
date = models.DateField('pay date')
stock = models.ForeignKey(Stock)
class UserStock(models.Model):
amount = models.FloatField('amount', default=0)
date = models.DateField('buy date')
price = models.FloatField('price', default=0)
user = models.ForeignKey(User)
stock = models.ForeignKey(Stock)
And this is sql code I want to write in django:
select stock_id, sum(price), sum(amount) as price from stocks_userstock group by stock_id;
I was trying to write something like this.
my_stock = UserStock.objects.filter(user=request.user)\
.annotate(sum_price = sum('price'), sum_amount = sum('amount'))
Thanks in advance, I hope it won't be a problem for some of you.
I believe just adding a values call will do that
my_stock = UserStock.objects.get(user=request.user)\
.values('stock_id').annotate(sum_price = sum('price'), sum_amount = sum('amount'))
and you will get back a list of dicts similar to
[
{'stock_id': 0, 'sum_price': 10, 'sum_amount': 25},
...
]
see here for more info
stock = Stock.objects.all().annotate(sum_price = Sum('user_stock__price'), sum_amount = Sum('user_stock__amount'))
It should work.
I've removed the User filter in my snippet to simplify but you can add it of course.
I have this nhibernate query:
var q =
NHibernateSession.Current.CreateSQLQuery
(
#"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
It looks for the lastest events on each container for the given lineCompany, it works, but i dont know how to set this resultset to a list of T HistoricEvent, i try this line:
.SetResultTransformer(Transformers.AliasToBean(typeof(HistoricEvent)));
But throws NHibernate.PropertyNotFoundException:Could not find a setter for property 'Event_Id' in class 'HistoricEvent'.
Is there any way to do this?, or maybe doing this same query using the ICriteria API?
Thx in advance.
Assuming HistoricEvent is a mapped entity, the following should give you what you're looking for:
var q =
NHibernateSession.Current.CreateSQLQuery
(
#"SELECT LastestEvents.*
FROM (
SELECT DISTINCT SbQcontainer.Container
FROM HistoricEvents
SbQcontainer WHERE SbQcontainer.LineCompany_Id = :lineCompany) as Sbq
JOIN HistoricEvents as LastestEvents
ON LastestEvents.id = (
SELECT TOP(1) id
FROM HistoricEvents mi
WHERE mi.Container = Sbq.Container and mi.LineCompany_Id = :lineCompany
ORDER BY mi.Date DESC
)"
).AddEntity(typeof(HistoricEvent));
q.SetParameter("lineCompany",lineCompany.Id);
q.SetCacheable(false);
var results = q.List<HistoricEvent>().ToList();
See the relevant documentation for further details.