I have a column storing codes like 71xxxx, 78xxxx, 59xxxx, and so on. I need to run a query to find out all the records that have codes starting with 71.
I mean, all the rows where the the first two digits of the code column are 71. If anyone can share the SQL query for this, I would be really helpful.
Thanks.
You would use like in a where clause for the filtering:
where code like '71%'
This is pretty basic SQL. If you are not familiar with it, you should study the language a bit more.
Try this
select substr(col, 1,2),count(*) from ITEM group by substr(col, 1,2)
Related
I am sniffing out an error in my SQL query that revolves around the ARRAY_AGG function. Below are the following two lines of SQL.
The below line is the correct and complete SQL version that I ultimately want to have. This behavior is the behavior that results in a correct query.
ARRAY_TO_STRING((ARRAY_AGG(R.version ORDER BY R.released_on DESC))[1:10], ', ')
and this line here I thought would be equivalent but when I execute this line of SQL I receive ERROR: more than one row returned by a subquery used as an expression
ARRAY_TO_STRING(ARRAY_AGG((SELECT "releases"."version" FROM "releases" ORDER BY "releases"."released_on" DESC LIMIT 10)), ',')
I am arriving at this line of SQL through arel and is proving to be a little challenging but is it possible that the two lines could be equivalent? The first line is slicing an array to retrieve 10 items while the second is essentially doing the same thing albeit in rows. Could this be tweaked or will this need to be rewritten?
I haven't used ruby but it looks like the nesting in different between the two lines. Your ARRAY_AGG is getting two different sets of data.
First:
ARRAY_AGG(R.version ORDER BY R.released_on DESC)
Second:
ARRAY_AGG((SELECT "releases"."version" FROM "releases" ORDER BY "releases"."released_on" DESC LIMIT 10))
Looking there I think on your second statement is wrong, from what I see online it looks like ARRAY_AGG should be use as a column function not on a table set. Below are some links that I was looking at but I wan't sure which is correct as ARRAY_AGG looks to be a sql function and i'm not sure what flavor you are using...
https://msdn.microsoft.com/en-us/library/azure/mt763803.aspx
https://www.postgresql.org/docs/8.4/static/functions-aggregate.html
With that I think it should look like:
(SELECT ARRAY_AGG("releases"."version") FROM "releases" ORDER BY "releases"."released_on" DESC LIMIT 10)
Just a guess..
Basic SQL question about specifying a group of years in a table. Working on a database and it is wanting me to list the author, title, publication date and retail price. I have that part down but its also ask to output all titles that start with "D" and were published in the 1970s.
I have the first part down: (this is how we are taught btw)
SELECT `fAuthorID`,`fTitle`,`fPubYear`,`fRetailPrice`
FROM `tBooks`
WHERE
But I cant seem to be able to get it to output the authors with a "D" and years 1970-1979 to display.
Assuming fpubyear is a number (integer) column, the correct way of querying for a continuous range of years is to use the BETWEEN operator.
SELECT fauthorID, fTitle, fPubYear, fReatailPrice
FROM tbooks
WHERE fTitle Like 'D%'
AND fPubYear BETWEEN 1970 and 1979;
The between operator includes both ends. It has the added benefit that an index on fpubyear can be used to quickly find the matching rows - which is not the case if the number first needs to be converted to a string to be able to apply the LIKE operator on it.
LIKE is for character values ("strings"), it should not be used with other types - especially not when relying on the evil implicit data type conversion. Other database would simply reject applying like on a number column.
SELECT fauthorID, fTitle, fPubYear, fReatailPrice
FROM tbooks
WHERE fTitle Like 'D%' AND fPubYear Like '197_'
Hi Dewie You an use this query,
SELECT fAuthorID,fTitle,fPubYear,fRetailPrice
FROM tBooks
WHERE fTitle like 'D%' and fPubYear like '%197%';
Hope this will give you result. Any issues just let me know
Please help me write the script for the following statement in qlikview which I have it in SQL.
SELECT CASE
WHEN Total_A=0 THEN 0
ELSE cast(((Total_B+Total_C)/Total_A) AS decimal (5,2))
END AS ratio
I have Total_A , Total_B and Total_C in the same field called Total_val
The SQL CASE is usually replaceable by the QlikView if().
Try this
if(Total_A=0,0,(Total_B+Total_C)/Total_A) as Ratio
if the A,B,C switch is inside the Val column then it will get a lot more tricky as you will have to aggregate and use nested ifs. But I believe the statement I wrote is equivalent to the SQL you gave us. If my answer doesn't work please give us a few rows of data to look at
i want select similar rows accourding to row's title columun. Title columun has mostly have 5 or 6 six keywords. Which algorithm do you recommend ? Soundex Maybe ?
P.S: Title columun has unicode chracters like Ç, Ö, Ş...
My question's answer mysql full text search. Also it supports unicode.
SELECT *, match(project_title) against('sample project 55') as similarity
FROM projects
WHERE status IN(1, 2, 3, 4, 5, 6) AND id != ? AND match('sample project 55') against(?)
ORDER BY similarity DESC
If you mean similar in spelling and pronunciation, I'd look into using the SOUNDEX function.
Honestly, I'd create a table for keywords(id, external_id, keyword), and then I would join the table against itself, order by how many matches there are, and then grab the rows back out.
If you're matching against a single row, you can select only that one, for much better efficiency with the join.
This could be combined with SOUNDEX to match together things that are close
I'm having trouble with Microsoft Access 2003, it's complaining about this statement:
select cardnr
from change
where year(date)<2009
group by cardnr
having max(time+date) = (time+date) and cardto='VIP'
What I want to do is, for every distinct cardnr in the table change, to find the row with the latest (time+date) that is before year 2009, and then just select the rows with cardto='VIP'.
This validator says it's OK, Access says it's not OK.
This is the message I get: "you tried to execute a query that does not include the specified expression 'max(time+date)=time+date and cardto='VIP' and cardnr=' as part of an aggregate function."
Could someone please explain what I'm doing wrong and the right way to do it? Thanks
Note: The field and table names are translated and do not collide with any reserved words, I have no trouble with the names.
Try to think of it like this - HAVING is applied after the aggregation is done.
Therefore it can not compare to unaggregated expressions (neither for time+date, nor for cardto).
However, to get the last (principle is the same for getting rows related to other aggregated functions as weel) time and date you can do something like:
SELECT cardnr
FROM change main
WHERE time+date IN (SELECT MAX(time+date)
FROM change sub
WHERE sub.cardnr = main.cardnr AND
year(date)<2009
AND cardto='VIP')
(assuming that date part on your time field is the same for all the records; having two fields for date/time is not in your best interest and also using reserved words for field names can backfire in certain cases)
It works because the subquery is filtered only on the records that you are interested in from the outer query.
Applying the same year(date)<200 and cardto='VIP' to the outer query can improve performance further.