linq query analogous to sql Not In, In clause - sql-server

I want the linq query analogous to sql query mentioned below
select HTId,HTN from tblHTMaster where HTId in (
select HTId from tblUHTs
where UId='F7ECFB41-177F-4408-B856-A4F669FAA714')
Thanks in advance

from s in db.tblMaster
let t= from u in db.tblUHTs
where u.UId='F7ECFB41-177F-4408-B856-A4F669FAA714'
select u.HTId
where t.Contains(s.HTId)
select new {s.HTId, s.HTN}

Related

Translate SELECT DISTINCT t-sql query to DAX expression

I need to create calculate table for the report in PowerBI Desktop.
I know how to do that in t-sql but I am unable to interpret it to DAX.
So should I use t-sql and add this query using "Get Data"?
Or should I create calculate table using DAX?
Which one is more efficient?
select distinct PolicyNumber,
ReserveStatus,
case when ReserveStatus = 'Open' then 1 else 0 end as OpenStatus
from RockhillClaimsDataFeed_PBI
group by PolicyNumber,ReserveStatus
Result looks like that:
can somebody help?
This is achievable by creating a calculated table in Power BI, with similar syntax using SELECTCOLUMNS and DISTINCT.
RockhillClaimsSummary =
DISTINCT(
SELECTCOLUMNS(
RockhillClaims,
"PolicyNumber", RockhillClaims[PolicyNumber],
"ReserveStatus", RockhillClaims[ReserveStatus],
"OpenStatus", IF(RockhillClaims[ReserveStatus] = "Open", 1, 0)
)
)
Results:

How to perform "select top 1 x from table" statement in spark sql

I am facing problem converting bellow query in spark-sql in pyspark
SQL-server query is
coalesce((Select top 1 f2.ChargeAmt from Fact_CMCharges f2
where f2.BldgID = f.BldgID
and f2.LeaseID = f.LeaseID
and f2.IncomeCat = f.IncomeCat
and f2.Period < f.Period
and f2.ActualProjected = 'Lease'
order by f2.Period desc),0) as Charge
I did not find replacing key word of top in pyspark sql . Kindly Help me
how could i convert this query in py-spark sql
Since you said Spark-SQL and if you have `DF', then you can use something like this.
df.limit(1).show()

SQL Server Management Studio says this statement is incorrect

I'm using SQL Server Management Studio to test some SQL queries, and the query below is said to have 'incorrect syntax near ','', which is the comma after 'image_uuid'. I read up on the syntax and I think I'm listing the columns correctly.
I need to move rows from one table to an archive table. I will be using this statement, then after drop the rows that I moved. If there's a cleaner way to do this, please explain what else I can do. Thanks!
INSERT INTO litmus_mailing_archive(image_uuid, server_id, list_id, mailing_id,
litmus_job_id, email_subject, test_email_message_id,
test_running, test_started, test_finished,
ended_with_error, images_purged, notification_email)
SELECT
(image_uuid, server_id, list_id, mailing_id,
litmus_job_id, email_subject, test_email_message_id,
test_running, test_started, test_finished,
ended_with_error, images_purged, notification_email)
FROM
litmus_mailing
WHERE
server_id = ?
AND list_id = ?
AND mailing_id = ?
AND test_running = 'false';
The question marks are just placeholders for the info. When I test it, I insert actual data that is in litmus_mailing.
Remove the parentheses from the select query:
SELECT (image_uuid, server_id, list_id, mailing_id,
litmus_job_id, email_subject, test_email_message_id,
test_running, test_started, test_finished, ended_with_error,
images_purged, notification_email)
Change it to this:
SELECT image_uuid, server_id, list_id, mailing_id,
litmus_job_id, email_subject, test_email_message_id,
test_running, test_started, test_finished,
ended_with_error, images_purged, notification_email
Remove the parentheses around the SELECT field list.

View from DB2 to SQL Server 2005

I'm attempting to move a view between DB2 and SQL Server.
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr
WHERE ( bus_cnty_cntry_cd, bus_st ) IN (SELECT cnty_cntry_cd,
st
FROM uhelp.cnty_cntry_cd
WHERE
cnty_cntry_descr LIKE '%invalid%');
The view works in DB2, but doesn't work with SQL Server because of the WHERE clause. Can I have a recommendation on how to rewrite this view to work with SQL Server?
It usually helps to define what "doesn't work" means (e.g. what error did you get) and also to specify the version of SQL Server you are using.
Unfortunately SQL Server doesn't support IN() with more than one clause. However you can re-write your view this way:
ALTER VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr AS mstr
WHERE EXISTS
(
SELECT 1
FROM uhelp.cnty_cntry_cd
WHERE cnty_cntry_descr LIKE '%invalid%'
AND cnty_cntry_cd = mstr.bus_cnty_cntry_cd
AND st = mstr.bus_st
);
one way
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr m
WHERE EXISTS( SELECT 1 FROM uhelp.cnty_cntry_cd c
WHERE c.cnty_cntry_descr LIKE '%invalid%'
AND c.bus_cnty_cntry_cd = m.bus_cnty_cntry_cd
AND c.st = m.bus_st)

NHibernate group by named parameters doesn't work with repeated parameters

My query in HQL is basically:
select functionA(a, :paramA), functionB(b, :paramB), functionC(c, :paramC), sum(d)
from tableA
groupby by functionA(a, :paramA), functionB(b, :paramB), functionC(c, :paramC)
However this gets turned into SQL of
select functionA(a, #param0), functionB(b, #param1), functionC(c, #param2), sum(d)
from tableA
groupby by functionA(a, #param3), functionB(b, #param4), functionC(c, #param5)
Now obviously this is going to throw a 'blah' is invalid in the select list because.... error as the group by clause doesn't match the select clause. I'm about to change this to a string format so I can get on with some productive work but if anyone has the answer as to why NHibernate will not reuse the same named query input the 2 times it is used that would be much appreciated.
A similar question seems to have been asked here with no real answer either.
This has been solved in NHibernate 3.0.0.Alpha1. You can get it here

Resources