update Query for a table in sql server 2005 - sql-server-2005

i have an table called employees
empID name startdate Middate DEptID
1 kumar 2011-04-30 2005-09-13 1
2 kiran 2011-05-23 2006-08-13 1
3 kishor 2008-04-13 2009-04-23 6
4 anu 2009-09-03 2000-06-13 1
5 priya 2010-05-04 2010-04-13 6
6 sweth 2011-07-13 2007-12-16 1
7 manu 2011-08-15 2006-12-07 6
Is there any way i can update this table so that
startdate = Middate for all the employees
empID name startdate Middate DEptID
1 kumar 2005-09-13 2005-09-13 1
2 kiran 2006-08-13 2006-08-13 1
3 kishor 2009-04-23 2009-04-23 6
4 anu 2000-06-13 2000-06-13 1
5 priya 2010-04-13 2010-04-13 6
6 sweth 2007-12-16 2007-12-16 1
7 manu 2006-12-07 2006-12-07 6
pls let me know how can i solve this
thanks
prince

update employees set startdate=middate

Related

MS-Access - Merge data between two tables base on conditions

I am using MS Access and I am trying to create a query between two tables and merge same rows base on:
cust_id = cust_id and
a_date = f_date and
price = paid
and have the desire output.
My data now:
tblapp
app_id cust_id a_date price a_memo
------------------------------------------
1 1 10/10/20 20 hello
2 1 11/10/20 10 bye
3 2 12/10/20 30 hi
4 2 12/10/20 30 text
5 2 12/10/20 30 lol
6 2 12/10/20 30 ciao
7 3 14/10/20 25 peace
tblfin
fin_id cust_id f_date paid
----------------------------------
1 1 10/10/20 20
2 1 11/10/20 10
3 1 11/10/20 10
4 2 12/10/20 30
5 3 14/10/20 25
As you can see,
cust_id = 1 on 10/10/20 with bill 20 and paid 20
cust_id = 1 on 11/10/20 with bill 10 and paid 10 + 10
cust_id = 2 on 12/10/20 with bill 30 + 30 + 30 + 30 and paid 30
cust_id = 3 on 14/10/20 with bill 25 and paid 25
Derire query output:
app_id cust_id a_date price a_memo fin_id cust_id f_date paid
----------------------------------------------------------------------
1 1 10/10/20 20 hello 1 1 10/10/20 20
2 1 11/10/20 10 bye 2 1 11/10/20 10
3 2 12/10/20 30 hi 4 2 12/10/20 30
7 3 14/10/20 25 peace 5 3 14/10/20 25
Tried the following sql but i am getting duplicates(like cust_id 1 and 2 where data rows are not the same in two tables):
SELECT f.fin_id,
f.cust_id,
f.f_date,
f.paid,
a.app_id,
a.cust_id,
a.a_date,
a.price,
a.a_memo
FROM tblfin AS f
LEFT JOIN tblapp AS a ON (f.cust_id=a.cust_id)
AND (f.f_date=a.a_date)
AND (f.paid=a.price);
Solution using MySQL is welcome. Thank you.

How to number the occurrences of a particular value in multiple cells

