Issue for Group By in Stored Procedure - stored-procedures

Now the below code is grouped by employeeid
select
COUNT(employeelog.employeeid) as empVisit,
employeelog.employeeid as employeeID
into
#tempdr
from
Filteredps_employeelog as employeelog
where
employeelog.ps_employeetypename in (#employeevisit, #empfamvisit)
group by
employeelog.employeeid
I want to group by admissionid. When I tried as below expected result is not returned. Is this the right way to group by admissionid?
select
COUNT(employeelog.admissionid) as empadmission
into
#tempdr
from
Filteredps_employeelog as employeelog
where
employeelog.ps_employeetypename in (#employeevisit, #empfamvisit)
group by
employeelog.admissionid
Thanks

so, you need to get number of employees with same admissionid right?
change your query like this..
select employeelog.admissionid,COUNT(employeelog.employeeid) as empadmission
into #tempdr
from Filteredps_employeelog as employeelog
where employeelog.ps_employeetypename in(#employeevisit ,#empfamvisit)
group by employeelog.admissionid,employeelog.employeeid
EDIT:
try this one if you just want the admissionid count for each
select employeelog.admissionid,COUNT(employeelog.admissionid) as empadmission
into #tempdr
from Filteredps_employeelog as employeelog
where employeelog.ps_employeetypename in(#employeevisit ,#empfamvisit)
group by employeelog.admissionid

Related

Hyperlink base on another column

I'm new to BigQuery. Is there a way to Hyperlink base on another column's info?
In other words, how to hyperlink a specific column based on a specific ID (in another column) in the same table? E.g. How to create a hyperlink column "Program" by referencing the "SF_Record_ID" ID
SELECT CASE WHEN PMD_PRODUCT_NAME LIKE '%MANO%' THEN 'Brazil MAO'
WHEN PC.COUNTRY ='BR' THEN 'Brazil'
ELSE GEO.COUNTRY_DESCRIPTION
END AS Country
, IFNULL(PMD.PMD_FRANCHISE,"Unspecified") as Franchise
, PC.PMD_PROGRAM_NAME AS Progam
, PC.PMD_PRODUCT_NAME AS Product
, PC.KEY_CUSTOMER AS Key_Customer
, PC.PRODUCT_STATUS AS Product_Status
, PC.SF_RECORD_ID AS SF_Record_ID
, PC.PRODUCT_COST AS Cost
SF_Record_ID is the unique ID
Link: "https://XXXXXXXXXXXXXX/SF_Record_ID/view"
You can use dynamic SQL to achieve this:
DECLARE HLINK_PREFIX STRING;
DECLARE HLINK_POSTFIX STRING;
SET HLINK_PREFIX = "https://XXXXXXXXXXXXXX/";
SET HLINK_POSTFIX = "/view";
EXECUTE IMMEDIATE FORMAT("""SELECT concat(concat('%t', SF_Record_ID),'%t') FROM table""",HLINK_PREFIX,HLINK_POSTFIX);

Yii2: How to do a inner join of three tables?

I have a many-to-many relationship so I have three tables (administrators, offices and the intermediate table):
I need to do a very simple innerjoin of the three tables to show lastnames and their offices with Active Record. I tried but I couldn't.
This is what I tried:
$admins = Admins::find()
->joinWith(Oficinas::tableName())
->all();
echo '<pre>';
print_r($admins);
echo '</pre>';
die();
Also I would like to know how to show the SQL query so it can help me to find a solution.
You need to specify the relation name for joinWith() rather than table names. Since there isn't any info on the relation names I will use simple innerJoin using table names as per your requirements to display the last name and the office name for the admins.
Admins::find()
->alias('a')
->select('a.lastnameadm, o.nombreofi')
->innerJoin('admin_oficinas ao','ao.idadm = a.idadm')
->innerJoin('oficinas o','o.idofi = ao.idofi')
->all();
Try this:
TABLE_NAME_1::find()
->join('inner join',
'table_name_2',
'table_name_2.column = table_name_1.column'
);
->join('inner join',
'table_name_3',
'table_name_3.column = table_name_2.column'
)->all();
but if you can also use Via like the following example:
public function getClassschedule()
{
return $this->hasOne(TABLE_NAME_2::className(), ['table_name_1.column' => 'table_name_2.column'])
->via('tableName3');
}
when excuded, the results should be obtained like the previous example. so there's no need to repeated relations. full documentation can be seen here :
https://www.yiiframework.com/doc/guide/2.0/en/db-active-record

Masterdata Services udpValidateModel Procedure

i've got a few tables in MDS.
One table (clients) ist filled via SQL and the other one is a masterdata table (country) filled by hand.
I have a business rule on table clients:
"Name must be unique" and no b-rule on Country.
I want to validate the data PROGRAMMATICALLY i do not want to CLICK "apply business rules" in explorer window on the webinterface.
I found several threads about how to use the sp mentioned in the title (udpValidateModel) to validate all entities in a model.
well...this thing does nothing. I can see the validationStatus in each of my tables "Awaiting Revalidation" after changing business rules or update data via sql. It doesnt matter what i do the status wont change (neither the validation icons in webui).
i also tried validateentity but the same "nothing" happens.
The SP below:
DECLARE #User_ID int
DECLARE #Model_ID int
DECLARE #Version_ID int
SET #User_ID = (SELECT ID FROM [MasterDataServices].[mdm].[tblUser] where userName = SYSTEM_USER )
SET #Model_ID = (SELECT Top 1 Model_Id FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID')
SET #Version_ID = (SELECT Top 1 VersionNbr FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID'
ORDER BY ID DESC )
EXECUTE [MasterDataServices].[mdm].[udpValidateModel] #User_ID, #Model_ID, #Version_ID, 1
Can anyone help?
SP 'udpValidateModel' works perfectly fine, looks like the parameters you are populating is not correct.
You may correct this as below and try; make sure that the system user has full authorization for the model.
SET #User_ID = (SELECT ID FROM [MasterDataServices].[mdm].[tblUser] where userName = SYSTEM_USER )
SET #Model_ID = (SELECT Top 1 Model_Id FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID')
SET #Version_ID = (SELECT Top 1 VersionNbr FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = #Model_ID
ORDER BY ID DESC )

Access 2010 multiple field textbox not showing all

A textbox in a report I am trying to show properly is hiding multiples, even though the second field is different.
For example: "=[Client] & " " & [Invoice]".
If there is a Bob with multiple invoices, the report will only show one Bob and the lowest invoice number.
How can I make the textbox show every client and invoice individually?
Thanks.
Edit: The SQL requested
"SELECT [SUMFCRR&Plaintiff].Firm, [SUMFCRR&Plaintiff].[Firm Type], IIf([ReportID] Is Null,0,Val([ReportID])) AS [Report #], [SUMFCRR&Plaintiff].SumOfFeesBilled, [SUMFCRR&Plaintiff].[SumOfRecommended Reduction], [SUMFCRR&Plaintiff].[SumOfCosts Billed], [SUMFCRR&Plaintiff].[SumOfCost Reduction], [SumOfFeesBilled]+[SumOfCosts Billed]+[SumOfRecommended Reduction]+[SumOfCost Reduction] AS Reimbursement, [SUMFCRR&Plaintiff].Plaintiff, [SUMFCRR&Plaintiff].ReportID, [SumOfRecommended Reduction]+[SumOfCost Reduction] AS ReductionSum, [SUMFCRR&Plaintiff].Invoice, ([SumOfFeesBilled]+[SumOfRecommended Reduction]) AS FeeReimbursement, ([SumOfCosts Billed]+[SumOfCost Reduction]) AS CostReimbursement, -[SumOfRecommended Reduction] AS PositivefeeReduction, IIf([positivefeereduction]>[sumoffeesbilled] Or [sumofcost reduction]>[sumofcosts billed],"E"," ") AS ErrorNote, -[sumofcost reduction] AS PositveCostReduction
FROM [SUMFCRR&Plaintiff]
WHERE ((([SUMFCRR&Plaintiff].Firm) Like [which firm?] & "*") AND (([SUMFCRR&Plaintiff].ReportID)=[which report?]))
ORDER BY IIf([ReportID] Is Null,0,Val([ReportID]));"
Edit2: The two fields of concern here are [Plaintiff] and [Invoice], and all information is shown when I run the query itself.
Fixed it. It was originally in the [Plaintiff] grouping. I changed the group to [Invoice], and the multiple invoice numbers started showing.

SCCM 2007 Report to check servers time zone

I need to create a SCCM 2007 report to check the time zone over a specific collection. I found something to do this, but is not working when i insert it in the Report SQL Statement.
select SMS_R_System.Name, SMS_R_System.SMSAssignedSites, SMS_R_System.IPAddresses, SMS_R_System.IPSubnets, SMS_R_System.OperatingSystemNameandVersion, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.LastLogonUserDomain, SMS_R_System.LastLogonUserName, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.NetbiosName, SMS_G_System_COMPUTER_SYSTEM.CurrentTimeZone from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId where SMS_G_System_COMPUTER_SYSTEM.CurrentTimeZone != -360 order by name
For a report you need to use the database views, see below for a quickly modified query which should do what you need:
select
v_R_System.Name0,
v_GS_COMPUTER_SYSTEM.CurrentTimeZone0
from v_R_System
inner join v_GS_COMPUTER_SYSTEM on v_GS_COMPUTER_SYSTEM.ResourceID = v_R_System.ResourceId
join v_FullCollectionMembership ON v_FullCollectionMembership.ResourceID = v_R_System.ResourceID
where
v_GS_COMPUTER_SYSTEM.CurrentTimeZone0 != -360
AND v_FullCollectionMembership.CollectionID = '<yourcollectionid>'
order by Name0
Just replace "<yourcollectionid>" with the id you want to query.

Resources