Hie lets assume we have the following table
TABLE NAME : DRINGE
__________________________________________________
ID PRODUCT MACHINE MASS STATE
01 1.76ann HRB 50 inlet
02 1.76ann HRB 38 inlet
03 2.55ann GUDO 45 outlet
04 95mm x 4 GUDO 36 dispatched
___________________________________________________
And the following formula:
(inlet –outlet ) +outlet – dispatched = [resulted displayed to new view]
And the values to be substituted are:
INLET = 50 , 38
OUTLET=45
DISPATCHED = 36
So substituting in the above formula
[(inlet –outlet ) +outlet – dispatched = [resulted displayed to new view]
We get this
(50+38 – 45 ) + 45 – 36 = 52
What I want is for the result ie 52 to be displayed in an sql view like the following view
Dringe VIEW
_____________
Total_summary|
_____________|
52 |
_____________|
.
Does anyone have any idea of an sql query I can use to do this ?
I rily need your help am stuck again, thanx in advance.
The below will follow your formula
CREATE VIEW DRINGEView AS
SELECT sum(case when state = 'inlet' then mass else 0 end) -
sum(case when state = 'outlet' then mass else 0 end)+
sum(case when state = 'outlet' then mass else 0 end) -
sum(case when state = 'dispatched' then mass else 0 end) AS Total_summary
FROM dringe
But adding outlet then subtracting outlet would be redundant. The brackets in your formula would also be redundant as all of your operators are addition/subtraction, could one of your operators be multiplication/division?
you can just add those calculations to your query in something like
(40+50+fieldName*anotherFieldName) AS calculation
and you can even use sub tables like
(40+50+(SELECT fieldName FROM anotherTable WHERE someValue) AS calculation
create view dringe_view as
select (sum(inlet) – sum(outlet) ) +sum(outlet) – sum(dispatched ) as total_summary
from tab
But your claculation can be further simplified (remove outlet - outlet) to (inlet – dispatched)
Related
I've been looking for a tutorial everywhere but I couldn't find any specific solution to this. Does anybody know how to query from this table:
//////////////////
places // type /
//////////////////
alabama // ZX //
alabama // AQ //
africa // AQ //
vietnam // FD //
vietnam // FD //
////////////////
into this
///////////////////////////////////////////////////
places | ZX | AQ | FD
///////////////////////////////////////////////////
alabama| 1 | 2 | 0
africa | 0 | 1 | 0
vietnam| 0 | 0 | 2
///////////////////////////////////////////////////
EDIT: I edited the format, sorry for the error earlier.
Here is a possible solution that works across a variety of database engines.
-- Start: Setup Data
CREATE TABLE things (
places VARCHAR(50),
type VARCHAR(50)
);
INSERT INTO things VALUES ('alabama','ZX');
INSERT INTO things VALUES ('alabama','AQ');
INSERT INTO things VALUES ('africa','AQ');
INSERT INTO things VALUES ('vietnam','FD');
INSERT INTO things VALUES ('vietnam','FD');
-- End: Setup Data
SELECT
things.places,
SUM(
CASE
WHEN things.type = 'ZX' THEN 1
ELSE 0
END
) AS ZX_COUNT,
SUM(
CASE
WHEN things.type = 'AQ' THEN 1
ELSE 0
END
) AS AQ_COUNT,
SUM(
CASE
WHEN things.type = 'FD' THEN 1
ELSE 0
END
) AS FD_COUNT
FROM things
GROUP BY things.places
The drawback to this solution is that you need to know a priori which columns you want to have in your pivot table.
Here is an very well thought out answer to your question.
I'm using MySQL - Rows to Columns and this tutorial http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/#comment-6128 to help me pivot a table and it's working pretty well. Starting with this:
mediaID q_short_name start_time stop_time audio_link
ee CVV Number 208 210 j.mp3
ee Expiration Date 308 310 j.mp3
ff CVV Number 124 127 k.mp3
ff Expiration Date 166 169 k.mp3
The goal is this:
mediaID CVVNumStartT CVVNumStopT ExpDateStart_time ExpDateStop_time Aud
ee 208 210 308 310 k.mp3
ff 124 127 166 169 j.mp3
I got part of the way there with this code:
CREATE VIEW test__extension AS (
SELECT amr_text.*,
CASE WHEN q_short_name = 'CVV Number' THEN amr_text.start_time END AS
CVV_Start_Time,
CASE WHEN q_short_name = 'CVV Number' THEN amr_text.stop_time END AS
CVV_Stop_Time,
CASE WHEN q_short_name = 'Expiration Date' THEN amr_text.start_time END
AS Expiration_Start_Time,
CASE WHEN q_short_name = 'Expiration Date' THEN amr_text.stop_time END
AS Expiration_Stop_Time, FROM amr_text);
CREATE VIEW test_extension_pivot AS (SELECT mediaID,
SUM(CVV_Start_Time) AS CVV_Start_Time,
SUM(CVV_Stop_Time) AS CVV_Stop_Time,
SUM(Expiration_Start_Time) AS Expiration_Start_Time,
SUM(Expiration_Stop_Time) AS Expiration_Stop_Time,
FROM test_extension GROUP BY mediaID);
This creates columns exactly like the goal table. But now the values for everything except the mediaIDs are rendered as NULL. My questions are, why did they get replaced by NULL, and what can I use instead of SUM to render the values of Expiration and CVV Start and Stop Time as they are in the original table?
This is a fun side project I have been working on. I added a column for weight (op1w to op12w) to go with the description (op_1 to op_12). Now I need to go back through and update all entries with the appropriate op1w to op12w depending on the op_1 to op_12. Here is a part of the drop_log table with those columns.
Here are the values to match and what to update the op1w to op12w with (this exists as a table called mechs in the db):
mech weight
AS7 100
AWS 80
BJ 45
BLR 85
CDA 40
CN9 50
COM 25
CPL 65
CTF 70
DISC 0
DRG 60
GRF 55
HBK 50
HGN 90
JM6 65
JR7 35
KTO 55
LCT 20
ON1 75
QKD 60
RVN 35
SDR 30
SHD 55
STK 85
TBT 50
TDR 65
VTR 80
WVR 55
Would you guys help me with the case statement for this? I believe it should start something like this:
UPDATE drop_log
SET
op1w =(case when some_case_condition then something else op1w end),
op2w =(case when some_case_condition then something else op2w end)
.
Thanks for your help.
edit: OK after reading the comments, here is my attept
UPDATE drop_log
SET op1w=
CASE
WHEN op_1 = 'AS7'
THEN op1w = 100
WHEN op_1 = 'AWS'
THEN op1w = 80
.
.
.
ELSE op_1
END
CASE
WHEN op_12 = etc
THEN op12w = etc
.
.
.
ELSE op_12
END
Ok now I see what you guys are saying.. No I don't want you to fill it our for me. This is going to go on and on for each column 1-12 and then each column gets its own case for each weight possibility. Is that all there is to it? I thought it would be more involved.
The general format of your query should be:
UPDATE drop_log
SET op1w = CASE
WHEN op_1 = 'AWS' THEN '1'
WHEN op_2 = 'ABC' THEN op1
ELSE '0'
END,
op2w = CASE
WHEN op_1 = 'HDP' THEN op_1
ELSE '0'
END
i have a table named item with four attribute name,code,class,value
now i want to group them in following way:
group a: name='A',code=11,class='high',value between( (5300 and 5310),(7100 and 7200),(8210 and 8290))
group b: name='b',code=11,class='high',value between( (1300 and 1310),(2100 and 2200),(3210 and 3290))
how can i do it?
You might want to try something like this:
SELECT
CASE
WHEN code = 11 AND
class = 'high' AND
(code BETWEEN 5300 AND 5310 OR
code BETWEEN 7100 AND 7200 OR
code BETWEEN 8210 AND 8290)
THEN 'A'
WHEN code = 11 AND
class = 'high' AND
(code BETWEEN 1300 AND 1310 OR
code BETWEEN 2100 AND 2200 OR
code BETWEEN 3210 AND 3290)
THEN 'B'
ELSE Unknown
END AS name,
*
FROM your_table
ORDER BY name
You might wish to change ORDER BY to GROUP BY and you should be aware that BETWEEN includes both endpoints.
First group
select * from item
where name LIKE 'A'
and code LIKE '11'
and class LIKE 'high'
and (value BETWEEN 5300 AND 5310 OR value BETWEEN 7100 AND 7200 OR value BETWEEN 8210 AND 8290)
the same idea for group b
Is there any way to execute a scalar-valued function from within a Derived Column transformation in SSIS?
-Scenario-
I have a function in my source DB that converts weights based on a UOM value in the record's UOM column. I want to utilize this function in the ETL process to ensure that my weight measures are always pounds. Can I call this function from within a Derived Column? If not, is there another transformation task I could utilize within the Data Flow (trying to avoid staging columns)?
dbo.Tasks table
id | Name | netWeight | grossWeight | UOM
12 Task12 30000 50000 10
dbo.MeasurementUnits table
id | Name | Shortname | Type | Precision
12 Kilogram kg 3 10000
14 Pound lb 3 10000
dbo.GetConvertedWeight function
ALTER FUNCTION [dbo].[GetConvertedWeight](#iWeight money, #ifromUOM int, #iToUOM int)
RETURNS money
AS
BEGIN
DECLARE #lConvertedWeight money,
#lKgToGrams money,
#lLbToGrams money,
#lOzToGrams money,
#lWeightInGrams money
--convert the weight to grams first.
SELECT #lWeightInGrams = CASE WHEN #iFromUOM = 12 THEN (ISNULL(#iWeight,0) * 1000)
WHEN #iFromUOM = 14 THEN (ISNULL(#iWeight,0) * 453.5924)
WHEN #iFromUOM = 15 THEN (ISNULL(#iWeight,0) * 28.3495)
WHEN #iFromUOM = 13 THEN (ISNULL(#iWeight,0))
ELSE ISNULL(#iWeight,0)
END
--Convert the converted weight to grams to the desired weight
SELECT #lConvertedWeight = CASE WHEN #iToUOM = 12 THEN (ISNULL(#lWeightInGrams,0) / 1000)
WHEN #iToUOM = 13 THEN ISNULL(#lWeightInGrams,0)
WHEN #iToUOM = 14 THEN (ISNULL(#lWeightInGrams,0)/453.5924)
WHEN #iToUOM = 15 THEN (ISNULL(#lWeightInGrams,0) / 28.3495 )
ELSE (ISNULL(#lWeightInGrams,0)/453.5924)
END
RETURN #lConvertedWeight
Example function call
dbo.GetConvertedWeight(dbo.Tasks.netWeight, dbo.Tasks.weightUOM, 14) AS netWeight
Nope. What you'll want is an OLE DB Command to do that. Send the results to an Output Column, and life should be peachy keen for you--at least with regards to SSIS.