query get records lesser than SQL - sql

This query which get records of all clubs, i check record per records. The NULL columns doesn't play any role. Howerver i filled NULL's. This mean any NULL isn't existing.
This query get 202 recordcount using cfquery and on MSSQL 2014 get 204 records.
Could you please tell what could happen that cfquery can't get some records. The exact query work on MSSQL 2014 perfectly.
SELECT tblDistrict.IDDist,
tblClubs.IDDiv,
tblClubs.ClubArtStart,
tblClubs.IDClub,
tblClubs.ClubName,
tblDivisionen.DivBezeichnung,
tblDivisionen.Region,
tblClubs.OrgDatum,
tblClubs.ChartDatum,
tblClubs.ClubStatus,
tblClubs.ClubArt,
tblClubs.Clubort
FROM (tblDistrict
INNER JOIN tblDivisionen ON tblDistrict.IDDist = tblDivisionen.IDDist)
INNER JOIN tblClubs ON tblDivisionen.IDDiv = tblClubs.IDDiv
WHERE (((tblDistrict.IDDist)=1)) AND Clubstatus<>'E' AND Clubstatus<>'I'

I would start with some formatting and aliasing. Your query would look something like this. This would be a lot more maintainable.
SELECT d.IDDist
, c.IDDiv
, c.ClubArtStart
, c.IDClub
, c.ClubName
, div.DivBezeichnung
, div.Region
, c.OrgDatum
, c.ChartDatum
, c.ClubStatus
, c.ClubArt
, c.Clubort
FROM tblDistrict d
INNER JOIN tblDivisionen div ON d.IDDist = div.IDDist
INNER JOIN tblClubs c ON div.IDDiv = c.IDDiv
WHERE d.IDDist = 1
AND c.Clubstatus not in ('E', 'I')

Related

How to make table name dynamic Postgresql

This is my query which join the result of query performed in postgresql from one database with another query from another database the thing is the table's name in the seconde database depend on the result of the query that target the first database. Here is the query:
select
gdr.id ,
attachment_id ,
state ,
gdr.user_id ,
gdr.document_id ,
internal_code,
ia."name" ,
gdd.id,tb2.col_3095
from
gediso_document_record as gdr
inner join ir_attachment ia on ia.id = gdr.attachment_id
inner join gediso_document_descriptor gdd on gdd.document_id = gdr.document_id
inner join (
select * from dblink('dbname=gediso_document','select id,record_id,col_3095 from doc_id_'||gdr.id)
AS tb2(id int,record_id int,col_3095 varchar)
)AS tb2 ON tb2.record_id = gdr.id
where
gdr.user_id =2031
limit 100
The problem resides in this line { select * from dblink('dbname=gediso_document','select id,record_id,col_3095 from doc_id_'||gdr.id) }
It won't allow me to use gdr.id
Can anyone help me please ? Thank you in advance.

Monitoring SQL transactions and their elapsed time in seconds

i'm currently challenged with the task to monitor an mssql server and i'd like to get an overview of pending/running transactions in tempDB.
I use following query to get a table of transactions and their elapsed_time_seconds
SELECT
a.session_id
, a.transaction_id
, a.transaction_sequence_num
, a.elapsed_time_seconds
, b.program_name
, b.open_tran
, b.STATUS
FROM sys.dm_tran_active_snapshot_database_transactions a
JOIN sys.sysprocesses b ON a.session_id = b.spid
ORDER BY elapsed_time_seconds DESC
The problem:
This query does not return anything if the table is empty.
Not even NULL. Additonally I dont speak SQL.
I've tried to place COALESCE and ISNULL in the query at different rows but this didn't help.
Can i somehow extend the query so that it returns 0 0 0 0 0 in the table row if nothing else is returned?
Thanks and best regards
Manuel
It's unclear why you would want this, but you can start off with a dummy VALUES constructor and left-join everything else.
Note that sysprocesses is deprecated
Note also that dm_tran_active_snapshot_database_transactions only shows transactions in the snapshot isolation level. You probably want dm_tran_active_transactions instead.
SELECT
ISNULL(t.session_id, 0)
, ISNULL(t.transaction_id, 0)
, ISNULL(t.transaction_sequence_num, 0)
, ISNULL(t.elapsed_time_seconds, 0)
, s.program_name
, ISNULL(s.open_transaction_count, 0)
, s.STATUS
FROM (VALUES(0)) v(dummy)
LEFT JOIN
sys.dm_tran_active_snapshot_database_transactions t
JOIN sys.dm_exec_sessions s ON s.session_id = t.spid
ON 1 = 1
ORDER BY elapsed_time_seconds DESC

Two select statements turn into one

I have set of two queries. In first query, if is separate from second, I got good results.
First query
SELECT *
FROM
(
SELECT nks.[Id]
, nks.[IdNarudzbe]
, nks.[IdArtikla] as artikal
, nks.[IdUsluge]
, nks.[Naziv]
, nks.Kolicina
, p.Naziv as kupac
, p.Id as kupacId
, p.Adresa
, p.Telefon
, nkz.[BrojDokumenta] AS nalog
, nkz.[BrojDokumentaKroz] AS nalogKroz
, nkz.[RokIsporuke]
, nkz.[IdNastaloOdDokumenta]
, d.Naziv as drzava
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
--...
from first query nkz.[IdNastaloOdDokumenta] is important to second
SELECT BrojDokumenta
, BrojDokumentaKroz
FROM .[dbo].[NarudzbeKupacaZaglavlje]
where id = nkz.[IdNastaloOdDokumenta]
For ex. In first query I got nkz.[IdNastaloOdDokumenta] = 20. Number 20 I use in second query in where statement, and value I get from BrojDokumenta, I would like to join to first query.
I was wondering if is possible to make one query out of these two. I think I can not union operator because number of column from these two queries don't match.
The same table, and the same columns are already in the first query. Perhaps you want a self-join, like this:
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz2
ON nkz2.Id = nkz.[IdNastaloOdDokumenta]
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0

Like SQL Statement SQL Server 2008R2

I have a simple query but I have mutiple records I need to filter out. I'm using the like statment with wild cards. Is there a better way do do this then writing out each one? Can I create a udf, table that it refrences? How? If I can. Thanks :)
SELECT a.SalesOrderNo ,
a.ShipExpireDate ,
a.CustomerNo ,
b.ItemCode ,
b.LineKey ,
b.QuantityOrdered ,
b.QuantityShipped ,
b.ItemCodeDesc ,
b.ExplodedKitItem
FROM dbo.SO_SalesOrderHeader a
LEFT JOIN dbo.SO_SalesOrderDetail b
ON a.SalesOrderNo = b.SalesOrderNo
WHERE b.ItemType = '1'
AND b.ItemCodeDesc NOT LIKE '%Cert%'
AND b.ItemCodeDesc NOT LIKE '%Fee%'
AND b.ItemCodeDesc NOT LIKE '%Tag%'
AND b.ItemCode NOT LIKE 'GF%'
AND b.ItemCode NOT LIKE 'PXDIALPREP'
AND b.ItemCode NOT LIKE '/C%'
AND a.ShipExpireDate = CONVERT(DATE, GETDATE(), 101)
Here's a different design that lets you put ItemCodeDesc in a seperate table (this could also be a TVF). I can't comment on performance though.
On a different note, be aware that because you are outer joining to sales order detail, this table can have NULL records. In turn your b.ItemType = '1' will always be FALSE when ItemType is NULL. So you may as well make it an inner join (and you might find your query plan is doing that anyway)
SELECT a.SalesOrderNo ,
a.ShipExpireDate ,
a.CustomerNo ,
b.ItemCode ,
b.LineKey ,
b.QuantityOrdered ,
b.QuantityShipped ,
b.ItemCodeDesc ,
b.ExplodedKitItem
FROM dbo.SO_SalesOrderHeader a
LEFT JOIN dbo.SO_SalesOrderDetail b
ON a.SalesOrderNo = b.SalesOrderNo
WHERE b.ItemType = '1'
AND b.ItemCode NOT LIKE 'GF%'
AND b.ItemCode NOT LIKE 'PXDIALPREP'
AND b.ItemCode NOT LIKE '/C%'
AND a.ShipExpireDate = CONVERT(DATE, GETDATE(), 101)
AND NOT EXISTS (
SELECT 1 FROM dbo.MappingTable MT
WHERE b.ItemCodeDesc LIKE MT.ItemCodeDesc
)
Note: I am guessing that your criteria is meant to filter out item types that can't be shipped (like Fees), adjust as per your requirements.
The problem you are encountering is a result of discrete values being stored in an ID. Looks like you should have a column IsShippable, or better yet a code table for ItemCodeType with rows of Cert, Fee, Tag, etc. and the IsShippable column there. if you had a code table then you'd be able to do
inner join ItemCodeTypes ict on ict.ItemCodeTypeId = b.ItemCodeTypeId and ict.IsShippable = 1
Cert, Fee, Tag, rows in the ItemCodeTypes table would have IsShippable = 0:
Id | Name | IsShippable
1 Cert 0
2 Fee 0
3 Tag 0
4 Product 1
5 Book 1
Edit: To more directly answer your question, you could make a view like this, and then when you query from it easily filter on Where IsShippable = 1:
Select CASE
When b.ItemCodeDesc LIKE '%Cert%' Then 0
When b.ItemCodeDesc LIKE '%Fee%' Then 0
--etc.
Else 1
END as IsShippable
,*
From dbo.SO_SalesOrderDetail

Records repeat for SSRS 2005 report

When I run a report for a purchase order, the report duplicates records for product codes.
For example the purchase order is: P000976, the report display the product code twice when it should only appear once. 45-5540 appears twice.
P000976 09-17-2012 15,040.00 15,040.00 0.00
45-5540 "Lordotic Cervical Spacer 10mm
Lordotic Cervical Spacer 10mm" 20 20 0
45-5540 "Lordotic Cervical Spacer 10mm
Lordotic Cervical Spacer 10mm" 20 20 0
When I put the report's SQL in SQL server and run the sql by seeing where the code cause the additional product code it is this line within the SQL:
join all_product_codes_VW p on q.distpartno = p.distpartno
select q.specialrequirement
, q.distpartno
, q.toproduce
, q.prodbegindate
, q.distributor
, rc.report_category_name
, s.productperpo
, r.ebi_released
, w.ebi_in_WIP
, p.distproductname
, tp.typeprefixdetail
, tp.cost
, '1' as ReportTotals
from all_required_vw q
left join all_shipped_grafts_new_VW s on (q.distpartno = s.distpartno and q.specialrequirement = s.ponumber)
left join all_released_Grafts_VW r on q.distpartno = r.distpartno
left join all_in_WIP_VW w on q.distpartno = w.distpartno
join all_product_codes_VW p on q.distpartno = p.distpartno
join setup_tissue_prefix tp on q.typenumber = tp.typeprefix
join setup_report_category_1 rc on q.distributor = rc.report_category_id
where q.prodbegindate < #enddate
and q.completed = '0'
and rc.report_category_name like '%' + isnull(#tcustomer, '') + '%'
order by q.prodbegindate, p.distproductname
This is the SQL for the view for which the join creates the duplicate.
SELECT COUNT_BIG(*) AS BIG, DistPartNo, DistProductName, Distributor, UMTBProductCode
FROM dbo.Setup_Distributor_Product_info
WHERE (Distributor <> '7') OR (Distributor IS NULL)
GROUP BY DistPartNo, DistProductName, Distributor, USSAProductCode
If you comment out these lines
--, p.distproductname
--join all_product_codes_VW p on q.distpartno = p.distpartno
Does the query return single rows for each distpartno? If yes then you're right that the all_products_code_VW view is causing the multiple rows.
Run these two queries and look at how many rows are in each and it will give you a clue as to why this is:
Select * from all_required_vw where distpartno = '45-5540'
Select * from all_product_codes_VW where distpartno = '45-5540'
My guess is that joining on only the distpartno is not enough to give you unique results. There might be more than one distributor for the same part, or multiple productnames for the same part number, e.g. different distrubutors using the the same part number with different products.
Possible this GROUP BY clause
GROUP BY DistPartNo, DistProductName, Distributor, USSAProductCode
need replace on this
GROUP BY DistPartNo, DistProductName, Distributor, UMTBProductCode