I am having trouble getting my query to group correctly. I need to include the TELLERACTIVITY.ENTRY_DATE date column so I can filter by the date later on. However, I do not want it to group by the TELLERACTIVITY.ENTRY_DATE date because it gives me more results than I want.
SELECT DISTINCT ACTIVITYINFO.MEMBER_NBR AS "Member Number"
,ACCOUNT.ACCOUNT_NBR AS "Account Number"
,TELLERACTIVITY.FOCUS_TELLER_ID AS "Teller ID"
,TELLERACTIVITY.ENTRY_DATE AS "Date"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 1 THEN 1
ELSE 0
END )) AS "Branch 1"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 2 THEN 1
ELSE 0
END )) AS "Branch 2"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 3 THEN 1
ELSE 0
END )) AS "Branch 3"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 4 THEN 1
ELSE 0
END )) AS "Branch 4"
FROM TELLERACTIVITY
JOIN OPERATOR
ON TELLERACTIVITY.FOCUS_TELLER_ID = OPERATOR.OPR_NBR
JOIN ACTIVITY
ON TELLERACTIVITY.ACTIVITY_ID = ACTIVITY.ACTIVITY_ID
AND TELLERACTIVITY.FOCUS_TELLER_ID = ACTIVITY.FOCUS_TELLER_ID
AND TELLERACTIVITY.ENTRY_DATE = ACTIVITY.ENTRY_DATE
JOIN ACTIVITYINFO
ON ACTIVITY.ACTIVITY_ID = ACTIVITYINFO.ACTIVITY_ID
JOIN ACCOUNT
ON ACTIVITYINFO.MEMBER_NBR = ACCOUNT.MEMBER_NBR
AND ACTIVITYINFO.ACCOUNT_NBR = ACCOUNT.ACCOUNT_NBR
WHERE TELLERACTIVITY.FOCUS_TELLER_ID < 6000
GROUP BY ACTIVITYINFO.MEMBER_NBR
,ACCOUNT.ACCOUNT_NBR
,OPERATOR.BRANCH_NBR
,TELLERACTIVITY.FOCUS_TELLER_ID
,TELLERACTIVITY.ENTRY_DATE
ORDER BY ACTIVITYINFO.MEMBER_NBR
Related
Refer to the below image, how to merge all chapter into one single rows of different page number?
e.g.
Page 1 has 3 rows as there are 3 data at columns of chp 8,9,10
Page 2 has 5 rows as there are 5 data at columns of chp 8,9,1,11,12
So
how to have a single row of each page number and the all data placed at their own chp?
Basic SQL as below:
SELECT `mc_ee_page_id` as Page,
IF ( mc_ee_chp_id = 1, ( CASE
WHEN (is_marked = 0 and mk_no_mark = 1 ) THEN "Y"
WHEN (is_marked = 1 and mk_no_mark = -1) THEN "N"
WHEN (is_marked = 1 and mk_no_mark = 1 ) THEN "O" ELSE "X"
END ) , "" ) AS chp_1 ,
IF ( mc_ee_chp_id = 2, ( CASE
WHEN (is_marked = 0 and mk_no_mark = 1 ) THEN "Y"
WHEN (is_marked = 1 and mk_no_mark = -1) THEN "N"
WHEN (is_marked = 1 and mk_no_mark = 1 ) THEN "O" ELSE "X"
END ) , "" ) AS chp_2 ,
IF ( mc_ee_chp_id = 3, ( CASE
WHEN (is_marked = 0 and mk_no_mark = 1 ) THEN "Y"
WHEN (is_marked = 1 and mk_no_mark = -1) THEN "N"
WHEN (is_marked = 1 and mk_no_mark = 1 ) THEN "O" ELSE "X"
END ) , "" ) AS chp_3 ,
:
:
IF ( mc_ee_chp_id = 12, ( CASE
WHEN (is_marked = 0 and mk_no_mark = 1 ) THEN "Y"
WHEN (is_marked = 1 and mk_no_mark = -1) THEN "N"
WHEN (is_marked = 1 and mk_no_mark = 1 ) THEN "O" ELSE "X"
END ) , "" ) AS chp_12
FROM `maxcare_mc_ee_hw`
WHERE `stud_id` = '3312' AND `mc_ee_level_id` = '1'
GROUP BY mc_ee_chp_id, mc_ee_page_id
ORDER BY mc_ee_page_id asc
LIMIT 500
One way would be to GROUP the results of your SQL by making it the subquery of an outer query...
SELECT Page, MAX(chp_1) AS chp_1, MAX(chp_2) AS chp_2, MAX(chp_3) AS chp_3, MAX(chp_4) AS chp_4, MAX(chp_5) AS chp_5, MAX(chp_6) AS chp_6, MAX(chp_7) AS chp_7, MAX(chp_8) AS chp_8, MAX(chp_9) AS chp_9, MAX(chp_10) AS chp_10, MAX(chp_11) AS chp_11, MAX(chp_12) AS chp_12
FROM
(
-- *** paste your SQL in here ***
) AS indivdual_rows
GROUP BY Page
It assumes a page cannot have more than one non-blank value for the same chapter. If that is not the case, then it will only show the MAX alphabetical value.
Click here to go to SQL Fiddle to see it in action.
I have a view which "web.individual_usage_vw".
Whose total count is = 39057.
SQL Query: select count(*) from web.individual_usage_vw;
In this view, it has few columns in which it has numeric data.
So I need to fetch all the records from view where data is > 1.
So I used below query:
select count(*) from web.individual_usage_vw
where "Business & economy" != 0
and "Executive rewards" != 0
and "Health & benefits" != 0
and "Investment" != 0
and "Corporate marketing" != 0
and "Retirement" != 0
and "In General" != 0
and "Mergers & acquisitions" != 0
and "Corporate strategy operations" != 0
and "Broad-based rewards" != 0
and "Leadership" != 0
and "Talent" != 0
and "Other" != 0;
This result me with count 0
Whereas
select count(*) from web.individual_usage_vw
where "Business & economy" = 0
and "Executive rewards" = 0
and "Health & benefits" = 0
and "Investment" = 0
and "Corporate marketing" = 0
and "Retirement" = 0
and "In General" = 0
and "Mergers & acquisitions" = 0
and "Corporate strategy operations" = 0
and "Broad-based rewards" = 0
and "Leadership" = 0
and "Talent" = 0
and "Other" = 0;
Result me with count = 36228
I am not able to debug this error.
There is nothing wrong with the results. The second query is fetching rows only when all your columns (in the predicate) are having 0. In your first query, you are fetching rows where all columns have non zero values. The missing records (delta) might be having one or more columns where the result is zero.
For example
C1, C2
0 0
1 0
1 1
0 1
select * from t1 where c1 = 0 and c2=0; -- returns one row
select * from t1 where c1 != 0 and c2!=0; -- returns one row and not three
Hence the difference. If you need to return all rows that are not returned in your second query then use NOT EXISTS
Roughly, this is what you need.
SELECT columns
FROM table
WHERE NOT EXISTS (SELECT *
FROM table
WHERE all columns = 0) ;
I need help with the following query code. Currently it is giving me the 3802 query error "Database does not exist". Well I did my "from" selection query as 'oo', so that definitely exists. This worked yesterday with some minor changes to the "from" part of the query. The assistance is appreciated.
UPDATE snd_bqa.open_order_all_test
FROM (
SELECT
CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Complete",
CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN 'YES'
ELSE 'NO'
END AS "Order Cancelled",
CASE WHEN pih.effective_start_date IS NOT NULL AND pih.product_instance_status_code = 3
THEN 'YES'
ELSE 'NO'
END AS "Service Active in PB",
c.ordernumber,
c1.order_requested_due_date,
c.creationdate AS "Order Creation Date",
c.ordersubmitdate AS "Order Submit Date",
c.lastupdateddate,
c.supptype,
m.order_status_desc,
m.phase_stage,
cus.sub_nasp_id,
cus.gch_id,
b.work_order_no,
b.itemcode,
b.instance_id,
COALESCE(b.specific_date, b.standard_date) AS "Service Req Due Date",
m.milestone,
m.milestone_desc,
cus.custlegalname,
a.account_number,
vle.vle_id,
vle.currency_cd,
cus.duns_number,
a.cle_vle_id,
CURRENT_TIMESTAMP(2) AS "Originally Loaded",
CURRENT_TIMESTAMP(2) AS "Last Upd",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND NVL(c.supptype, 'NULL') <> 'CAN'
THEN c.lastupdateddate
ELSE NULL
END) AS TIMESTAMP(2)) "Order Completed Date",
CAST((CASE WHEN m.order_status_desc = 'Completed' AND c.supptype = 'CAN'
THEN c.lastupdateddate
ELSE NULL
end) AS TIMESTAMP(2)) "Order Cancelled Date",
pih.last_modified AS "Service Last Modified Date",
pih.effective_start_date AS "Service Effective Date"
FROM (
SELECT *
FROM edw_stg_ord_cw_vw.cwpc_basketitem
WHERE itemcode LIKE 'PR%'
AND (instance_id, lastupdateddate) IN (
SELECT instance_id, MAX(lastupdateddate)
FROM edw_stg_ord_cw_vw.cwpc_basketitem
GROUP BY instance_id
)) AS b
INNER JOIN edw_stg_ord_cw_vw.cworderinstance AS c
ON c.basket_id = b.basketid
AND c.isactive = 1
AND c.request_type = 'ORD'
LEFT JOIN (
SELECT c.ordernumber,
MAX(COALESCE(b.specific_date, b.standard_date)) order_requested_due_date
FROM edw_stg_ord_cw_vw.cworderinstance AS c
INNER JOIN edw_stg_ord_cw_vw.cwpc_basketitem AS b
ON b.basketid = c.basket_id
AND c.request_type = 'ORD'
AND c.isactive = 1
GROUP BY c.ordernumber
) AS c1
ON c1.ordernumber = c.ordernumber
LEFT JOIN edw_stg_ord_cw_vw.uno_customer AS cus
ON cus.cworderid = c.cwdocid
AND cus.oi_customer_id = c.ordering_customer_id
LEFT JOIN edw_stg_ord_cw_vw.uno_milestone AS m
ON m.milestone_id = c.milestone_id
LEFT JOIN edw_stg_ord_cw_vw.uno_account AS a
ON a.cworderid = c.cwdocid
AND b.account_id = a.oi_account_id
LEFT JOIN edw_stg_ord_cw_vw.uno_cle_vle AS vle
ON vle.cle_vle_id = a.cle_vle_id
AND c.cwdocid = vle.cworderid
INNER JOIN snd_bqa.open_order_all_test tst
ON tst."Service Instance ID" = b.instance_id
AND tst."Milestone" <> m.milestone
LEFT JOIN edw_pb_stg_vw.product_instance_history AS pih
ON pih.general_5 = tst."Service Instance ID"
WHERE cus.sub_nasp_id NOT IN ('19HNYO', '23MAXA', '19HNYR', '10INTD')
AND COALESCE(b.specific_date, b.standard_date, c1.order_requested_due_date) BETWEEN CURRENT_DATE - 60 AND CURRENT_DATE - 30
) oo
SET "Order Complete" = oo."Order Complete",
"Order Cancelled" = oo."Order Cancelled",
"Service Active in PB" = oo."Service Active in PB",
"Service Order Number" = oo.ordernumber,
"Order Requested Due Date" = oo.order_requested_due_date,
"Order Last Updated" = oo.lastupdateddate,
"Order Creation Date" = oo."Order Creation Date",
"Order Submit Date" = oo."Order Submit Date",
"Order Supp Type" = oo.c.supptype,
"Order Status" = oo.order_status_desc,
"Phase Stage" = oo.phase_stage,
"Milestone" = oo.milestone,
"Milestone Description" = oo.milestone_desc,
"NASP ID" = oo.sub_nasp_id,
"GCH ID" = oo.gch_id,
"Work Order Numbers" = oo.work_order_no,
"Product Code" = oo.itemcode,
"Service Requested Due Date" = oo."Service Req Due Date",
"Customer Name" = oo.custlegalname,
"Account Number" = oo.account_number,
"VLE ID" = oo.vle_id,
"Currency Code" = oo.currency_cd,
"DUNs Number" = oo.duns_number,
"CLE VLE ID" = oo.cle_vle_id,
"Last Record Update" = oo."Last Upd",
"Order Completed Date" = oo."Order Completed Date",
"Order Cancelled Date" = oo."Order Cancelled Date",
"Service Last Modified Date" = oo."Service Last Modified Date",
"Service Effective Date" = oo."Service Effective Date"
WHERE open_order_all_test."Service Instance ID" = oo.instance_id
;
I found the issue. I had oo.c.supptype, when it should have just been oo.supptype
This caused Teradata to think 'oo' was referring to a database. case closed.
I have a table TEST with columns VALUE,VALUE_SIM,SIM_STATUS,ID. I want to update the column SIM_STATUS for the ID = 288. I also want to display the columns after updating.
The conditions are :
1. SIM_STATUS = 0 when VALUE = VALUE_SIM.
2. SIM_STATUS = 1 when VALUE < VALUE_SIM.
3. SIM_STATUS = 2 when VALUE > VALUE_SIM.
I wrote the following query but it is showing an error.
("UPDATE TEST"
"SET SIM_STATE = ( CASE WHEN VALUE = VALUE_SIM THEN SIM_STATE = 0 END )"
"SET SIM_STATE = ( CASE WHEN VALUE < VALUE_SIM THEN SIM_STATE = 1 END )"
"SET SIM_STATE = ( CASE WHEN VALUE > VALUE_SIM THEN SIM_STATE = 2 END )"
"where ID = 288 ");
The query that you want is:
UPDATE TEST
SET SIM_STATE = (CASE WHEN VALUE = VALUE_SIM THEN 0
WHEN VALUE < VALUE_SIM THEN 1
WHEN VALUE > VALUE_SIM = 2
END)
WHERE NUMBER = 288;
Your query has several syntax errors. I don't even know if you intend for the double quotes to be part of the query.
This would do i guess
UPDATE TEST
SET
SIM_STATE =
CASE WHEN VALUE < VALUE_SIM THEN 1
+ CASE WHEN VALUE > VALUE_SIM THEN 2
+ CASE WHEN VALUE = VALUE_SIM THEN 0
WHERE ID = 1
The following query returns a single number, representing the n in a 1:N ratio. Instead of just returning a single number, how can I prepend the integer value with the following text 1: to give the result the correct ratio format?
select round((sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" then 1 else 0 end)) /
(sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" and job_level IN ("08", "09") then 1 else 0 end)),0)
Assuming it would be for MySQL:
select
CONCAT("1:", round((sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
then 1
else 0
end)) /
(sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
and job_level IN ("08", "09")
then 1
else 0
end)),0))
I hope it helps.
You can prepend text by just doing this:
SELECT "Foo" + stuff FROM table;
In this case, if stuff was 3, the result would be "Foo3" (without the quotes).
So in your case:
SELECT "1:" + ... -- the rest of it goes here