How to execute this query to compare dates - sql

Write a query to display the students who are older than 'Balakrishnan'. Sort the results based on firstname in ascending order.
The output should look like this
+--------+-----------+----------+-------------+------------+-----------+
| STUDID | FIRSTNAME | LASTNAME | STREET | CITY | DOB |
+--------+-----------+----------+-------------+------------+-----------+
| 3009 | Abdul | Rahman | HAL | Bangalore | 19-JAN-88 |
| 3002 | Anand | Kumar | Indiranagar | Bangalore | 19-JAN-88 |
| 3001 | Dileep | Kumar | Jai Nagar | Bangalore | 10-MAR-89 |
| 3004 | Gowri | Shankar | Gandhipuram | Coimbatore | 22-DEC-87 |
| 3008 | John | Dravid | Mylapore | Chennai | 15-SEP-87 |
| 3006 | Prem | Kumar | Ramnagar | Coimbatore | 17-MAY-87 |
| 3007 | Rahul | Dravid | KKNagar | Chennai | 08-OCT-87 |
+--------+-----------+----------+-------------+------------+-----------+

Try this:-
It may be help you.
SELECT * FROM TABLE_NAME
WHERE DOB < TO_DATE('DOB_of_Balakrishnan','DD-MM-YYYY')
ORDER BY FIRSTNAME;
I am using oracle 11g.

As I see that the DOB of Balakrishnan is not provided...
try using this:
SELECT *
FROM table_name
WHERE dob<(SELECT dob
FROM table_name
WHERE LOWER(firstname)='bala')
ORDER BY firstname;

Related

Why does a LIMIT 1 return another result that a standard select with ordering?

I have a MariaDB database table which holds employeeNumber (PK), firstName, LastName and email.
I have several records in there (see at the end of the question).
When I do SELECT employeeNumber,lastName,firstName,email FROM employees ORDER BY lastName; it returns 1337 Bondur Loui as first result, though I think it should be 1102 Bondur Gerard. When I add a LIMIT 1 it suddenly works correctly: it returns Gerard. When I do LIMIT 2, it again select the right choice: Gerard.
Why does an order by lastName without limit return Loui while with limit it returns Gerard?
The existing records:
+----------------+-----------+-----------+---------------------------------+
| employeeNumber | firstName | lastName | email |
+----------------+-----------+-----------+---------------------------------+
| 1002 | Diane | Murphy | dmurphy#classicmodelcars.com |
| 1056 | Mary | Patterson | mpatterso#classicmodelcars.com |
| 1076 | Jeff | Firrelli | jfirrelli#classicmodelcars.com |
| 1088 | William | Patterson | wpatterson#classicmodelcars.com |
| 1102 | Gerard | Bondur | gbondur#classicmodelcars.com |
| 1143 | Anthony | Bow | abow#classicmodelcars.com |
| 1165 | Leslie | Jennings | ljennings#classicmodelcars.com |
| 1166 | Leslie | Thompson | lthompson#classicmodelcars.com |
| 1188 | Julie | Firrelli | jfirrelli#classicmodelcars.com |
| 1216 | Steve | Patterson | spatterson#classicmodelcars.com |
| 1286 | Foon Yue | Tseng | ftseng#classicmodelcars.com |
| 1323 | George | Vanauf | gvanauf#classicmodelcars.com |
| 1337 | Loui | Bondur | lbondur#classicmodelcars.com |
| 1370 | Gerard | Hernandez | ghernande#classicmodelcars.com |
| 1401 | Pamela | Castillo | pcastillo#classicmodelcars.com |
| 1501 | Larry | Bott | lbott#classicmodelcars.com |
| 1504 | Barry | Jones | bjones#classicmodelcars.com |
| 1611 | Andy | Fixter | afixter#classicmodelcars.com |
| 1612 | Peter | Marsh | pmarsh#classicmodelcars.com |
| 1619 | Tom | King | tking#classicmodelcars.com |
| 1621 | Mami | Nishi | mnishi#classicmodelcars.com |
| 1625 | Yoshimi | Kato | ykato#classicmodelcars.com |
| 1702 | Martin | Gerard | mgerard#classicmodelcars.com |
+----------------+-----------+-----------+---------------------------------+
23 rows in set (0.001 sec)

