Today I need somebody help for this questions.
My data
--------|-------------|------------|------------
PPP BBB BBBs
acno111 160716463.59 2327181.57 14825211.29
cno 0 0 0
cny 0 0 0
usd 313489.30 1257613.04 1257613.04
swd 0 1069568.53 0
thb 14825211.29 0 12
question :
If it is in another currency, select the PPP column.
Of the same line, for example USD is 313489.30
But in THB
Use the same line, PPP column 14825211.29 minus
Column, BBB, the top combination is the total amount 2327181.57
Equals 14825211.29 - 2327181.57 = 12498029.72
Please help resolve the problem.
Related
I have a piece of a SQL statement below:
SELECT WOCUSTFIELD.WORKORDERID, WORKORDER.ACTUALFINISHDATE,
CASE WHEN wocustfield.custfieldname LIKE '%bags%'
THEN (REPLACE( wocustfield.custfieldname, 'bags:', ''))
ELSE REPLACE( wocustfield.custfieldname, 'boxes:', '')
END AS Facility,
For each of the custfieldname that has a number > than 0 put in for Bags or Boxes I would like to count it as a Facility (visit to teh site).
Currently, it is counting every custfieldname regardless if it has a number ( 0 or >) in the field as a visit and is totaling all custfieldnames for each day.
for example, if the data look like the following for Jan 1st 2023:
Bags Boxes
Dogstation 1 3 4
Dogstation 2 0 0
Dogstation 3 5 1
Dogstation 4 2 0
Dogstation 5 0 0
I would like to have 3 Facility (visits) stations and not 5. I hope this makes sense. thanks for any help given
The COUNT aggregation counts 1 for every non-null. Zero (0) is not null, so it will count as 1. You want to do something like this:
SUM(CASE WHEN bags >0 then 1 else 0 end) as stores_with_bags
Just add in a WHERE clause. I'm not exactly sure how your table is formatted, but ex:
WHERE Bags>0 OR Boxes>0
I could use some help building a query that will be used for KPI's. This the current setup and what I'm trying to achieve, FYI I am beginner at SQL but I need to pull total number of hours for specific EVT_PPM by given year. This is being done on the front end and I have not been able to get an inner join to work. Any help or guidance would be greatly appreciated.
Table R5EVENTS:
EVT_TARGET EVT_CODE EVT_PPM EVT_STATUS EVT_CLASS
3/1/2019 1 PM-01 C CM
4/1/2019 2 PM-02 CANC PC
5/1/2019 3 PM-03 R CM
Table R5BOOKEDHOURS:
BOO_EVENT BOO_HOURS
1 2
2 5
3 1
My query
SELECT COUNT (1) boo_hours
FROM R5EVENTS, R5BOOKEDHOURS
WHERE (EVT_TARGET >= to_date('1/1/2019' ,'MM/DD/YYYY')
AND
EVT_TARGET <= to_date('12/31/2020' ,'MM/DD/YYYY') )
AND
(EVT_PPM IN ('PM-01','PM-02','PM-03','PM-04','PM-05','PM-06','PM-06','ST-01','ST-02') )
AND EVT_STATUS != 'CANC'
AND EVT_CLASS LIKE 'CM'
The output I get is 0 results!
I just started checking SQL server in my work, and I just can´t manage to solve this very easy thing.
Lets say I have this Table:
Model Serial Pass Failure
Y72742 LURC1 0 Ref 5
Y72742 LURC2 0 Ref 10
Y72596 JDID1 1 EMT
Y72596 JDID2 1 EMT
Y72742 LURC3 0 Ref 10
Y72596 JDID3 0 Ref 5
Y72596 JDID4 0 Ref 18
Y72596 JDID5 1 EMT
Y72596 JDID6 0 Ref 18
Y72596 JDID7 0 Ref 5
Y72596 LURC4 1 UMT
Y72596 LURC5 1 UMT
So in the PASS column, the 1 means the unit passed, the 0 means it failed. The Failure Column indicates what was the failure.
I'm trying to accomplish something like this:
Model Pass Failed Percentage
Y72742 2 3 40%
Y72596 1 4 20%
But since is the same column I just can't get it done.
I tried with a subquery with the "AND" operator, but I got errors. if anyone could help me I would be very grateful.
Regards.
You can construct your query using SUM and GROUP BY:
SELECT
Model
, SUM(case when Pass=1 then 1.0 else 0.0 end) AS Pass
, SUM(case when Pass=1 then 0.0 else 1.0 end) AS Failed
, 100*SUM(case when Pass=1 then 1.0 else 0.0 end)/COUNT(*) AS Percentage
FROM MyTable
GROUP BY Model
MySQL implementation:
SELECT
t.Model,
SUM(IF(t.Pass=1,1,0)) as Passed,
SUM(IF(t.Pass=0,1,0)) as Failed,
SUM(IF(t.Pass=1,1,0))/COUNT(t.Pass) as Percentage
FROM MyTable t
GROUP BY t.Model
I need to get data from different columns depending on a set of rules and I don't see how to do it. Let me illustrate this with an example. I have a table:
ID ELEM_01 ELEM_02 ELEM_03
---------------------------------
1 0.12 0 100
2 0.14 5 200
3 0.16 10 300
4 0.18 15 400
5 0.20 20 500
And I have a set of rules which look something like this:
P1Z: ID=2 and ELEM_01
P2Z: ID=4 and ELEM_03
P3Z: ID=4 and ELEM_02
P4Z: ID=3 and ELEM_03
I'm trying to output the following:
P1Z P2Z P3Z P4Z
------------------------
0.14 400 15 300
I'm used to much simpler queries and this is a bit above my level. I'm getting mixed up by this problem and I don't see a straightforward solution. Any pointers would be appreciated.
EDIT Logic behind the rules: the table contains data about different aspects of a piece of equipment. Each combination of ID/ELEM_** represents the value of one aspect of the piece of equipment. The table contains all values of all aspects, but we want a row containing data on only a specific subset of aspects, so that we can output in a single table the values of a specific subset of aspects for all pieces of equipment.
Assuming that each column is numeric and ID is unique you could do:
SELECT
SUM(CASE WHEN ID = 2 THEN ELEM_01 END) AS P1Z,
SUM(CASE WHEN ID = 4 THEN ELEM_03 END) AS P2Z,
SUM(CASE WHEN ID = 4 THEN ELEM_02 END) AS P3Z,
SUM(CASE WHEN ID = 3 THEN ELEM_03 END) AS P4Z
...
I have seen a lot of similar questions, but none seem to have an answer applicable to what I am trying to do, so here goes:
I have a table with data spread over 4 rows. Certain values in those rows need to populate another table, all in one row, mapped to specific columns (this to enable a PLC to pick it up).
I created the following query, which as you may expect gives me output in multiples rows (though doesn't actually seem to work when I execute more than 2 part in one go).
How would I go about merging these into the one row? I have had a go with UNION, and had a look at nested queries but can't work it out. Help gratefully received.
--////////
--To change format so one row contains all recipe data,
INSERT INTO dbo.Rep_Criteria (FILE_NAME, BAC, Rev, Tol_Lo1, Tol_Hi1, Eng_Tol1, Min, Max, Min TCs)
SELECT File_Name, BAC, Rev, Low, High, Eng_Tol, Min, Max, Min_TC
FROM dbo.Recipe
WHERE ID = 1 ;
INSERT INTO dbo.Rep_Criteria (Tol_Lo2, Tol_Hi2, Eng_Tol2)
SELECT Low, High, Eng_Tol
FROM dbo.Recipe
WHERE ID = 2 ;
INSERT INTO dbo.Rep_Criteria (Tol_Lo3, Tol_Hi3, Eng_Tol3)
SELECT Low, High, Eng_Tol
FROM dbo.Recipe
WHERE ID = 3 ;
INSERT INTO dbo.Rep_Criteria (Tol_Lo4, Tol_Hi4, Eng_Tol4)
SELECT Low, High, Eng_Tol
FROM dbo.Recipe
WHERE ID = 4 ;
--////////
This might help, though formatting messed up:
Table 1: (Input)
File_Name BAC Rev Low High Eng_Tol Min Max Min_TC
Zeppelin 5636 F 0 6 4.0 1550 1725 1
Zeppelin 0 0 6 15 5.0 0 0 0
Zeppelin 0 0 15 100 6.0 0 0 0
Zeppelin 0 0 100 600 15.0 0 0 0
Table 2: (Output)
File_Name - BAC - Rev - Tol_Lo1 - Tol_Lo2 - Tol_Lo3 - Tol_Lo4 - Tol_Hi1 - Tol_Hi2 - Tol_Hi3 - -------
Zeppelin 5636 F 0 6 15 100
--------
Tol_Hi4 - Eng_Tol1 - Eng_Tol2 - Eng_Tol3 - Eng_Tol4 - Min - Max - Min# T/C's
600 4.0 5.0 6.0 15.0 1550 1725 1