I have a database table named car with the schema Car(maker, model, type).
I am trying to find a SQL query that finds out makers who produce only the models of the same type, and the number of those models exceeds 1.
I am a beginner to SQL and have tried the query
select maker, type
from car
group by maker, type
having count(model) > 1
But it yields wrong results. Any help will be highly appreciated.
Thanks
You need to use CAPITALISED letters for instructions:
Try this:
SELECT maker, type
FROM car
GROUB BY maker, type
HAVING COUNT(model) > 1;
And also let's not forget about the semicolon at the end.
Related
I am working on the lab test values mapping (MEASUREMENT table of the OMOP CDM).
My local mapping table (handmade) has my measurement name (in French) and the associated LOINC code.
The LOINC vocabulary has been loaded from Athena (OHDSI community tool) https://athena.ohdsi.org/search-terms/
I load my local concepts into the CONCEPT table, then use an SQL query to associate the equivalent LOINC concept_id (from concept_code mapping/LOINC source codes).
I realise that the link is not made on the LOINC concept_code.
Indeed, when I filter the CONCEPT table on a LOINC concept_code (ex 34714-6) I find no result.
select *
from omop.concept
where concept_code in ('34714-6');
When I filter on the corresponding concept_id (3032080) I find the result with the desired concept_code.
select *
from omop.concept
where concept_id in ('3032080');
I have tested concept_code like '34714__' which returns the expected line.
This is not due to the encoding because when I copy/paste the resulting concept_code (filtering on concept_id = ‘3032080’) into my query concept_code in ('34714-6') I get the same problem.
However other LOINC codes work:
select *
from omop.concept
where concept_code in ('14646-4');
When I check what symbol exacty is being used :
select ASCII(substr(concept_code,1,1))
,ASCII(substr(concept_code,2,1))
,ASCII(substr(concept_code,3,1))
,ASCII(substr(concept_code,4,1))
,ASCII(substr(concept_code,5,1))
,ASCII(substr(concept_code,6,1))
,ASCII(substr(concept_code,7,1))
from omop.concept
where concept_id = 3032080 ;
I also checked/removed the whitespaces.
The same process works on drugs (concept_code from ATC).
Can you tell me where this error comes from?
Thank you for your help.
please check that you're using the latest sql-client and JDBC driver versions
I am trying to determine a solution to filter records using LIKE with a multi-valued parameter. In a simplistic example a user wants to return a set of 5-digit Object codes by entering the following in a parameter window in a SSRS report:
#parm_Object
1,24,333,34567
This ideally would return Object codes satisfying the following criteria:
1 : All Object codes starting with '1'
24: All Object codes starting with '24'
333: Similar
34567: Object code '34567'
I guess a starting point for me would be to determine whether this could be handled in the actual query, or should I do it on the SSRS side.
general good practice is to get rid of the data you don't need ASAP. so that would be in the query.
a SSRS multivalued parameter will show up as a comma separated list in the query.
the first step is to get from this comma separated list to a table (or table function), then you can join this table and apply like operators
for example
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS a
INNER JOIN dbo.split1('test,fafa,tata') b
ON 1=1
WHERE a.COLUMN_NAME like b.Value + '%'
will return rows having column names starting with test, fafa or tata. the dbo.split1 function you have to write your own or get one from the internet. the link suggested by Tab Alleman for example.
This is my simple JPQL:
SELECT s
FROM Site s
GROUP BY s.siteType
siteResult = q.getResultList();
for (Site site : siteResult) {
// loops all sites
}
This query returns all sites, including sites of the same siteType.
I'm using JPA 2.0 Eclipselink.
Whats wrong here?
Such a query does not make sense. If you use GROUP BY, other attributes in SELECT should be aggregated. As it is said in JPA specification:
The requirements for the SELECT clause when GROUP BY is used follow
those of SQL: namely, any item that appears in the SELECT clause
(other than as an aggregate function or as an argument to an aggregate
function) must also appear in the GROUP BY clause. In forming the
groups, null values are treated as the same for grouping purposes.
If you think SQL counterpart of your query:
SELECT s.attr1, attr2, s.siteType
FROM site s
GROUP BY (s.siteType)
you notice that it is hard to imagine which possible value of attr1 and attr2 should be chosen.
In such a case EclipseLink with derby just drops GROUP BY away from the query, which is of course little bit questionable way to handle invalid JPQL. I like more how Hibernate+MySQL behaves with such a invalid JPQL, it fails with quite clear error message:
java.sql.SQLSyntaxErrorException: The SELECT list of a grouped query
contains at least one invalid expression. If a SELECT list has a GROUP
BY, the list may only contain valid grouping expressions and valid
aggregate expressions.
Answer to comment:
One Site contains probably also attributes other than siteType as well. Lets use following example:
public class Site {
int id;
String siteType;
}
and two instances: (id=1, siteType="same"), (id=2, siteType="same"). Now when type of select is Site itself (or all attributes of it) and you make group by by siteType, it is impossible to define should result have one with id value 1 or 2. Thats why you have to use some aggregate function (like AVG, which gives you average of attribute values) for remaining attributes (id in our case).
Behind this link: ObjectDB GROUP BY you can find some examples with GROUP BY and aggregates.
So I keep getting the title error. The string I am using to create the query is,
select p from Product p where p.productType.productTypeId in (:productTypeIds)
And here is a clip of the java
List<Long>partTerminologyIds = getProducTypeds(partTerminologys);
..........................................................................
query.setParameter("partTerminologyIds", productTypeIds);
I have no idea why I am getting this error, ane yes partTerminolgyId in my database is a numeric 18.
Any ideas???
So in the end it ended up being that a "foreign key" was a string in one table and an numeric in the other. I rebuilt the database with new scripts and did not reverse engineer the new database.
This query is invalid:
select p from Product p where p.productType.productTypeId in (:productTypeIds)
Do you mean:
SELECT p FROM product p WHERE p.productTypeId IN (:productTypeIds)
Or rather:
SELECT * FROM product p WHERE p.productTypeId IN (:productTypeIds)
And if so, what is the data type of productTypeId in your query. Please clarify.
This looks like a Hibernate query. You need to do query.setParameterList to specify a collection value, otherwise Hibernate won't know to expand out :productTypeIds to a list of placeholders instead of simply binding the list as a serializable blob (which I think is an awful default).
I was unable to cope with converting this Sql Query to Linq Expression
SELECT IdVehicle,
AVG(Kilometers)
FROM [Fuel]
GROUP BY IdVehicle, CONVERT(NVARCHAR, Fuel.Time, 102)
Rows are inserted into this table when someone fills fuel into vehicle. Kilometers are read from tachograph. It's possible to fill fuel twice a day so I need average value.
Ideally, LINQ result would be Dictionary.
I would really appreciate any suggestion.
For cases like these you might wanna try LinqPad (www.linqpad.net). This great tool lets you test and analyze both SQL statements and Linq expressions.