2014-04-28 31 views
-1

我想知道是否有一種可能的方式來做這個選擇在Python中的功能,我想知道更有名的診斷,但它可以在3個不同的領域,我知道這個選擇可以工作,但我不知道如何將它放入Python中,或者如果還有其他什麼東西在Python中執行相同的操作。SQL選擇計數到python代碼

SELECT Count(*) 
FROM (
    SELECT DISTINCT 
      imp_diagnostica_99 
     , imp_diagnostica_100 
     , imp_diagnostica_101 
    FROM expmedico_expedienteconsultainicial 
    ) As distinctified 

我想獲得醫生髮送更多的程序。

model.py

class AutorizacionImagenologia(models.Model): 

imagenologia_fecha_solicitud_1 = models.DateField(auto_now=True) 
created_at = models.DateTimeField(auto_now_add=True) 
updated_at = models.DateField(auto_now=True) 
imagenologia_fecha_emision = models.DateField(auto_now=True, null=True, blank=True) 
tipo_plan = models.CharField(max_length=60, null=True, blank=True) 
empresa_empleadora = models.CharField(max_length=100, null=True, blank=True) 
imagenologia_hora_de_solicitud_2 = models.TimeField(auto_now=True, null=True, blank=True) 
imagenologia_medico_solicita_3 = models.ForeignKey(Medico, related_name='MedicoImagenologia', null=True) 
imagenologia_nombre_pac_4 = models.CharField(max_length=60, null=False, blank=True) 
imagenologia_apellido = models.CharField(max_length=60, null=False, blank=True) 
imagenologia_fecha_nacimiento_5 = models.DateField(max_length=10, null=True, blank=True) 
imagenologia_edad_miembro6 = models.CharField(max_length=10, null=True, blank=True) 
imagenologia_sexo_7 = models.CharField(max_length=10, null=True, blank=True) 
imagenologia_diagnostico_9 = models.CharField(max_length=100, null=True, blank=True) 
imagenologia_credencial = models.CharField(max_length=20, null=False) 
imagenologia_procedimiento = models.ForeignKey(
     Estudios_Imagenologia, 
     related_name='ProcedimientosImg', blank=True, null=True) 
imagenologia_procedimiento2 = models.ForeignKey(Estudios_Imagenologia, related_name='ProcedimientosImg2', blank=True, null=True) 
imagenologia_procedimiento3 = models.ForeignKey(
     Estudios_Imagenologia, related_name='ProcedimientosImg3', 
     blank=True, null=True) 

回答

0

您可以隨時使用RawQuerySet一個自定義模式(僅用於這條SQL),或使用連接光標API:

from django.db import connection 

with connection.cursor() as cursor: 
    cursor.execute('your_sql_here') 
    for row in cursor.fetchall(): 
     print 'row fields:', row[0], row[1], row[3] 
0

如果您正在使用Django並有一個模型expmedico_expedienteconsultainicial可以區分expmedico_expedienteconsultactive對象:

expmedico_expedienteconsultainicial.objects.all().values('imp_diagnostica_99', 'imp_diagnostica_100','imp_diagnostica_101').distinct().count() 

請給出你的Django模型並解釋你想要做什麼,以便我們可以給你最佳答案。

Django與查詢集一起使用的文檔https://docs.djangoproject.com/en/dev/ref/models/querysets/