Author |
Topic |
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-06 : 11:52:57
|
Hi,Here's the "Customer" table I haveCus_ID Cus_code Cus_State1 X Y2 y N3 Z NHow do I insert a new record into the same Customer table with new Cus_code = 'P' when Cus_code is X or Y and Cus_State is Y for those respective customer codes. I don't want any duplicate records to be inserted. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-06 : 12:12:00
|
do you mean this?INSERT INTO Customer (Cus_Code,Cus_State)SELECT 'P','y'FROM CustomerWHERE EXISTS (SELECT 1 FROM Customer WHERE Cus_Code IN ('x','y') )AND NOT EXISTS (SELECT 1 FROM Customer WHERE Cus_Code ='P' AND Cus_State ='y') ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-06 : 13:01:10
|
I also want to check for customer id like when cus_id = '1'. How do I incorporate that? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-06 : 13:02:57
|
add it as a condition inside NOT EXISTS using AND------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-06 : 13:03:38
|
Actually the cus_id is not a unique key. There could be different rows with same customer id.Cus_id cus_code cus_state1 X Y1 Y N1 Z N Now, How do I insert a new record into the same Customer table with new Cus_code = 'P' when Cus_code is X or Y and Cus_State is Y for those respective customer codes. I don't want any duplicate records to be inserted. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-06 : 13:11:47
|
so in above case how many new records it will insert? can you show table output after insertion?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-06 : 14:26:53
|
Basically I want to insert a new row for every cus_id which has cus_code = X or cus_code = Y or cus_code = both x and y and cus_state = y for either one of them. |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-06 : 14:34:32
|
Here's the actual table customersub_id list_id cust_id ss_id sub_codesub_state4363835 1 3275330 0 B2B N4363836 1 3275330 0 CC Y4363837 1 3275330 0 CU N4363838 1 3275330 0 FND Y4363839 1 3275330 0 MA N4363840 1 3275330 0 MB Y4363841 1 3275330 0 MNE N4363842 1 3275330 0 MP N4363843 1 3275330 0 MR N4363844 1 3275330 0 MTL N4363845 1 3275330 0 PD Y4363846 1 3275330 0 PDD Y4363847 1 3275330 0 SO Y4363848 1 3275330 0 TV Y4363849 1 3275330 0 WEB Y4363850 1 3275330 0 YOC YI want to insert an additional record with sub_code = 'AWD' sub_state = 'y' for each customer who has sub_code = 'PDD' or 'TV' and sub_state = "Y". How do i do that? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-06 : 23:20:13
|
[code]INSERT Customer (list_id,cust_id,ss_id,sub_code,sub_state)SELECT list_id,cust_id,ss_id,'AWD','y'FROM CustomerGROUP BY list_id,cust_id,ss_idHAVING SUM(CASE WHEN sub_code IN ('PDD','TV') AND sub_State='y' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN sub_code = 'AWD' AND sub_state = 'y' THEN 1 ELSE 0 END)=0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-07 : 10:13:38
|
This results in inserting two duplicate records with AWD when both PDD and TV cus_state is Y |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-07 : 11:00:07
|
nope..it will insert only a single record so far as list_id,cust_id,ss_id values are same from them as per your sample data.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-07 : 14:47:38
|
The thing is I have multiple customers with different sub_codes for the same customer_id |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-07 : 14:58:44
|
so what. i'm not grouping by sub_code so you wont get any duplicates as per your earlier posted sample data------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-07 : 15:39:35
|
This is what I get when I run the following query.INSERT AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS (List_ID, Master_Customer_id, SS_ID, Subscription_code, Subscription_state, Add_Date)SELECT List_ID, Master_Customer_id, SS_ID,'AWD','y', '2013-03-06 11:40:20.400'FROM AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS Where Master_Customer_id = '03275330'GROUP BY List_ID, Master_Customer_id, SS_ID, Add_DateHAVING SUM(CASE WHEN Subscription_code IN ('PDD','TV') AND Subscription_state='y' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN Subscription_code = 'AWD' AND Subscription_state = 'y' THEN 1 ELSE 0 END)=0 i don't want two records with the same subscription code AWD for the same customer idsub_id list_id mastcustid ssid subcode substate adddate 4363835 1 3275330 0 B2B N 40:20.4 4363836 1 3275330 0 CC Y 40:20.4 4363837 1 3275330 0 CU N 40:20.4 4363838 1 3275330 0 FND Y 40:20.4 4363839 1 3275330 0 MA N 40:20.4 4363840 1 3275330 0 MB Y 40:20.4 4363841 1 3275330 0 MNE N 40:20.4 4363842 1 3275330 0 MP N 40:20.4 4363843 1 3275330 0 MR N 40:20.4 4363844 1 3275330 0 MTL N 40:20.4 4363845 1 3275330 0 PD Y 40:20.4 4363846 1 3275330 0 PDD Y 40:20.4 4363847 1 3275330 0 SO Y 40:20.4 4363848 1 3275330 0 TV Y 40:20.4 4363849 1 3275330 0 WEB Y 40:20.4 4363850 1 3275330 0 YOC Y 40:20.4 8304011 1 3275330 0 AWD y 40:20.4 8304012 1 3275330 0 AWD y 40:20.4 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 00:17:33
|
quote: Originally posted by maddyslayer This is what I get when I run the following query.INSERT AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS (List_ID, Master_Customer_id, SS_ID, Subscription_code, Subscription_state, Add_Date)SELECT List_ID, Master_Customer_id, SS_ID,'AWD','y', '2013-03-06 11:40:20.400'FROM AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS Where Master_Customer_id = '03275330'GROUP BY List_ID, Master_Customer_id, SS_ID, Add_DateHAVING SUM(CASE WHEN Subscription_code IN ('PDD','TV') AND Subscription_state='y' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN Subscription_code = 'AWD' AND Subscription_state = 'y' THEN 1 ELSE 0 END)=0 i don't want two records with the same subscription code AWD for the same customer idsub_id list_id mastcustid ssid subcode substate adddate 4363835 1 3275330 0 B2B N 40:20.4 4363836 1 3275330 0 CC Y 40:20.4 4363837 1 3275330 0 CU N 40:20.4 4363838 1 3275330 0 FND Y 40:20.4 4363839 1 3275330 0 MA N 40:20.4 4363840 1 3275330 0 MB Y 40:20.4 4363841 1 3275330 0 MNE N 40:20.4 4363842 1 3275330 0 MP N 40:20.4 4363843 1 3275330 0 MR N 40:20.4 4363844 1 3275330 0 MTL N 40:20.4 4363845 1 3275330 0 PD Y 40:20.4 4363846 1 3275330 0 PDD Y 40:20.4 4363847 1 3275330 0 SO Y 40:20.4 4363848 1 3275330 0 TV Y 40:20.4 4363849 1 3275330 0 WEB Y 40:20.4 4363850 1 3275330 0 YOC Y 40:20.4 8304011 1 3275330 0 AWD y 40:20.4 8304012 1 3275330 0 AWD y 40:20.4
this was not i suggestedmodify like above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-08 : 12:05:37
|
Thanks dude, It worked.What does the following mean?HAVING SUM(CASE WHEN Subscription_code IN ('PDD','TV') AND Subscription_state='y' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN Subscription_code = 'AWD' AND Subscription_state = 'y' THEN 1 ELSE 0 END)=0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 12:09:16
|
the first condition checks for presence of records in table with Subscription_code IN ('PDD','TV') AND Subscription_state='y' for the same List_ID, Master_Customer_id, SS_ID value combination.the second condition make sure you add the record with 'AWD','y' only if its not already present------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-08 : 12:11:32
|
Thanks again. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-08 : 13:07:46
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
maddyslayer
Yak Posting Veteran
57 Posts |
Posted - 2013-03-11 : 10:57:34
|
Can you please let me know how do I get all the customers with subscription codes "pdd" and "tv" and subscription state = y for both those codes? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-11 : 12:28:28
|
do you mean only records of customers having subscription codes "pdd" and "tv" and subscription state = y like belowSELECT *FROM AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS WHERE Subscription_code IN ('PDD','TV') AND Subscription_state='y' or all records of customers who have records with subscription codes "pdd" and "tv" and subscription state = y ie likeSELECT *FROM(SELECT *,SUM(CASE WHEN Subscription_code IN ('PDD','TV') AND Subscription_state='y' THEN 1 ELSE 0 END) OVER (PARTITION BY Master_Customer_id) AS CntFROM AMA_SUB_SUBSCRIBER_SUBSCRIPTIONS)tWHERE Cnt >0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Next Page
|