[![enter image description here][1]][1]good day to you all.
I have a table with some field like attached file T_Jadwal2.jpg. https://i.stack.imgur.com/mPuBg.jpg
I need the the table insert to other table with the difference way format like T_Jadwal3.jpg https://i.stack.imgur.com/hRg7y.jpg
Kindly to have the solution in query to append the T_Jadwal2 to T_Jadwal3
Thank you.
Regards,
Fernando
Create a union query like this:
Select ID_Jadwal, [Shift], Supervisor As Nm_Karyawan, "Supervisor" As Posisi
From YourTable
Where Supervisor Is Not Null
Union All
Select ID_Jadwal, [Shift], Printing, "Printing"
From YourTable
Where Printing Is Not Null
Union All
Select ID_Jadwal, [Shift], Tinta, "Tinta"
From YourTable
Where Tinta Is Not Null
Union All
Select ID_Jadwal, [Shift], Cylinder, "Cylinder"
From YourTable
Where Cylinder Is Not Null
Union All
Select ID_Jadwal, [Shift], Dry, "Dry"
From YourTable
Where Dry Is Not Null
Union All
Select ID_Jadwal, [Shift], Extrusion, "Extrusion"
From YourTable
Where Extrusion Is Not Null
Union All
Select ID_Jadwal, [Shift], Slitting, "Slitting"
From YourTable
Where Slitting Is Not Null
Union All
Select ID_Jadwal, [Shift], Rewind, "Rewind"
From YourTable
Where Rewind Is Not Null
Use this as source in your append query.
Related
I have 3 tables scan_1, scan_2, scan_3. Here is structure of SQL schema:
scan_1: scan_2: scan_3:
P_no work P_no work P_no work
1 YES 1 YES 1 NO
2 NO 2 NO 2 NO
3 YES 3 YES 3 NO
I want to count P_no where work ='YES'. But if yes occurred in 2 position LIKE for P_no = 1 which having YES in scan_1 and scan_2 it must be count as 1.
my query is :
SELECT count(`P_no`) AS `ab1` FROM
`scan_1`,`scan_2`,`scan_3` WHERE
((`scan_1`.`work`= 'YES') OR
(`scan_2`.`work`= 'YES') OR
(`scan_3`.`work`= 'YES'))
TRY THIS:
SELECT COUNT(*) AS total_count
FROM
(
SELECT P_no, work FROM scan_1 WHERE work = 'YES'
UNION
SELECT P_no, work FROM scan_2 WHERE work = 'YES'
UNION
SELECT P_no, work FROM scan_3 WHERE work = 'YES'
) AS total
You can achieve your desired result using UNION like below :
SELECT COUNT(*) AS ab1
FROM (
SELECT P_no, work FROM scan_1 WHERE work='YES'
UNION
SELECT P_no, work FROM scan_2 WHERE work='YES'
UNION
SELECT P_no, work FROM scan_3 WHERE work='YES'
) as T
SQL HERE
Notice the three queries which is merged using UNION. It merges the result and excludes the duplicate records from the result.
This may work for you as far I understood. Using union for eliminating duplicates.
select count(*) AS `ab1` FROM
(
select `P_no`,`work` from `scan_1` s1 where `work`= 'YES'
union
select `P_no`,`work` from `scan_2` s2 where `work`= 'YES'
union
select `P_no`,`work` from `scan_3` s3 where `work`= 'YES'
) as final
Try this,
SELECT COUNT(DISTINCT P_No) AS ab1
FROM (
SELECT P_no, work FROM scan_1
UNION
SELECT P_no, work FROM scan_2
UNION
SELECT P_no, work FROM scan_3
) T
WHERE T.Work='YES'
Hope this helps you.
To select something from the data base we could use:
SELECT * FROM tableName where name="Ed"
But what if I need to select something from a given array, eg:
SELECT * FROM ("Bob","Sam","Ed") where name="Ed"
Is it possible?
Yes it is possible:
http://sqlfiddle.com/#!9/9eecb7d/64737
SELECT t.* FROM
(SELECT "Bob" name UNION SELECT "Sam" UNION SELECT "Ed") t
WHERE t.name="Ed"
But it has almost no sense. Because if you set all data as constant static values you can just:
SELECT "Ed"
there is no reason even to call mysql :-)
You can try with
SELECT * FROM (
select "Bob" as name
from dual
union
select "Sam" as name
from dual
union
select "Ed"as name
from dual ) as t
where t.name="Ed";
Here i like to explain my problem
Here i have a table like this
Company_name employee_id name dob father_name father_dob mother_name mother_dob
TCS EMP1201 Kalai 13/11/92 Mahendran 13/11/86 Amutha 15/7/88
CTS EMP1202 Naveen 13/11/92 raman 13/11/86 seetha 15/7/88
TCS EMP1203 Ganesh 13/11/92 Viijay 13/11/86 Sangetha 15/7/88
i need to write a query to show the father_name and father_dob seperately for employee_id belongs
same thing for mother_name and mother_dob too
i want to get a table like this,
employee_id name dob
EMP1201 Kalai 13/11/91
EMP1201 Mahendran 13/11/86
EMP1201 Amutha 15/7/88
so how can i write select query for this scenario.
Use UNION and finally order it by employee_id.
Query
select * from
(
select employee_id,name,dob
from table_name
union
select employee_id,father_name,father_dob
from table_name
union
select employee_id,mother_name,mother_dob
from table_name
)t
order by t.employee_id;
Fiddle demo
Update: If there is no(empty) father_name and father_dob,
select * from
(
select employee_id,name,dob
from table_name
union
select employee_id,father_name,father_dob
from table_name
where father_name is not null and father_dob is not null
union
select employee_id,mother_name,mother_dob
from table_name
where mother_name is not null and mother_dob is not null
)t
order by t.employee_id;
Check this fiddle.
Just use a union, something like:
SELECT employee_id, name, dob
FROM YourTable
UNION
SELECT employee_id, father_name, dob
FROM YourTable
Order the final dataset as desired.
I have a table that has the following schema:
DATA | CAUSE_1 | TIME_1 | CAUSE_2 | TIME_2 | CAUSE_3 | TIME_3
The CAUSE.* field (VarChar) can not contain any string, and if so, the field TIME.* is 0.
I'm trying to create a query, but unfortunately without success, where I would have the result display in this form:
CAUSE | TOT_TIME | N_RIPET_CAUSE,
where:
In CAUSE I have a list of what is contained in CAUSE_1 ... CAUSE_3,
In TOT_TIME the sum of the values in TIME_1 ... TIME_3,
In N_RIPET_CAUSE the number of repetitions of each CAUSE.
I hope I explained.
try this
SELECT DATA ,CAUSE , TOT_TIME , N_RIPET_CAUSE
FROM ( select DATA, CONCAT(`CAUSE_1`,' ',`CAUSE_2`, ' ', `CAUSE_3`) as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM your_table
group by DATA
) t
SEE SQLFIDDLE DEMO
EDIT.
try this
( select DATA , `CAUSE_1` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA)
union all
(select DATA , `CAUSE_2` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA )
union all
(select DATA , `CAUSE_3` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA )
SQL DEMO HERE
EDIT:
try this due to your need
select cause, sum(time) Tot_time, count(cause) N_Ripet_Cause
from(
select cause_1 as cause, time_1 as time
from Table1
union all
select cause_2 as cause, time_2 as time
from Table1
union all
select cause_3 as cause, time_3 as time
from Table1
) t
group by cause
DEMO SQL FIDDLE
If you cannot change the table structure, then in order to get this result, you are going to need to unpivot the columns into rows.
MySQL does not have an unpivot function but this can be done using a UNION ALL query. You can then apply the aggregate to those values to get the final result:
select cause, sum(time) Tot_time, count(cause) N_Ripet_Cause
from
(
select data, cause_1 as cause, time_1 as time
from yourtable
union all
select data, cause_2 as cause, time_2 as time
from yourtable
union all
select data, cause_3 as cause, time_3 as time
from yourtable
) src
group by cause
You could make a select from union select like that:
select * from
(
select cause_1 as cause, time_1 as time from tableName
union
select cause_2 as cause, time_2 as time from tableName
union
select cause_3 as cause, time_3 as time from tableName
) as joinedValues
Then you could perform any actions from that select.
Like number of each clause:
select cause, count(cause) from
(
...
) as joinedValues
group by cause
Jack is on the mark - you've got too many possibly redundant cells in your table structure. Use relations to eliminate such occurrences.
DataTable
dID | Data
instancesTable
ID | dID | CAUSE | TIME
Then use NATURAL JOIN upon the two tables to extract the information;
SELECT * FROM DataTable NATURAL JOIN instancesTable WHERE dID=? LIMIT 3
This query will return a list of causes and times of whatever occurred on the ID of the 'Data' in the first table.
Edit: *N_RIPET_CAUSE* can be found using a SUM(CAUSE) on the dID.
I have a table tblAccount. I want to get the top 4 rows from the table. If no record is there, I want to get 4 blank rows.
select *
from tblAccount
o/p
----
AccountID AccountNo
1 #101
2 #102
NULL NULL
NULL NULL
The above should be the result if two records are there.
SQLFiddle demo
select TOP 4 AccountID,AccountNo
from
(
select 0 as srt,AccountID,AccountNo from tblAccount
union all
select 1 as srt,NULL as AccountID, NULL as AccountNo
union all
select 2 as srt,NULL as AccountID, NULL as AccountNo
union all
select 3 as srt,NULL as AccountID, NULL as AccountNo
union all
select 4 as srt,NULL as AccountID, NULL as AccountNo
) as t
order by srt,AccountID
This should work. You could do the same thing with a temp table, just give it the right number of fields and rows.
with meganull(a,b) as (
select CAST(null as int),
CAST(null as varchar(max))
union all
select *
from meganull
)
select top 4 *
from (
select *
from tblAccount
union all
select *
from meganull) as sq
try this...
Select * FROM (VALUES (1),(2),(3),(4)) AS value(tmpID) left join
(
select Row_Number() over (order by AccountID) as RowCtr, *
from tblAccount
) accountTable on tmpID = accountTable.RowCtr
I used a similar technique when I wanted a way to force a certain number of rows to display in a SSRS report, where the blank rows represented empty spots in a physical rack.