SQL query to find a partial string match that could include special characters

SQL query with special character ()
The original query (big thanks to GMB) can find any items in address (users table) that have a match in address (address_effect table).
The query works fine if address contains ',' but I can't seem to make it work if there is '()' in the address field.
Here is the sql query that's not working:
UPDATE users u
SET u.COUNT = (
SELECT COUNT(*) FROM address_effect a
WHERE FIND_IN_SET(a.address, REPLACE(u.address, ', ', ','')'))
)
Fyi, I'm testing this on my local system with XAMPP (using MariaDB).
I tried to identify '()' as an escape character by prepending it with backslash '' but it doesn't help.
user table
+--------+-------------+---------------+--------------------------+--------+
| ID | firstname | lastname | address | count |
| | | | | |
+--------------------------------------------------------------------------+
| 1 | john | doe |james street, idaho, usa | |
| | | | | |
+--------------------------------------------------------------------------+
| 2 | cindy | smith |rollingwood av,lyn, canada| |
| | | | | |
+--------------------------------------------------------------------------+
| 3 | rita | chatsworth |arajo ct, alameda, cali | |
| | | | | |
+--------------------------------------------------------------------------+
| 4 | randy | plies |smith spring, lima, (peru)| |
| | | | | |
+--------------------------------------------------------------------------+
| 5 | Matt | gwalio |park lane, (atlanta), usa | |
| | | | | |
+--------------------------------------------------------------------------+
address_effect table
+---------+----------------+
|address |effect |
+---------+----------------+
|idaho |potato, tater |
+--------------------------+
|canada |cold, tundra |
+--------------------------+
|fremont | crowded |
+--------------------------+
|peru |alpaca |
+--------------------------+
|atlanta |peach, cnn |
+--------------------------+
|usa |big, hard |
+--------+-----------------+
I would suggest using regular expressions for this. It seems more general than fiddling with the string:
update users u
set count = (select count(*)
from address_effect ae
where u.address regexp concat('[[:<:]]', ae.address, '[[:>:]]'))
);
The funky character class is MySQL's way of delineating a word boundary (I am more used to \W but MySQL doesn't support that).
Here is a db<>fiddle.
Just like you replace the space after each comma with just a comma, use REPLACE() to remove the chars '(' and ')':
FIND_IN_SET(a.address, REPLACE(REPLACE(REPLACE(u.address, ', ', ','), '(', ''), ')', ''))
See the demo.
Results:
| ID | firstname | lastname | address | count |
| --- | --------- | ---------- | -------------------------- | ----- |
| 1 | john | doe | james street, idaho, usa | 2 |
| 2 | cindy | smith | rollingwood av,lyn, canada | 1 |
| 3 | rita | chatsworth | arajo ct, alameda, cali | 0 |
| 4 | randy | plies | smith spring, lima, (peru) | 1 |
| 5 | Matt | gwalio | park lane, (atlanta), usa | 2 |

Compare very different tables

