Can anyone explain what is wrong wtih the below query - mysql

Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.
SELECT DISTINCT CITY
FROM STATION
WHERE (SUBSTR(CITY,1,1)
AND SUBSTR(CITY,-1,1)) NOT IN ("a","e","i","o","u");
Can someone explain what's wrong with this MySQL query?

Both the first and last letters require assertions:
SELECT DISTINCT CITY
FROM STATION
WHERE LEFT(CITY, 1) NOT IN ('a', 'e', 'i', 'o', 'u') AND
RIGHT(CITY, 1) NOT IN ('a', 'e', 'i', 'o', 'u');
We can also handle this with regular expressions:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY REGEXP '^[^aeiou].*[^aeiou]$';

Related

Getting the most common value in a table

I'm struggling to work out how to get the most commonly occurring value from a table in MySQL.
Example:
CREATE TABLE words(`letter1` char(1), `letter2` char(1));
INSERT INTO words(`letter1`, `letter2`)VALUES
('A', 'A'), ('B', 'A'), ('C', 'A'), ('D', 'A'), ('D', 'B'),
('B', 'B'), ('D', 'B'), ('A', 'C'), ('B', 'D'), ('D', 'A');
So for letter1 I want to pick out the value 'D' and for letter2 I want to pick out 'A'.
For a tie I'm not too bothered which of the tied values it picks.
Thanks for any help, It looks like it ought to be easy but I can't figure it out. For one letter it would be easy but for multiple I don't know how to.
SELECT ( SELECT letter1
FROM table
GROUP BY 1
ORDER BY COUNT(*) DESC LIMIT 1 ) letter1,
( SELECT letter2
FROM table
GROUP BY 1
ORDER BY COUNT(*) DESC LIMIT 1 ) letter2;
If two or more letters have the same and maximal amount of occurences then one indefinite letter of these letters (but in most cases - the least lexicographically) will be returned.
Will it work for you?
Query for letter1
SELECT letter1 FROM words GROUP BY letter1 ORDER BY COUNT(1) DESC LIMIT 1;
Query for letter2
SELECT letter2 FROM words GROUP BY letter2 ORDER BY COUNT(1) DESC LIMIT 1;
For calculations based on individual columns, Akina already provided an answer. Just in case you'd like to find the mostly used pair , you can try this if you don't care which pair to pick in case of a tie:
select count(*) ct,letter1,letter2 from words group by letter1,letter2 order by ct desc limit 1;

How to get the latest value?

I am trying to write a query and I want to get only the max(date), and I have to group by the rest of info such as job_ID, invoice, total_paid, and payment_method.
I can't group by the payment method because logically its not correct and also because the payment methods are different... and, I cannot use listagg here.
Any idea how can I accomplish my goal?
I almost reached the end but the payment method cause some errors in the code...
Thank you in advance :)
You can get the row with the max date for any id with a qualify row_number() over():
with data as (
select $1 id, $2 date, $3 value
from values (1, 1, 'a')
, (1, 2, 'b')
, (1, 3, 'c')
, (2, 1, 'e')
, (2, 2, 'f')
, (2, 3, 'g')
)
select *
from data
qualify row_number() over(partition by id order by date desc) = 1
;
If this is not what you want — you will need to improve the question by detailing sample input data and desired results.

Why is my OR operator not working in my WHERE clause?

I need to write a where statement that only returns names starting with A, B, C, or E. I have a WHERE clause with a less that D condition, and that seems to work fine. But my Equals E condition doesn't appear to be working.
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name < 'D'
OR vendor_contact_last_name = 'E'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
It is only returning names that start with A, B, and C.
You can use the following using LEFT:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE LEFT(vendor_contact_last_name, 1) IN ('A', 'B', 'C', 'E')
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
... or using RLIKE:
SELECT vendor_name,
CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS full_name
FROM vendors
WHERE vendor_contact_last_name RLIKE '^[A-CE]'
ORDER BY vendor_contact_last_name, vendor_contact_first_name;
demo on dbfiddle.uk

How to get the average number of Item Sold per week in SQL?

I have table of sold item which has following columns like
SoldItemID
SoldID
SoldAmount
DateOfPurchase
DateOfActivation
CreatedDate
How to get the average number of Item sold Per week in SQL?
Any help would be highly appreciated!
I know I am late to the party, but just in case other people are looking, this might work:
select date_part('week', transaction_date) week_of_the_year, item_id,
avg(sold_qty) from your_table group by 1, 2 order by 1
For SQL SERVER
WITH CTE AS(
SELECT
*
FROM (
VALUES
('2018-08-23', 'A', 10),
('2018-08-23', 'B', 10),
('2018-08-24', 'A', 14),
('2018-08-31', 'A', 8),
('2018-08-31', 'B', 10)
) as list (Date_, Item, Amount)
)
select
WKnum, MIN(Date_), MAX(Date_), Item, AVG(Amount)
from(
select
*,
DATEPART(WK, Date_) WKnum
from CTE
) as A
group by WKnum, Item
Try below query: it will work on sql server
select datepart(week,DateOfPurchase),avg(item)
from
(
select DateOfPurchase,count(SoldItemID) item
from tablename group by DateOfPurchase) a
group by datepart(week,DateOfPurchase)

GROUP CONCAT with ORDER in MEMSQL

Here is a toy example:
CREATE TABLE TEST
(
ID INT,
AGG NVARCHAR(20),
GRP NVARCHAR(20)
);
INSERT INTO TEST VALUES
(1, 'AB', 'X'), (2, 'BC', 'X'), (3, 'AC', 'X'),
(4, 'EF', 'Y'), (5, 'FG', 'Y'), (6, 'DC', 'Y'),
(7, 'JI', 'Z'), (8, 'IJ', 'Z'), (9, 'JK', 'Z');
Now, I would like to do this (this is a valid code in MySQL, but not in MEMSQL):
SELECT
COUNT(*),
SUM(ID),
GROUP_CONCAT(AGG ORDER BY AGG),
GRP
FROM TEST
GROUP BY GRP
So that the output looks like this (Required Output):
3 6 AB,AC,BC X
3 15 DC,EF,FG Y
3 24 IJ,JI,JK Z
Note that the values in the third column are sorted for each row. My output looks like this (Current Wrong Output):
3 6 BC,AB,AC X
3 15 DC,EF,FG Y
3 24 IJ,JI,JK Z
Compare each row in the third column, the lists are sorted.
However, since the above query is not valid in MEMSQL, I have to remove the ORDER BY AGG part in GROUP_CONCAT which causes the third column to not be sorted.
As per the documentation of GROUP_CONCAT, the expression can also be a function, however, there is no built in function to sort. I have tried many combinations of SELECT ... ORDER BY statements in GROUP_CONCAT without success. Is this impossible to do, or am I missing something?
I think this works for my case.
SELECT
COUNT(*),
SUM(T.ID),
GROUP_CONCAT(T.AGG),
T.GRP
FROM (
SELECT
*,
RANK() OVER(PARTITION BY GRP ORDER BY AGG) AS R
FROM TEST
) T
GROUP BY T.GRP
ORDER BY T.R
It is rather convoluted, so I hope someone can suggest an improvement.
Try this:
SELECT
COUNT(*),
SUM(ID),
GROUP_CONCAT(AGG),
GRP
FROM TEST
GROUP BY GRP
ORDER BY GROUP_CONCAT(AGG)