retrive the the data which modified in specific column along with secong max updated date in sybase sql - sybase-ase

I have the data
column name status1 status2 status3 update_date user
Row1 A 1 2 3 06/05/2017 A
Row2 A 1 3 3 05/05/2017 C
Row3 A 5 2 3 03/05/2017 A
Row4 A 2 3 3 02/05/2017 B
Row5 A 2 2 1 01/05/2017 A
THE EXPECTED RESULT IS:
i want to retrive the data modified in status1(specific) and its last modified date along with before modification which data it had on specific date(04/05/2014)
Ex:
Row3 A 5 2 3 03/05/2017 A
Row4 A 2 3 3 02/05/2017 B
as you can see in row 3 the status1 lastly updated on 03/05/2017 by the user A (I want this data)
as well as in row 4 before the update it had a data of row 4
could you help me on this?

Related

BigQuery: Flattening all repeated fields in nested schema

I am having so much trouble with querying from Big Query's nested schema.
I have the following fields.
I want to flatten the table and get something like this.
user | question_id | user_choices
123 | 1 | 1
123 | 1 | 2
123 | 1 | 3
123 | 1 | 4
From other resources, I got to a point where I can query from one of the records in the repeated columns. Such as the following:
SELECT user, dat.question_id FROM tablename, UNNEST(data) dat
It gives me this result.
But when I do this, I get another repeated columns again.
SELECT user, dat.question_id, dat.user_choices FROM tablename, UNNEST(data) dat
Can anyone help me how to UNNEST this table properly so I can have flattened schema for all data items?
Thanks!
Below is for BigQuery Standard SQL
#standardSQL
SELECT user, question_id, choice
FROM `project.dataset.table`,
UNNEST(data) question,
UNNEST(user_choices) choice
You can test, play with above using dummy data from your question like below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 user,
[STRUCT<question_id INT64, user_choices ARRAY<INT64>>
(1,[1,2,3]),
(2,[2,5]),
(3,[1,3])
] data UNION ALL
SELECT 2 user,
[STRUCT<question_id INT64, user_choices ARRAY<INT64>>
(1,[2,3]),
(2,[4,5]),
(3,[2,6])
] data
)
SELECT user, question_id, choice
FROM `project.dataset.table`,
UNNEST(data) question,
UNNEST(user_choices) choice
ORDER BY user, question_id, choice
with result
Row user question_id choice
1 1 1 1
2 1 1 2
3 1 1 3
4 1 2 2
5 1 2 5
6 1 3 1
7 1 3 3
8 2 1 2
9 2 1 3
10 2 2 4
11 2 2 5
12 2 3 2
13 2 3 6

How to multiple column join in one column in excel (I Want Formula)

HOW TO JOIN MULTIPLE COLUMN IN ONE COLUMN
TABLE 1 TABLE 2 TABLE 3
1 2 5
2 4 3
3 5 3
4 5 1
I WANT TO
1
2
3
4
2
4
5
5
5
3
3
1
If your data is like below, enter the formula in the first row of any column and drag down until there is no value left over,
=IF(ROW()<=COUNTA(A:A),INDEX(A:A,ROW()),IF(ROW()<=COUNTA(A:B),INDEX(B:B,ROW()-COUNTA(A:A)),IF(ROW()>COUNTA(A:C),"",INDEX(C:C,ROW()-COUNTA(A:B)))))

mysql query to add data with column the same

I have a mysql table where i may put multiple entries for the same date.
Does anyone know a query which will go through the data and add values up if they land on the same date?
for e.g. 2015-10-01 will have one line with Value1 having the answer of 3
id date Value1 Value2 Value3
1 2015-10-01 1 1 1
2 2015-10-01 2 2 2
3 2015-10-02 1 1 1
4 2015-10-02 2 2 2
5 2015-10-03 1 1 1
6 2015-10-03 2 2 2
7 2015-10-03 3 3 3
You need group by and sum():
select date, sum(value1) as total from table group by date

Count sequential field values based on date in seperate table using SQL

I have been trying to devise an SQL query in Access 2010 to count the number of sequential field values which are based over 3 tables using fields with unique ID's
Example
Table1: Course
CorID Date
1 01/01/2012
2 01/03/2012
3 01/02/2012
Table 2: Delegate
DelID StaffID CorID Value CounterField
1 17263 2 99 1
2 17263 1 99 2
3 17263 3 99 3
4 17263 65 4 1
5 17263 44 5 1
6 17263 78 5 2
Table 3: Staff
StaffID Surname
1 Test
2 Smith
17263 Jones
The CounterField increases by 1 where the Value field in Table 2 is the same as the previous Value field. There would be a requirement to ensure that the count only uses the the order based on the course table Date field. The delegate table would also contain more DelID fields than listed and will contain different StaffID values as well.
The CounterField in the above table is an example of what I want the query to be able to do.
Is this possible? Thanks in advance.
If you want to count how many rows you have in Delegate that are not duplicate regarding the Value field:
SELECT count(*)
FROM Delegate
WHERE CounterField = 1
Try running a subquery:
SELECT a.DelID, a.StaffID, a.CorID, a.[Value],
(SELECT Count(*) FROM Delegate b
WHERE b.DelID <= a.DelID AND a.[Value]=b.[Value]) As CounterField
FROM Delegate a;

How to avoid duplicate data into Mysql table?

order-:
order_id(PK) item status order_no
1 pant 0 100
2 shirt 1 200
product-:
prod_id(Fk) id(pk) price quantity order_no status
1 1 10 2 100 0
1 2 20 3 100 0
2 3 15 1 200 1
These are my two table-: order & product.
what happend is to insert data into product table, my query first check status of order table & if it is 0, it add data into product table.
My program runs through CRON job..so each time it just checks order status & if it is 0,
it again adds same data into product table.
i.e. my product table become like this
prod_id(Fk) id(pk) price quantity order_no status
1 1 10 2 100 0
1 2 20 3 100 0
2 3 15 1 200 1
1 4 10 2 100 0
1 5 20 3 100 0
which I don't want. my id column is pk which is auto-incremented...
so how should I avoid duplicate data into Product table?
Is there any way we can update or replace the same data??
plz guide me..
Create a UNIQUE index on prod_id (or whatever columns determine that the record is to be updated rather than inserted), and then use either INSERT ... ON DUPLICATE KEY UPDATE or REPLACE.

Resources