I'm new to access and coding so please bear with me.
I want my [Exp] dates that fall between 1/12/2020 and 1/7/2021 to be shown/displayed in [Exp2] record and if it doesn't fall between the two mentioned dates then show/display nothing in Exp2 record.
Example: |
| Exp | Exp2 |
| 1/5/2020 | |
| 3/8/2020 | |
| 12/13/2020| 12.13.2020 |
see in Exp2 only 3rd record is shown/displayed while the first and second are empty.
enter image description here
enter image description here
Use a query like this:
Select
[Exp],
IIf([Exp] Between #2020-12-01# And #2021-07-01#, [Exp], Null) As Exp2
From
YourTable
or use a subquery (slower):
Select
[Exp],
(Select
First(T.Exp)
From
YourTable As T
Where
T.Exp = YourTable.Exp
And
T.Exp Between #2020-12-01# And #2021-07-01#) As Exp2
From
YourTable
Related
Want a null row where no data exist
Hello. This is in regards to MySQL Workbench 6.3.
I'm trying to return a list of results for every item listed in my select statement, which would include those items that don't actually exist. So if i list 5 items in my select statement and only 3 exist, i'd like to get 5 rows returned, 3 actual rows with data and 2 more rows that only show null. Can someone please show me how to edit my query below to show this ? Thank you !
select emails from table where email in (dog, frog, cat, tiger, lizard);
Actual Result (only the 3 emails that actual exist show)
dog
cat
tiger
Desired Result
dog
null
cat
tiger
null
The desired results are not possible.. You can't expect MySQL to
return the selected records in the order they went in the IN()
operator.
So i think you better off when you change the desired result to something you know sometiming was not found in the table, i think you are looking for.
Query
SELECT
search_emails.email
, (
CASE
WHEN t.email IS NOT NULL
THEN 'true' ELSE 'false'
END
) AS found
FROM (
SELECT 'dog' AS email
UNION
SELECT 'frog' AS email
UNION
SELECT 'cat' AS email
UNION
SELECT 'tiger' AS email
UNION
SELECT 'lizard' AS email
) AS search_emails
LEFT JOIN
t
ON
t.email = search_emails.email
Result
| email | found |
| ------ | ----- |
| dog | true |
| cat | true |
| tiger | true |
| frog | false |
| lizard | false |
see demo
select emails from table where email in (dog, frog, cat, tiger, lizard) OR email IS NULL
be sure that the values provided for IN (...) are provided as strings.
I need help in merging rowns into column in sql:
Table:
|TopologyType | TopologyName | Name |
| 1 | oneway | client1 |
| 1 | oneway | client2 |
| 2 | proxy | client1 |
| 2 | proxy | proxy1 |
| 2 | proxy | client2 |
Output:
| 1 | oneway | client1 | NULL | client2 |
| 2 | proxy | client1 | proxy | client2 |
In case you don't need all of the output fields to be in columns you could use GROUP_CONCAT
Example query
SELECT `t`.`TopologyType`,`t`.`TopologyName`,GROUP_CONCAT(DISTINCT(`tt`.`Name`)) as `name` from `test` `t`
left join `test` `tt`
ON `t`.`TopologyType`=`tt`.`TopologyType`
GROUP BY `t`.`TopologyName`;
Output
TopologyType TopologyName name
1 oneway client1,client2
2 proxy client1,proxy1,client2
view this example in SQL fiddle
Oof, you're a bit ropey on detail so don't expect this answer to work without some adjustment. Let me present you some sample data:
Name, Class, Score
john, math, 70
james, math, 75
john, english, 48
james, english, 69
Desired output:
name, mathscore, englishscore
john, 70, 48
james, 75, 69
Pivoting query for mysql (doesn't support PIVOT command)
SELECT
name,
MAX(CASE WHEN class = 'math' THEN score END) as mathscore
MAX(CASE WHEN class = 'english' THEN score END) as englishscore
FROM
grades
GROUP BY
name
If you want to know more about how it works, then run it without the GROUP BY and without the MAX functions:
SELECT
name,
CASE WHEN class = 'math' THEN score END as mathscore
CASE WHEN class = 'english' THEN score END as englishscore
FROM
grades
And then bear in mind that MAX() will not consider nulls when doing its work, so it causes the rows to collapse to only those containing values
As an aside - see how i've phrased my answer in terms of giving a sample data and a desired output? Do please write your next SQL question more like that, including the SQL you tried
Edit:
OK, so just apply the pattern in my SQL, to your names. For me, the "name" was the fixed column, the "class" was the one being pivoted out into multiple columns. The general pattern is:
SELECT
fixedColumn1,
fixedColumn2
..
fixedColumnN,
MAX(CASE WHEN column_with_values_that_must_be_headers = a1_value_you_want_in_its_own_column THEN column_with_value_to_appear_as_data1) as column_header_name1,
MAX(CASE WHEN column_with_values_that_must_be_headers = a2_value_you_want_in_its_own_column THEN column_with_value_to_appear_as_data2) as column_header_name2,
...
MAX(CASE WHEN column_with_values_that_must_be_headers = aN_value_you_want_in_its_own_column THEN column_with_value_to_appear_as_dataN) as column_header_nameN
FROM
table
GROUP BY
fixedColumn1,
fixedColumn2
..
fixedColumnN,
Here's a partial; have a go at finishing it off - you'll learn way more about this technique put doing a bit of it, than just hanging around waiting for someone to give you the answer to paste in :)
SELECT
TopologyType,
TopologyName,
MAX(CASE WHEN name = 'client1' THEN name END) AS client1,
... --put more CASE WHENs here
FROM
table
GROUP BY
TopologyType,
TopologyName
The only slight difference between your query and my earlier examples is that I had one column that I wanted to be the header (the name of the class; english / maths) but another column to be the data (the test score of the person in that class) whereas you effectively want the name as the column header AND the data. It's ok though - just follow the pattern above, using the name column as the header and the data:
MAX(CASE WHEN name = 'client1' THEN name END) AS client1,
^^^^^^^^^^^^^^^^ ^^^^
column_header cell_data
I am trying to figure out best way to get the aggregate of a person's hours spent on a project name that follows a certain pattern
Current Tables
+--------------------+----------------+-----------------+
| Tbl_Employee | Tbl Projects | tbl_timesheet |
+--------------------+----------------+-----------------+
| employee_id | project_id | timesheet_id |
| employee_full_name | cws_project_id | employee_id |
| | | project_id |
| | | timesheet_hours |
+--------------------+----------------+-----------------+
Here is the query I have so far
select
te.employee_id,
te.employee_last_name,
te.employee_first_name,
te.employee_department,
te.employee_type_id,
te.timesheet_routing,
sum(tt.timesheet_hours) as total_hours,
month(tt.timesheet_date) as "month",
year(tt.timesheet_date) as "year"
from tbl_employee te
left join tbl_timesheet tt
on te.employee_id = tt.employee_id
join tbl_projects tp
on tp.project_id = tt.project_id
where te.employee_active = 1
and te.employee_id > 0
and employee_department IN ("Project Management","Engineering","Deployment Srvs.")
and year(tt.timesheet_date) = 2015
group by te.employee_last_name, year(tt.timesheet_date), month(tt.timesheet_date)
order by employee_last_name
What I need to add to my select statement is something to the effect of
sum(tt.timesheet_hours) as where cws_project_id like '%Training%' as training
In short I need to know the sum of hours an employee has contributed to a project where the cws_project_id contains the word Training. I know you cant add a where clause to a Sum but I cant seem to find another way to do it.
If this makes a difference I need to do this several times - ie where the project_name contains a different word.
Thank you so much for any help that can be provided. I hope that is not clear as mud.
Here is the general form of what you are looking for:
SELECT SUM(IF(x LIKE '%y%', z, 0)) AS ySum
even more general
SELECT SUM(IF([condition on row], [value or calculation from row], 0)) AS [partialSum]
Edit: For more RDBMS portability (earlier versions of MS SQL do not support this form of IF):
SELECT SUM(CASE WHEN [condition on row] THEN [value or calculation from row] ELSE 0 END) AS [partialSum]
I've a database called test and i've tables called x,y,z.
How do i select x,y,z and there is a column called date IN X,Y,Z check whether there is a particular date.
Is there any build in function that does this?
update
SELECT column date from all tables which is in a database called test
Thanks in advance!!
As far as I know, in SQL you cannot 'select a table', you can select some
column(s) from one or many tables at once. The result of such a query is an another table (temporary table) that you retrieve the data from.
Please be more specific about what exactly you want to do (e.g.: "I want to select a column 'z' from table 'tableA' and column 'y' from table 'tableB'") - then I'm sure your question has a pretty simple answer :)
SELECT x.date AS x_date, y.date AS y_date, z.date AS z_date FROM x,y,z;
That produces a result:
+---------+---------+---------+
| x_date | y_date | z_date |
+---------+---------+---------+
| | | |
| | | |
+---------+---------+---------+
Alternatively you can get everything in one column by ussuing a query:
SELECT date FROM x
UNION ALL
SELECT date FROM y
UNION ALL
SELECT date FROM z;
That produces a result:
+-------+
| date |
+-------+
| |
| |
+-------+
In the example above you would get also duplicate values in the single column. If you want to avoid duplicates replace 'UNION ALL' with 'UNION'
I'm still not sure if I undestood what you really want ot achieve, but I still hope that helps
Also take a look at:
http://www.w3schools.com/sql/sql_union.asp
http://www.sql-tutorial.net/SQL-JOIN.asp
I have an sql query which shows the delivery details of a vehicle. ( it uses greatest to fetch max value from a range of colums for each vehicle stop)
SELECT deliveryid AS deliverynumber, loadid1 AS loadnumberdate,
haulieraccepted AS haulier,
greatest(drop1arrivedatetime, drop2arrivedatetime, drop3arrivedatetime,
drop4arrivedatetime, drop5arrivedatetime) AS planneddate,
date(greatest(ActualDrop1Arrive, ActualDrop2Arrive, ActualDrop3Arrive,
ActualDrop4Arrive, ActualDrop5Arrive )) AS actualenddate,
mitigation
FROM deliverydetails
WHERE deliveryid=44
the output is
deliverynumber | loadnumberdate | haulier | planneddate | actualenddate | mitigation
44 | 484487 | stols transport | 2011-11-26 15:50:00 | 2011-11-26 | customerdelay
How can I add to the mysql query to compare columns 'planneddate' and 'actualenddate'? if the dates are the same then set the query field to 'ontime' else if actualenddate>planneddate then 'deliverylate'. So ideally I want the following output:
deliverynumber | loadnumberdate | haulier | planneddate | actualenddate | mitigation | Status
44 | 484487 | stols transport | 2011-11-26 15:50:00 | 2011-11-26 | customerdelay | ontime.
Thanks for the assistance.
You can use a CASE statement or IF function. Perhaps something like:
SELECT ...., IF(actualenddate>planneddate,'deliverylate','ontime') AS status FROM ....
use mysql if condition and date conversion function to check and display according to....
You can wrap your original query as a subquery. This will rename the columns. Then, use a case ... then clause to add the column.
Assuming your original query works just fine, it would look like this:
select
*,
case when (... some comparison on 'planneddate' and 'actualenddate' ...)
then <true output>
else <false output> end
from
(<your original query>) as myalias;
The trick is that the columns from the subquery are renamed, allowing you to use their new names (planneddate and actualenddate).