SQL query, check year on one column - mysql

I have a column in my MySQL table showing year.
Example:
Table: Information
ID | Year_sold
--- --------
1 | 2002-2010
2 | 2005-2015
3 | 2011-____
4 | 1975-1978
I will ask the table if it has data to show for a specific year.
Example:
SELECT * FROM Information WHERE Year_sold = '2012';
This will of course not work, but how can I type the SQL query if the result should be ID 2 and 3.
If the item is still active and being sold, the years will be shown like ID 3, "2011-____". I can replace the "____" if needed.

Use the BETWEEN, LEFT and SUBSTRING_INDEX functions.
SELECT ID, Year_sold
FROM Information
WHERE '2012' BETWEEN LEFT(Year_sold,4) AND SUBSTRING_INDEX(Year_sold,'-',-1)
Output
ID Year_sold
2 2005-2015
3 2011-____
SQL Fiddle: http://sqlfiddle.com/#!9/7df7b7/1/0
If possible I would start again with your table structure and have something like:
Table: Information
ID | Year_sold_from | Year_sold_to
--- ------------------------------
1 | 2002 | 2010
2 | 2005 | 2015
3 | 2011 | null
4 | 1975 | 1978

Related

Calculate the fullness of an apartment using SQL expression

I have a database which looks like this:
Reservations Table:
-------------------------------------------------
id | room_id | start | end |
1 | 1 | 2015-05-13 | 2015-05-16 |
2 | 1 | 2015-05-18 | 2015-05-20 |
3 | 1 | 2015-05-21 | 2015-05-24 |
-------------------------------------------------
Apartment Table:
---------------------------------------
id | room_id | name |
1 | 1 | test apartment |
---------------------------------------
Meaning that in the month 05 (May) there is 31 days in the database we have 3 events giving us 8 days of usage 31 - 8 = 23 / 31 = 0.741 * 100 = %74.1 is the percentage of the emptiness and %25.9 is the percentage of usage. how can i do all of that in SQL? (mySQL).
This is my proposal:
SELECT SUM(DAY(`end`)-DAY(`start`))/EXTRACT(DAY FROM LAST_DAY(`start`)) FROM `apt`;
LAST_DAY function gives as output the date of last day of the month.
Check this
http://sqlfiddle.com/#!9/7c53b/2/0
Not the most efficient query but will get the job done.
select
sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1)
as usePercent,
100-(sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1))
as emptyPercent
FROM
(select DATEDIFF(end,start) as days from test1) a
What I did is first get the date difference and count them. Then in a nested query use the day(last_day()) function to get the last day of month. Then calculated by using your logic.

Access 2007 Query to return latest date note from subquery

I have 2 tables joined by ProjectID in Access 2007. Table structure of primary table (A) is like
ProjectID | CustID
1 | 5
2 | 8
I have a secondary table (B) of notes on the ProjectID, structured like
ProjectNotesID | ProjectID | Note | CreateDate
---------------------------------------------------
1 | 1 | Note11 | 1/2/2015
2 | 1 | Note12 | 2/2/2015
3 | 2 | Note21 | 4/8/2015
4 | 2 | Note22 | 3/5/2015
I want to return all of, or part of, Table A, with the latest note of Table B, something like
ProjectID | CustID | Note | CreateDate
------------------------------------------
1 | 5 | Note12 | 2/2/2015
2 | 8 | Note21 | 4/8/2015
I can do (and have done) this with PHP & MySQL, but can't get it to work in Access 2007. I can return the ProjectID and latest Note date by the following query in Access 2007
SELECT ProjectID, Max(CreateDate) AS MaxOfCreateDate
FROM Table B
GROUP BY ProjectID;
I have tried Unique Values, etc., but can't get the results I am looking for.
Thanks!
Pete
I found an answer. First, I changed the query where I got the latest date, and instead got the max ProjectNotesID. The query is ::
SELECT ProjectID, Max(ProjectNotesID) AS MaxOfProjectNotesID
FROM Table B
GROUP BY ProjectID;
I then created a second query combining the above query and Table B, joining Query.MaxOfProjectNotesID <-> TableB.ProjectNotesID, and Query.ProjectID <-> TableB.ProjectID. The second query pulls all values from TableB.

