I have two tables. The first contains (amongst others) 20 columns named "CustomCode1" to "CustomCode20". These contain values for data that can be associated with the relevant custom codes. The other table contains 20 rows. Each row (with ID 1 to 20) is associated with the relevant column in the first table. The second table has a column for "Enabled".
What I need is to only select the enabled columns from the first tables.
e.g. suppose ID 10, 15 and 20 are enabled in the second table, I only want to select the value in the columns CustomCode10, CustomCode15 and CustomCode20.
I know this isn't the best design for the tables, but I am having to work with what I have been given. There will only ever be these 20 codes.
Any help would be gratefully appreciated.
Thank you in advance
Edit:
I have this in SQL which will only return values for the fields which are enabled:
select
case when ((select enabled from customcodes where id = 1) = 1) then CustomCode1 end as CustomCode1,
case when ((select enabled from customcodes where id = 2) = 1) then CustomCode2 end as CustomCode2,
case when ((select enabled from customcodes where id = 3) = 1) then CustomCode3 end as CustomCode3,
case when ((select enabled from customcodes where id = 4) = 1) then CustomCode4 end as CustomCode4,
case when ((select enabled from customcodes where id = 5) = 1) then CustomCode5 end as CustomCode5,
case when ((select enabled from customcodes where id = 6) = 1) then CustomCode6 end as CustomCode6,
case when ((select enabled from customcodes where id = 7) = 1) then CustomCode7 end as CustomCode7,
case when ((select enabled from customcodes where id = 8) = 1) then CustomCode8 end as CustomCode8,
case when ((select enabled from customcodes where id = 9) = 1) then CustomCode9 end as CustomCode9,
case when ((select enabled from customcodes where id = 10) = 1) then CustomCode10 end as CustomCode10,
case when ((select enabled from customcodes where id = 11) = 1) then CustomCode11 end as CustomCode11,
case when ((select enabled from customcodes where id = 12) = 1) then CustomCode12 end as CustomCode12,
case when ((select enabled from customcodes where id = 13) = 1) then CustomCode13 end as CustomCode13,
case when ((select enabled from customcodes where id = 14) = 1) then CustomCode14 end as CustomCode14,
case when ((select enabled from customcodes where id = 15) = 1) then CustomCode15 end as CustomCode15,
case when ((select enabled from customcodes where id = 16) = 1) then CustomCode16 end as CustomCode16,
case when ((select enabled from customcodes where id = 17) = 1) then CustomCode17 end as CustomCode17,
case when ((select enabled from customcodes where id = 18) = 1) then CustomCode18 end as CustomCode18,
case when ((select enabled from customcodes where id = 19) = 1) then CustomCode19 end as CustomCode19,
case when ((select enabled from customcodes where id = 20) = 1) then CustomCode20 end as CustomCode20
from Customers
where customeraccountnumber = 'XXX'
But need a Linq equivalent. I can remove the null values returned elsewhere.
You can make a join like this :
using (yourDbContext dc = new yourDbContext())
{
var result = (from ro in dc.YourFirstTable
join pr in dc.YourSecondTable
on ro.RelatedID equals pr.RelatedID
where ro.Enabled == true &&
pr.AnyColumnYouWant == "SomeValue"
select new
{
FirstProperty = ro.User_FullName, //you give a name to your property,
SecondProperty = pr.Proj_Date
...
...
}).ToList();
}
Related
I'm trying to create an SQL that added two more conditions when the specific column(value) is matched.
Example:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOLD_COLUMN_1
END = 1
It is working when AGE = 18 it will return value that ADULTHOOLD_COLUMN = 1 and ADULTHOOLD_COLUMN_1 = 1
but when age is not equal to 18 it will not return any rows.
I was expecting that all of the data(with 1 or 0) will return.
ADULTHOOLD_COLUMN(Value in db is 1 or 0)
ADULTHOOLD_COLUMN_1(Value in db is 1 or 0)
Sounds a bit strange, but ok, looks like you need else:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN
ELSE 1
END = 1
AND
CASE
WHEN AGE = '18'
THEN ADULTHOOD_COLUMN_1
ELSE 1
END = 1
Or maybe just to make it more readable:
SELECT
COLUMN_NAME
FROM
TABLE
WHERE
NAME_COLUMN = 'NAME'
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN = 1)
AND (AGE is null OR AGE!='18' OR AGE = '18' AND ADULTHOOD_COLUMN_1 = 1)
PS. Are you sure that AGE is not number? If that's a number datatype, you need to replace '18' to 18
I want to run a multi-column correlated update of this kind:
UPDATE t1 t1_alias
SET (table_name, tablespace_name) = (
SELECT table_name, tablespace_name
FROM t2 t2_alias
WHERE t1_alias.table_name = t2_alias.table_name
);
But my attempt:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
... throws:
ORA-01407: cannot update ("FOO"."CUSTOMER"."CUSTOMER_NAME") to NULL
The source table customer_temp has no null values so I must be getting matches wrong. What is my error or misconception?
Presumably, there are some rows in the target table that have no match in the subquery.
You can avoid this with by adding an exists condition that filters out "unmatched" rows:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
)
where exists (
select 1
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
Getting error.. unable to define 2 parameters in one query
define price = 99.969000
define isin = '9128283C2';
WITH A AS
(select BUY_SELL,CASE WHEN BUY_SELL = 0 THEN (&price - 0.0025) ELSE (&price + 0.0025) END BS,PRODUCT_ISIN,QUANTITY
from table1 t where t.t_dt = '01-nov-17'
AND BUY_SELL = 0
AND quantity = 25 and product_isin = &isin)
SELECT * FROM A where row_num = 1;
I want to add following where clause to Linq query. How subquery like below using linq
WHERE (Restaurants.[IsActive] = 1)
AND exists
(
select 1 from APIKeys
where ApiKey = 'on35e5xbt3m4cbcef4e4448t6wssg11o'
and (KeyType = 1
and fk_RestaurantsID = [t2].[RestaurantsID]
or KeyType = 2
and fk_RestaurantGroupID = RG.RestaurantGroupsID
and [t1].[fk_RestaurantsID] in
(SELECT RestaurantsID
FROM Restaurants
WHERE RestaurantGroupsID = RG.RestaurantGroupsID))
)
AND (0 = (COALESCE([t0].[fk_MembersID],0)))
AND (1 = [t0].[fk_BookingStatusID])
AND ([t0].[Email] = 'nike.s#gmail.com')
AND (([t0].[Phone] = '9999999990') OR ([t0].[MobilePhone] = '9999999990'))
Use Any() to produce subquery which translated to EXISTS. E.g. with AdventureWorks database sample:
from p in Products
where p.FinishedGoodsFlag &&
SalesOrderDetails.Any(od => od.ProductID == p.ProductID)
select new { p.ProductID, p.Name }
Will produce following query to database:
SELECT [t0].[ProductID], [t0].[Name]
FROM [Production].[Product] AS [t0]
WHERE ([t0].[FinishedGoodsFlag] = 1) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM [Sales].[SalesOrderDetail] AS [t1]
WHERE [t1].[ProductID] = [t0].[ProductID]
))
How can I migrate this stored procedure into LINQ
DECLARE #customerIDs TABLE (customerID INT)
DECLARE #c INT
if (#customerName != '')
BEGIN
INSERT INTO #customerIDs
select id from customer
where name like '%' + #customerName + '%'
SELECT #c = COUNT(*) FROM #customerIDs
END
else
SET #c = 0
-- Insert statements for procedure here
SELECT DISTINCT l.*, dbo.GetListOfBranches(l.id) as 'list_of_branches', dbo.GetTopLevelCustomer(cid.customerID) FROM [login] l
LEFT JOIN login_to_customer ltc ON ltc.login_id = l.id
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID) AND
(l.surname LIKE '%' + ISNULL(#surname, l.surname) + '%') AND
(l.forename LIKE '%' + ISNULL(#forename, l.forename) + '%') AND
(user_type = ISNULL(#userType, user_type)) AND
(l.active = ISNULL(#active, l.active))
ORDER BY surname ASC
I have got this so far:
List<Int32> customerIds = new List<int>();
if (!String.IsNullOrEmpty(customerName))
{
CustomerDataContext custDataContext = new CustomerDataContext();
customerIds = (from a in custDataContext.Customers
where a.name.Contains(customerName)
select a.id).ToList<Int32>();
}
LoginDataContext dataContext = new LoginDataContext();
var query = (from t in dataContext.logins
join t2 in dataContext.login_to_customers on t.id equals t2.login_id into a
from subquery in a.DefaultIfEmpty()
select new { t, listOfBranches = dataContext.GetListOfBranches(t.id), topLevelCustomer = dataContext.GetTopLevelCustomer(t.id) });
I need would then do a
if (customerIds.Count() > 0)
{
query = query.Where(a => customerIds.Contains(a.t.customer_id))
}
but as I need to perform the distinct I cant have the customer_id column in the query. Is there a way to include the
LEFT JOIN #customerIDs cid ON cid.customerID = ltc.customer_id
WHERE
(#c = 0 OR ltc.customer_id = cid.customerID)
Within the LINQ?
Thanks,
Richard