I have two Tables that are read from separate files (.xlsx and .csv) and are imported into MS Access. They are not in the same format
(which is why I'm having such a difficult time with it).
Here is xlsxTable:
+--------------------------------------------------------------------------------------+
| ID | Name | SSN | SSN2 | Address |
+--------------------------------------------------------------------------------------+
| 00012345 | Robert Robin | ThisIsSSN | ThisIsSSN2 | 12345 StreetName St. CityName, KS |
| 00013245 | Pete Peters | ThisIsSSN | ThisIsSSN2 | 54321 StreetName St. CityName, MO |
| 00012358 | Mike Michaels| ThisIsSSN | ThisIsSSN2 | 69874 StreetName St. CityName, NY |
| 00098755 | Tim Timpson | ThisIsSSN | ThisIsSSN2 | 15987 StreetName St. CityName, KY |
| 00035784 | Tom Thompson | ThisIsSSN | ThisIsSSN2 | 95123 StreetName St. CityName, CA |
| 00012584 | Will Willers | ThisIsSSN | ThisIsSSN2 | 35789 StreetName St. CityName, WA |
| ........ | ........... | ......... | .......... | ................................. |
Here is my csvTable:
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tracking_number | last_name | first_name | middle_name | suffix | alias_last_name | alias_first_name | alias_middle_name | alias_suffix | number | number_type | dob | street | city | state | zip | country | phone |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 135247 | Keeves | Michael | | Jr | | | | | ThisIsSSN | SSN/ITIN | 1/1/1990 | StreetName | CityName | NJ | | US | |
| 135248 | Jackson | Sue | Master | | | | | | ThisIsSSN | SSN/ITIN | 10/29/1980 | StreetName | CityName | NY | zip | US | |
| 135248 | Thomspon | Dolf | Laundry | | | | | | DriverNum | Driver'sLicense | 11/15/1962 | StreetName | CityName | KS | | US | |
| 135249 | Peters | Pete | | | Peters | Petey | | | ThisIsSSN | SSN/ITIN | 5/6/1975 | StreetName | CityName | PA | zip | US | |
| 135250 | Rogers | Steve | | | | | | | ThisIsSSN | SSN/ITIN | 12/25/1990 | StreetName | CityName | CT | zip | US | |
| 135250 | Nikolson | Jack | | Jr | | | | | DriverNum | Driver'sLicense | 8/5/1975 | StreetName | CityName | CA | zip | US | |
| 135251 | Keeves | Keanu | Neo | | | | | | ThisIsSSN | SSN/ITIN | 10/30/2000 | StreetName | CityName | TX | zip | US | |
| 135252 | Starch | Tony | | | | | | | ThisIsSSN | SSN/ITIN | 9/10/1975 | StreetName | CityName | NJ | | US | |
|...................|...............|................|...............|..........|...................|......................|........................|.................|.............|....................|............|.............|..............|.........|......|.........|.......|
| dba_name | number | number_type | incorporated | street | city | state | zip | country | phone | | | | | | | | |
| Mini Mart | 92585487 | EIN | | Street | CityName | state | zipNum | GT | | | | | | | | | |
| | 15987548 | EIN | | street | CityName | KS | zipNum | US | | | | | | | | | |
| Check Systems | 35854855 | EIN | | street | CityName | CA | zipNum | US | | | | | | | | | |
|...................|...............|................|...............|..........|...................|......................|........................|.................|.............|....................|............|.............|..............|.........|......|.........|.......|
Where dba_name is in the above table is an actual row. For some reason, there's another portion of the file that starts a new list.
I have to query these tables and if a name along with SSN match, then I must take the name, address and SSN, and do something with them (most likely put into another table for export). I have loaded both tables from the files necessary.
I'm now needing to iterate through and find the matches. For the sake of the sample data, Pete Peters should match here since the data is in both tables. My expected output should look a lot like the first table:
| ID | Name | SSN | SSN2 | Address |
I currently have an MS Access database that contains these tables. Though, with how the data is parsed, I'm not sure where to even start with the SQL. Performance-wise, this may be extensive. I'm just looking for a way to get it working first.
How can I query these two very different tables and only pull the data that matches?
Access has a find duplicates query wizard. The fastest way to handle the problem is to combine the tables manually or using 1 or more queries and then run the wizard. Again, get all your data into one table and then run the wizard. To make things complicated by breaking them down.
you might get the data from the CSV Table: with a query like:
SELECT csvTable.First_Name AS First_Name, csvTable.Last_Name AS Last_Name, csvTable.Number AS [Number]
FROM csvTable
GROUP BY csvTable.First_Name, csvTable.Last_Name, csvTable.Number
HAVING (((Count(csvTable.Number))>1));
then create a query with the same structure from the xlsx table:
SELECT Left([xlsxTable]![FullName],InStr([xlsxTable]![FullName]," ")) AS First_Name, Right([xlsxTable].[FullName],Len([xlsxTable].[FullName])-InStr([xlsxTable]![FullName]," ")) AS Last_Name, xlsxTable.SSN AS [Number]
FROM xlsxTable
GROUP BY Left([xlsxTable]![FullName],InStr([xlsxTable]![FullName]," ")), Right([xlsxTable].[FullName],Len([xlsxTable].[FullName])-InStr([xlsxTable]![FullName]," ")), xlsxTable.SSN
HAVING (((Count(xlsxTable.SSN))>1));
The having Count >1 does the work of finding the duplicates. Most of the rest of this is obtuse string manipulations to turn Full Name into first and last name directly in the sql. Then combine the queries so you can run them at the same time in the sql pane using a UNION ALL statement:
SELECT csvTable.First_Name AS First_Name, csvTable.Last_Name AS Last_Name, csvTable.Number AS [Number]
FROM csvTable
GROUP BY csvTable.First_Name, csvTable.Last_Name, csvTable.Number
UNION ALL
SELECT Left([xlsxTable]![FullName],InStr([xlsxTable]![FullName]," ")) AS First_Name, Right([xlsxTable].[FullName],Len([xlsxTable].[FullName])-InStr([xlsxTable]![FullName]," ")) AS Last_Name, xlsxTable.SSN AS [Number]
FROM xlsxTable
GROUP BY Left([xlsxTable]![FullName],InStr([xlsxTable]![FullName]," ")), Right([xlsxTable].[FullName],Len([xlsxTable].[FullName])-InStr([xlsxTable]![FullName]," ")), xlsxTable.SSN;
union all keeps duplicates while union omits them. I have removed the having statements from the union as I find it works better. next use the find duplicates wizard on your combined query like:
SELECT [combine tables].First_Name, [combine tables].Last_Name, [combine tables].Number
FROM [combine tables]
GROUP BY [combine tables].First_Name, [combine tables].Last_Name, [combine tables].Number
HAVING (((Count([combine tables].Number))>1));

Hive - collect_list with multiple columns?

Say my table looks like this:
Name,Subject,Score
Jon,English,80
Amy,Geography,70
Matt,English,90
Jon,Math,100
Jon,History,60
Amy,French,90
Is there a way to use collect_list so that I can get my query as such:
Jon: English:80; Math:100; History:60
Amy: Geography:70; French:90
Matt: English:90
EDIT:
The complication here is that the collect_list UDF permits only one argument, i.e. one column.
Something like
SELECT name, collect_list(subject), collect_list(score) from mytable group by name
results in
Jon | [English,Math,History] | [80,100,60]
Amy | [Geography,French] | [70,90]
Matt | [English] | [90]
Not sure if this is what you needed.
select * from t0;
+-------+------------+-------+--+
| t0.a | t0.b | t0.c |
+-------+------------+-------+--+
| Jon | English | 80 |
| Amy | Geography | 70 |
| Matt | English | 90 |
| Jon | Math | 100 |
| Jon | History | 60 |
| Amy | French | 90 |
+-------+------------+-------+--+
select a, collect_list(concat_ws(':',b,cast(c as string))) from t0 group by a;
+-------+-----------------------------------------+--+
| a | _c1 |
+-------+-----------------------------------------+--+
| Amy | ["Geography:70","French:90"] |
| Jon | ["English:80","Math:100","History:60"] |
| Matt | ["English:90"] |
+-------+-----------------------------------------+--+

how to use distinct in impala

HI I am trying to query the distinct localities in my table.
Here is my query.
select distinct city,locality, avg_sqft from real_estate.re_search where city = 'bangalore' AND locality != 'jayanagar';
Result
+-----------+--------------+----------+
| city | locality | avg_sqft |
+-----------+--------------+----------+
| bangalore | bannerghatta | 13500 |
| bangalore | kormangala | 18000 |
| bangalore | kodipur | 7000 |
| bangalore | kormangala | 16000 |
| bangalore | horamavu | 9000 |
| bangalore | bellandur | 15500 |
| bangalore | kodipur | 9000 |
| bangalore | madivala | 12000 |
| bangalore | varthur | 12000 |
| bangalore | kormangala | 13500 |
| bangalore | bellandur | 13000 |
| bangalore | kodipur | 11500 |
| bangalore | kormangala | 14000 |
the problem is I need to display the distinct locality in result.any help will be appreciated.
You should be able to get a list of distinct members of the locality column in your table, where the city is Bangalore by using the COUNT and GROUP BY operators:
SELECT city
,locality
,COUNT(locality)
FROM database.table
WHERE city = 'Bangalore'
GROUP BY city
,locality;