I have a table that represents the purchases of a list of customers by date. The data is sorted in order by customer, and purchase date.
I need to place the total number of orders a particular customer has made in a third column (probably by checking the number of previous instances of the customer's name).
My table currently looks like this:
Column A Column B Column C
1 12/03/13 Angela
2 01/05/14 Angela
3 03/07/14 Angela
4 04/01/14 Angela
5 03/06/13 Ben
6 04/02/13 Ben
7 11/11/15 Carl
8 12/11/15 Carl
9 01/01/16 Carl
10 02/03/17 David
11 04/04/17 Ethan
And what I need to see is (where Column C is the Total Orders for that customer)
Column A Column B Column C
1 12/03/13 Angela 1
2 01/05/14 Angela 2
3 03/07/14 Angela 3
4 04/01/14 Angela 4
5 03/06/13 Ben 1
6 04/02/13 Ben 2
7 11/11/15 Carl 1
8 12/11/15 Carl 2
9 01/01/16 Carl 3
10 02/03/17 David 1
11 04/04/17 Ethan 1
Any help is greatly appreciated!
Try the following in C2
=COUNTIF($B$2:$B2,$B2)
Drag down for as many rows as required.

Sqlite query for fetching latest Exam date time with distinct patientID

In Sqlite db I have a table: Examination with columns ExamID, InternalPID, ExamDateTime
ExamID InternalPID ExamDateTime (from left to right)
1 2 2015-03-11
2 1 2015-11-11
3 4 2015-05-01
4 6 2015-08-10
5 2 2015-04-22
6 1 2014-12-11
7 2 2015-03-12
the query output should be latest Examination date of each patient. i.e the InternalPID should be distinct with its latest ExamDateTime.
Expect output from query:
ExamID InternalPID ExamDateTime
5 2 2015-04-22
2 1 2015-11-11
3 4 2015-05-01
4 6 2015-08-10
Thank you in advance
You can do this using a join and aggregation or a clever where clause:
select e.*
from examination e
where e.ExamDateTime = (select max(e2.ExamDateTime)
from examination e2
where e2.patientid = e.patientid
);

SQL : How to find number of occurrences without using HAVING or COUNT?

This is a trivial example, but I am trying to understand how to think creatively using SQL.
For example, I have the following tables below, and I want to query the names of folks who have three or more questions. How can I do this without using HAVING or COUNT? I wonder if this is possible using JOINS or something similar?
FOLKS
folkID name
---------- --------------
01 Bill
02 Joe
03 Amy
04 Mike
05 Chris
06 Elizabeth
07 James
08 Ashley
QUESTION
folkID questionRating questionDate
---------- ---------- ----------
01 2 2011-01-22
01 4 2011-01-27
02 4
03 2 2011-01-20
03 4 2011-01-12
03 2 2011-01-30
04 3 2011-01-09
05 3 2011-01-27
05 2 2011-01-22
05 4
06 3 2011-01-15
06 5 2011-01-19
07 5 2011-01-20
08 3 2011-01-02
Using SUM or CASE seems to be cheating to me!
I'm not sure if it's possible in your current formulation, but if you add a primary key to the question table (questionid) then the following seems to work:
SELECT DISTINCT Folks.folkid, Folks.name
FROM ((Folks
INNER JOIN Question AS Question_1 ON Folks.folkid = Question_1.folkid)
INNER JOIN Question AS Question_2 ON Folks.folkid = Question_2.folkid)
INNER JOIN Question AS Question_3 ON Folks.folkid = Question_3.folkid
WHERE (((Question_1.questionid) <> [Question_2].[questionid] And
(Question_1.questionid) <> [Question_3].[questionid]) AND
(Question_2.questionid) <> [Question_3].[questionid]);
Sorry, this is in MS Access SQL, but it should translate to any flavour of SQL.
Returns:
folkid name
3 Amy
5 Chris
Update: Just to explain why this works. Each join will return all the question ids asked by that person. The where clauses then leaves only unique rows of question ids. If there are less than three questions asked then there will be no unique rows.
For example, Bill:
folkid name Question_3.questionid Question_1.questionid Question_2.questionid
1 Bill 1 1 1
1 Bill 1 1 2
1 Bill 1 2 1
1 Bill 1 2 2
1 Bill 2 1 1
1 Bill 2 1 2
1 Bill 2 2 1
1 Bill 2 2 2
There are no rows where all the ids are different.
however for Amy:
folkid name Question_3.questionid Question_1.questionid Question_2.questionid
3 Amy 4 4 5
3 Amy 4 4 4
3 Amy 4 4 6
3 Amy 4 5 4
3 Amy 4 5 5
3 Amy 4 5 6
3 Amy 4 6 4
3 Amy 4 6 5
3 Amy 4 6 6
3 Amy 5 4 4
3 Amy 5 4 5
3 Amy 5 4 6
3 Amy 5 5 4
3 Amy 5 5 5
3 Amy 5 5 6
3 Amy 5 6 4
3 Amy 5 6 5
3 Amy 5 6 6
3 Amy 6 4 4
3 Amy 6 4 5
3 Amy 6 4 6
3 Amy 6 5 4
3 Amy 6 5 5
3 Amy 6 5 6
3 Amy 6 6 4
3 Amy 6 6 5
3 Amy 6 6 6
There are several rows which have different ids and hence these get returned by the above query.
you can try sum , to replace count.
SELECT SUM(CASE WHEN Field_name >=3 THEN field_name ELSE 0 END)
FROM tabel_name
SELECT f.*
FROM (
SELECT DISTINCT
COUNT(*) OVER (PARTITION BY folkID) AS [Count] --count questions for folks
,a.folkID
FROM QUESTION AS q
) AS p
INNER JOIN FOLKS as f ON f.folkID = q.folkID
WHERE p.[Count] > 3

How to create an internal numbering of occurrences with SQL

How can I create a new column (inCount) with numbering of occurrences in a specific column?
Here is an example:
id name inCount
1 Orly 1
2 Ernest 1
3 Rachel 1
4 Don 1
5 Don 2
6 Ernest 2
7 Angela 1
8 Ernest 3
9 David 1
10 Rachel 2
11 Sully 1
12 Sully 2
13 Rachel 3
14 David 2
15 David 3
16 Kevin 1
17 Kevin 2
18 Orly 2
19 Angela 2
20 Sully 3
21 Kevin 3
22 Don 3
23 Orly 3
24 Angela 3
Don from id 5 is numbered 2 because Don appears in id 4 too.
Don from id 22 is numbered 3 due to the above preceding occurrences.
I use MS SQL SERVER 2008 R2 Express edition.
Thanks.
You could use partition by, like:
select row_number() over (partition by name order by id) as inCount
, *
from YourTable
order by
id
This should work
SELECT id, Name, ROW_NUMBER() OVER(PARTITION BY Name ORDER BY id)
FROM table
ORDER BY id
EDIT: Added order by clause on the select in order to show results in same order indicated by OP. The ORDER BY in the ROW_NUMBER did not change the outcome, but I changed to id as it will keep the row_number correct for the sample data.