I have 3 tables
tb_spp
tb_daycare
tb_antarjemput
Here is the contents of all three tables:
tb_spp
tb_daycare
tb_antarjemput
I want outpunya like this
And for the last output is limit 1 like this:
How to master?
You can refer my query for option 1.
select T1.kode,
T1.Nominal_spp,
T2.Nominal_antarjemput,
(IFNULL(T1.Nominal_spp, 0)*2 + IFNULL(T2.Nominal_antarjemput, 0)) as Total
from tb_spp as T1
left join tb_antarjemput as T2 on T1.kode = T2.kode
Related
I have a table Product with 240 000 rows.
I want to select from this table data where idproduct = idproductcomponent
Output makes a table with 3 columns A12345678 35655455952625 9638520963258960, so in different column.
Always idproductcomponent of idproducttype 1 is 0, and idproductcomponent of idproducttype 2,3 are the same of idproduct of idproducttype 1.
Pelase, can you share with me any idea for this select ?
Assuming your database is SQL Server (you don't say which one it is) the query could look like:
select
a.name,
b.name,
c.name
from t a
left join t b on b.idproductcomponent = a.idproduct and b.idproducttype = 2
left join t c on c.idproductcomponent = a.idproduct and c.idproducttype = 3
where a.idproducttype = 1
and a.idproduct = 11163 -- parameter you are searching for
To increase the performance of this query you can add the index:
create index ix1 on t (idproductcomponent);
I have two tables named [DrugPrescriptionEdition] and [PrescriptionDoseDetail] and now, I join that two tables using the below query and taking a result set.
select * from DrugPrescription dp where id in(
SELECT distinct dpe.template
FROM [DrugPrescriptionEdition] dpe
join PrescriptionDoseDetail pdd on pdd.prescription = dpe.id
where doseEnd_endDate is NULL and doseEnd_doseEndType =1
)
but now I want to take records only contain, (1,2) combination of 'datasource' column and prescription.id should be same.
Example : like records { prescriptionID =4 and there contain ,(1,2) }. I will not consider, only 1 ,or 2 contain records.
Need some expert help to adding this conditions to my above query and modify it .
Expected result : I need to filter out , above query result using this, new condition too.
Let me assume your records are in a single table. Here is one method:
select t.*
from t
where (t.dataSource = 1 and
exists (select 1
from t t2
where t2. prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
) or
(t.dataSource = 2 and
exists (select 1
from t t2
where t2.prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
);
It is unclear if any other data sources are allowed. If they are not, then add:
and
not exists (select 1
from t t3
where t3.prescriptionid = t.prescriptionid and
t3.dataSource not in (1, 2)
)
I have a table with the following set of information:
r-Id v-id cost
---------------------------
i-1234 v-1234 0.5
v-1234 - 1.25
I can't quite put my finger on a query for a scenario like this: If a r-Id consists of a v-id, sum up the corresponding v-id cost to the r-id.
So i-1234 cost should be cost: 1.75. Any help is appreciated. Thanks.
Does this do what you want?
select t.r_id, max(t.cost) + coalesce(sum(t2.cost), 0)
from table t left join
table t2
on t.v_id = t2.r_id
group by t.r_id;
This produces the output you want for the data provided. If the r_id ("i-1234") could be repeated multiple times, then this is not quite the right query.
EDIT:
If the r_id could appear multiple times, then you need to pre-aggregate by it:
select t.r_id, sumcost + coalesce(sum(t2.cost), 0)
from (select t.r_id, sum(t.cost) as sumcost
from table t
group by t.r_id
) t left join
table t2
on t.v_id = t2.r_id
group by t.r_id, sumcost,
I looked up what I could, but could not find the same problem.
I am using the following SQL to attempt to generate the results that I am looking for.
I am trying to accomplish a join or another function that will allow me to Count or Select rows from the left side that have matching rows on the right side. If there is a matching column on the right side, I only want it to be counted once.
I am looking to see if at least one match (and count it as 1 even if they are more) for it to be counted.
This is what I am trying to do.
Left Side
Annual_Capture (ID)
1 Some Data
2 Some Data
3 Some Data
Right Side (Annual Comments)
2 Comment for Annual Capture id 2
2 Another Comment for Annual Capture id 2
Result:
From a select it would only return Annual_Capture (ID) of 2.
From a count, it would return only the record on Annual_Capture (ID 2)
In my case I keep getting a count of 2 instead of one.
Here is the SQL I am using:
COUNT:
SELECT COUNT(*) FROM Annual_Capture
JOIN Annual_Comments
ON Annual_Capture.id = Annual_Comments.Record_id
where Main_AbstractNumber = 133 and Year = 2012 ;
SELECT
SELECT * FROM Annual_Capture INNER JOIN Annual_Comments
ON Annual_Capture.id = Annual_Comments.Record_id WHERE Main_AbstractNumber = 133
AND Year = 2012
Any help would be appreciated!
select
count(distinct Annual_Capture.id)
from
Annual_Capture
inner join Annual_Comments
on Annual_Capture.id = Annual_Comments.Record_id
where
Main_AbstractNumber = 133
and Year = 2012
;
This only returns rows from Annual_Capture where there is at least one comment (which is how I read your rqmts):
SELECT Annual_Capture.id, count(Annual_Comments.*)
FROM Annual_Capture JOIN Annual_Comments ON Annual_Capture.id = Annual_Comments.Record_id
WHERE Main_AbstractNumber = 133 and Year = 2012
GROUP BY Annual_Capture.id
HAVING Count(Annual_Comments.*) >= 1;
Table 1 looks like the following.
ID SIZE TYPE SERIAL
1 4 W-meter1 123456
2 5 W-meter2 123456
3 4 W-meter 585858
4 4 W-Meter 398574
As you can see. Items 1 and 2 both have the same Serial Number. I have an innerjoin update statement that will update the UniqueID on these devices based on linking their serial number to the list.
What I would like to do. Is modify by hand the items with duplicate serial numbers and scripted update the ones that are unique. Im presuming I have to reference the distinct command here somewhere buy not sure.
This is my update statement as is. Pretty simple and straight forward.
update UM00400
Set um00400.umEquipmentID = tb2.MIUNo
from UM00400 tb1
inner join AA_Meters tb2 on
tb1.umSerialNumber = tb2.Old_Serial_Num
where tb1.umSerialNumber <> tb2.New_Serial_Num
;WITH CTE
AS
(
SELECT * , rn = ROW_NUMBER() OVER (PARTITION BY SERIAL ORDER BY SERIAL)
FROM UM00400
)
UPDATE CTE
SET CTE.umEquipmentID = tb2.MIUNo
inner join AA_Meters tb2
on CTE.umSerialNumber = tb2.Old_Serial_Num
where tb1.umSerialNumber <> tb2.New_Serial_Num
AND CTE.rn = 1
This will update the 1st record of multiple records with the same SERIAL.
If i understand your question correctly below query will help you out :
;WITH CTE AS
(
// getting those serial numbers which are not duplicated
SELECT umSerialNumber,COUNT(umSerialNumber) as CountOfSerialNumber
FROM UM00400
GROUP BY umSerialNumber
HAVING COUNT(umSerialNumber) = 1
)
UPDATE A SET A.umEquipmentID = C.MIUNo
FROM UM00400 A
INNER JOIN CTE B ON A.umSerialNumber = B.umSerialNumber
INNER JOIN AA_Meters C ON A.umSerialNumber = C.Old_Serial_Num