I have to provide counts for different activities, some of the columns are from some table but few I have to take by joining other tables. And in the last column I have to add counts *of 5 columns togather in one field*.Please see my query below and advice the best way to achieve my results :)
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
**SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT +c.count(*))as Total** --------- This is where I am getting stuck and confused???
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
I am really stuck as the table which is joined doesn'thave field named activity_count or activity_type so to see the counts I can only do count(*) but to add it all other fileds in one column and sum it up is very confusing??
Any help??/
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT END) +c.count(*) as Total
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
GROUP BY web. OID,web. MARKETING_GROUP;
Related
im looking for solutions to my problem.
i have a query
select em_name, sum(abs_day_left)
from pp_employees,
pp_types_abs,
pp_abs
where em_id = abs_em_id and abs_abs_id = abs_id and
abs_kod in ('12','13','14','15')
group by em_name
i want to make more columns with another abs_kod number (image attachment)
for example
second column
... abs_kod in
('656','44','323','33')
third column
... abs_kod in
('63','55','565','556')
and more..
example table
thanks for help and nice weekend
One more thing...
the formula counts days from the whole month
how to make it count correctly the days when it sets the parameters for the half month, for example from 1980-01-01 to 1980-03-15
thanks in advance
bob
I think that you are looking for conditional aggregation:
select
em_name,
sum(case when abs_kod in (12,13,14,15) then abs_day_left end) abs_day_left_1,
sum(case when abs_kod in (656,44,323,33) then abs_day_left end) abs_day_left_2,
sum(case when abs_kod in (63,55,565,556) then abs_day_left end) abs_day_left_3
from pp_employees
inner join pp_abs on em_id = abs_em_id
inner join pp_types_abs on abs_abs_id = abs_id
where and abs_kod in (12,13,14,15,656,44,323,33,63,55,565,556)
group by em_name
Notes:
always use explicit joins instead of old-shool, implicit joins - I tried to fix this but I am unsure I did it correctly, for a reason that lies in the following point...
always qualify the columns in the query with the table they belong to
I'm extracting student data who have completed a list of courses for degree requirements. One of the courses on the list is equivalent to another course, so if a student completes both equivalent courses, it can only be counted once towards a degree. I need to extract data on students who completed the list of courses, while filtering for just one of the equivalent courses.
Where am I going wrong?
I've tried different OR and AND NOT clauses but I can't seem to get the result that I need
use coll18_live
select ENR_STUDENT_ID, ENR_TERM, CRS_NAME, ENR_GRADE
from dbo.CA320_ENROLLMENT_VIEW_N03
WHERE ENR_CENSUS_REG_FLAG = 'Y'
and ENR_TERM in ('14/FA', '15/SP')
and not (CRS_NAME = 'BUSI-105' and CRS_NAME = 'ENGL-120')
and CRS_NAME in ('ACCT-120', 'ACCT-125', 'BUSI-100', 'BUSI-103', 'BUSI-105', 'ENGL-120')
I expect the output to show students who completed ACCT-120, ACCT-12, BUSI-100, BUSI-103, and BUSI-105 or ENGL-120 (but not both BUSI-105 or ENGL-120)
I think you want aggregating with a having clause. You cannot do this with a WHERE, because the information you want is (apparently) in different rows:
select ENR_STUDENT_ID
from dbo.CA320_ENROLLMENT_VIEW_N03
where ENR_CENSUS_REG_FLAG = 'Y' AND
ENR_TERM in ('14/FA', '15/SP')
group by ENR_STUDENT_ID
having sum(case when CRS_NAME in ('ACCT-120', 'ACCT-125', 'BUSI-100', 'BUSI-103') then 1 else 0 end) = 4 and
sum(case when CRS_NAME in ('BUSI-105', 'ENGL-120') then 1 else 0 end) > 0;
I need to get a state level count on number of services. For the purposes of this I only have two services. The first column is the states, the second column is the first services and the third column is the second service. What I am struggling with is to have the second and third column show up on the results in one query. Here is my code:
SELECT Distinct allstates.Name, count (data.StateName) as CareCase_Management_Services, count(data.StateName) Caregiver_Support_Services
From
(select distinct Name from USstate) allstates
Left Join
Client2017 data
on
allstates.Name = data.StateName and
data.FiscalYear = 2017 and
data.SrvstartCareCaseMgmtCode NOT IN('999','', '998') and
data.SrvstartCaregiverSuppCode NOT IN('999','', '998')
GROUP BY allstates.Name
ORDER BY allstates.Name ASC
I understand that you are looking to compute, for each state, the count of services that match certain criteria. There are two types of services, stored in two different columns.
If so, your query could be simplified using conditional aggregation :
SELECT
allstates.Name,
SUM(CASE WHEN c.SrvstartCareCaseMgmtCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) CareCase_Management_Services,
SUM(CASE WHEN c.SrvstartCaregiverSuppCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) Caregiver_Support_Services
FROM
(SELECT DISTINCT Name FROM USstate) s
LEFT JOIN Client2017 c ON s.Name = c.StateName AND c.FiscalYear = 2017
GROUP BY allstates.Name
With this technique, each service is counted according to its own logic ; when conditions are met, the record is counted in (1 is added to the SUM()), else it is ignored (+ 0).
NB : do you really have duplicated state names in USstate ? if no, you can replace subquery (SELECT DISTINCT Name FROM USstate) s with just USstate
I am facing different kind of problem. In select query I want to add a temporary column on fly based on other columns value.
I have 2 columns
IsOpeningClosingDateToo (tinyint),
HearingDate Date
Now I want to check that if IsOpeningClosingDate = 1 then
Select HearingDate, HearingDate as 'OpeningDate'
If IsOpeningClosingDate= 2
Select HearingDate, HearingDate as 'ClosingDate'
I have tried to do this but failed:
SELECT
,[HearingDate]
,CASE [IsOpeningClosingDate]
when 1 then [HearingDate] as OpeningDate
When 0 then [HearingDate] as ClosingDate
end as 'test'
]
FROM [LitMS_MCP].[dbo].[CaseHearings]
I would suggest returning three columns. Then you can fetch the values in on the application side:
SELECT HearingDate,
(CASE WHEN IsOpeningClosingDate = 1 THEN HearingDate END) as OpeningDate,
(CASE WHEN IsOpeningClosingDate = 0 THEN HearingDate END) as ClosingDate
FROM [LitMS_MCP].[dbo].[CaseHearings];
Alternatively, you could just fetch HearingDate and IsOpeningClosingDate and do the comparison in Python.
The important point is that the columns in a SQL query are fixed by the SELECT. You cannot vary the names or types of the columns conditionally within the query.
Hi guys i got stuck at one very simple issue that the following two queries are working fine but i want the result of both the queries in one table ,so what would be the possible way of joining these two so that they will combine and give result in one table,i tried UNION but it didnt work.Kindly tell me the solution.Thanks!
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY
from ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL
Select RATE=(CASE WHEN Discount IS NULL OR Discount=0 THEN RATE ELSE RATE-(RATE*(Discount/100))END),
AMOUNT=(CASE WHEN Discount IS NULL OR Discount=0 THEN AMOUNT ELSE AMOUNT-(AMOUNT*(Discount/100)) END)
from ITEM_DETAILS
if it is sql server did you try something like this
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY,
CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END AS 'RATE',
CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END AS 'AMOUNT'
from ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL
I am not sure if you want all the rates and amount from the ITEM_DETAILS table or only those which satisfy the join condition
By looking at both of your queries you want a combined result. In this case you do not Need UNION. You just Need to Combine the columns as below
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY,
RATE=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END),
AMOUNT=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END)
FROM ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL