Skip to content

Instantly share code, notes, and snippets.

@jesantos
jesantos / gist:ba0cda50c0c1fed50194
Created October 5, 2014 16:50
get count of rows per table
SELECT sc.name +'.'+ ta.name TableName
,SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN sys.schemas sc
ON ta.schema_id = sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name
ORDER BY SUM(pa.rows) DESC
@jesantos
jesantos / gist:1380519
Created November 20, 2011 17:16
Cross Tab gobi (voto_ok, votacionscore)
select partido, referencia, max(IF(numerovoto=58021,posicioncodigo,0)) '58021',
max(IF(numerovoto=58022,posicioncodigo,0)) '58022' from
(
select v.partido, v.referencia, v.NumeroVoto, v.posicioncodigo from voto_ok v
inner join votacionscore vs on v.NumeroVoto = vs.NumeroVoto and v.Legislatura = vs.legislatura
where v.Legislatura = 58
and vs.Presupuesto > 1
order by v.partido, v.Referencia, v.numerovoto
) votostats
group by partido, referencia
@jesantos
jesantos / gist:1277225
Created October 11, 2011 03:51
Excel VBA; Merge Values using a delimiter
Function MERGEVALUES(rn As Range, del As String)
Dim cell As Range
Dim result As String
Dim index As Integer
Dim textSplit As Variant
Dim curVal As String
For Each cell In rn
textSplit = Split(cell.text, del)
INSERT INTO gobi.votacion_def
(Legislatura, Descripcion, Sesion, Fecha, VotacionTotal, VotacionNum)
select Legislatura, Descripcion, str_to_date(Sesion, '%m/%d/%Y'), Fecha, VotacionTotal, VotacionNum
from tesis.votaciones;
@jesantos
jesantos / columnNames.sql
Created July 20, 2009 15:28
column names, types and length by table
USE DatabaseName
GO
SELECT column_name 'Column Name',
data_type 'Data Type',
CHARacter_maximum_length 'Maximum Length'
FROM information_schema.columns
WHERE table_name = 'TableName'
GO