Access table design

I have the following requirement for an Access table and I'm having trouble figuring out the best way to design the table. I'm not sure if what I have is the best practice in this case.
table fields are name, Status, Date
each name will have minimum three status, and they change every month and then stop at Open. the date for each status change has to be captures in the database.
Example :
Name | Status | Date
--------+-----------+------------------
name1 | N/A | April
name2 | N/A | April
name1 | Open | May
name2 | N/A | May
name1 | closed | June
name1 | open | July
From the normalized point of view you need 4 tables total
1st table the master table
Name | Status | Date (month ?)
--------+-----------+--------
1 | 1 | 4
2 | 3 | 5
2nd table the names table
ID | Names
--------+-----------
1 | name1
2 | name2
3rd table status table
ID | Status
--------+-----------
1 | N/A
2 | Open
3 | closed
4th table Months
ID | Month
--------+-----------
1 | January
2 | February
........|............
12 | December
Honestly I have thought about this particular setup a number of times myself. I inherited a database that uses the format that you have posted.
While I don't think that setup is bad, another idea that I had was to create 3 checkbox fields that belong to each status to show progress. Then there would be 3 date fields, each one pertaining to when the checkbox was checked off.
That would cut the number of records down by 2/3rds but again. I think it is more of a matter of opinion.

Self join and recursive selection in a table

Assuming a table as below
| ID | NAME | ROLE | MGRID |
---------------------------
| 1 | ONE | 5 | 5 |
| 2 | TWO | 5 | 5 |
| 3 | THREE | 5 | 6 |
| 4 | FOUR | 5 | 6 |
| 5 | FIVE | 15 | 7 |
| 6 | SIX | 25 | 8 |
| 7 | SEVEN | 25 | 7 |
| 8 | EIGHT | 5 | 8 |
How do I get a list of all employees reporting to an employee, including the ones who are in subsequent reporting levels below?
I mean, given emp id 5, I should get [1, 2] and given 7, I should get [1, 2, 5, 7]. How do I get this done?
Will self joins be of help here? Need to brush up my knowledge on joins now.
SELECT id
FROM emp
START WITH id = 7
CONNECT BY NOCYCLE mgrid = PRIOR id
SQLFIDDLE LINK
Here is a SQL statement using Oracle.
select id, name, role, mgrID
from employees
start with id = 7
connect by NoCycle prior id = mgrid;
Please note that the manager for employee 7 is the employee 7 - they are their own manager. This will cause an error - "Connect By loop in user data'. By using the NoCycle keyword you can tell Oracle to detect this and avoid the error.
Does this solve your issue?
I know this isn't exactly what you were asking, but if you are willing to choose a finite number of level's to recurse it isn't too bad to write.
SELECT table_2.id
FROM table LEFT JOIN
(table AS table_1 LEFT JOIN table AS table_2 ON table_1.id = table_2.MgrID)
ON table.id = table_1.MgrID
WHERE (((table.id)=7));
ETC.

MYSQL JOIN two tables

We just want to make the query for mysql database, in which there are 12 table according to the months(JAN - DEC), with 32 Columns(JAN1, JAN2, JAN3,....JAN31). These database is used for getting the availability for hotel,like if we select a tour for three days (29JAN-1JAN), so the query will check the records for 2 tables, one for JAN and other for FEB. the whole columns stored the values in digit(like, 5,10,2,0,5,etc)its showing Rooms available. We are successfully built a query for single month, but we unable to create a mysql query for 2 months, because we want a value in greater than 1. like we only shows the available rooms only.
$num = mysql_query("SELECT DISTINCT id,table_type,JAN29,room_type FROM JAN Where table_type='disp' AND JAN!=0 ");
above query is working fine for me, we just want this query for 2 tables. and getting the positive value , greater than 0(1).
Please help to solve this problem ..
Thanks
Rod
ID | JAN1 | JAN2 | JAN3 | JAN31|
34 | 5 | 3 | 3 | 4 |
56 | 4 | 3 | 9 | 3 |
28 | 0 | 7 | 0 | 9 |